index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <div class="cqcy-content" style="margin: 0;height: calc(100% - 70px);">
  3. <breadcrumb-view :breadcrumbList="breadcrumbList" :show-index="false"></breadcrumb-view>
  4. <el-button type="primary" class="cy-nav-btn" icon="el-icon-circle-plus-outline" size="mini" @click="handleAdd">新增</el-button>
  5. <el-divider></el-divider>
  6. <div class="cy-main">
  7. <el-table :data="groupDataList" style="width: 100%" border :stripe="true"
  8. @sort-change="sortChange"
  9. :header-cell-style="{background: '#E8E8E8'}">
  10. <el-table-column label="用户组名称" sortable="custom" prop="userGroupName" align="center"></el-table-column>
  11. <el-table-column label="排序号" sortable="custom" prop="sortNum" width="120" align="center"></el-table-column>
  12. <el-table-column label="备注" prop="userGroupDescribe" align="center"></el-table-column>
  13. <el-table-column label="创建时间" sortable prop="createTime" width="160" align="center"></el-table-column>
  14. <el-table-column label="操作" align="center">
  15. <template slot-scope="scope">
  16. <!-- <el-button size="mini" type="primary"-->
  17. <!-- @click="handleDetail(scope.$index, scope.row)">详情</el-button>-->
  18. <el-button size="mini"
  19. @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
  20. <el-button size="mini" type="danger"
  21. @click="handleDelete(scope.$index, scope.row)">删除</el-button>
  22. </template>
  23. </el-table-column>
  24. </el-table>
  25. </div>
  26. <!-- 新增用户组 -->
  27. <el-dialog
  28. :title="userGroupTitle"
  29. width="600px"
  30. top="10vh"
  31. center
  32. v-dialog-drag
  33. v-if="dialogAddUserGroup"
  34. :before-close="dialogClose"
  35. :visible.sync="dialogAddUserGroup"
  36. :close-on-click-modal="false"
  37. :append-to-body="true">
  38. <el-form ref="userGroupForm" :model="userGroupForm" :rules="userGroupRules" label-width="110px">
  39. <el-form-item label="用户组名称" prop="userGroupName">
  40. <el-input v-model="userGroupForm.userGroupName" maxlength="50"
  41. placeholder="请输入用户组名称" show-word-limit></el-input>
  42. </el-form-item>
  43. <el-form-item label="组人员" prop="groupUserList">
  44. <el-input :value="userGroupForm.userNameList.join(',')" placeholder="请选择用户"
  45. readonly style="width: calc(100% - 100px); margin-right: 15px;"></el-input>
  46. <el-button size="mini" @click="chooseUserEvent">选择人员</el-button>
  47. <span v-if="userGroupForm.userIdList.length > 0"
  48. style="font-size: 12px; color: #8e8c8c;">已选择 {{ userGroupForm.userIdList.length }} 人</span>
  49. </el-form-item>
  50. <el-form-item label="排序号" prop="sortNum">
  51. <el-input-number v-model="userGroupForm.sortNum" :min="1" :max="99999" label="请输入排序号"></el-input-number>
  52. </el-form-item>
  53. <el-form-item label="备注" prop="userGroupDescribe">
  54. <el-input type="textarea" v-model="userGroupForm.userGroupDescribe" maxlength="200"
  55. placeholder="请输入备注" show-word-limit></el-input>
  56. </el-form-item>
  57. </el-form>
  58. <span slot='footer'>
  59. <el-button type="primary" @click="addUserGroup">确定</el-button>
  60. <el-button @click="dialogClose">取消</el-button>
  61. </span>
  62. </el-dialog>
  63. <!-- 用户树 -->
  64. <el-dialog
  65. title="请选择用户"
  66. width="80%"
  67. top="10vh"
  68. center
  69. v-dialog-drag
  70. v-if="dialogDepartmentTree"
  71. :before-close="handleDeptCancel"
  72. :visible.sync="dialogDepartmentTree"
  73. :close-on-click-modal="false"
  74. :append-to-body="true">
  75. <div style="display: flex;height: 50vh; overflow: auto;">
  76. <div style="width: calc(30% - 15px); margin-right: 15px;">
  77. <el-tree :data="departments" :default-expand-all="true" @node-click="handleNodeClick"></el-tree>
  78. </div>
  79. <div style="width: 70%; border-left: 1px solid #d8d6d6; padding-left: 20px;">
  80. <el-table :data="userDataList" border :stripe="true"
  81. @sort-change="sortChange1"
  82. :header-cell-style="{background: '#E8E8E8'}">
  83. <el-table-column label="用户名称" sortable="custom" prop="userName" align="center"></el-table-column>
  84. <el-table-column label="最后登录时间" sortable prop="lastLoginTime" width="160" align="center"></el-table-column>
  85. <el-table-column label="操作" align="center">
  86. <template slot-scope="scope">
  87. <el-link type="danger" :underline="false" icon="el-icon-delete"
  88. v-if="scope.row.hasAdd"
  89. @click="removeUserForm(scope.$index, scope.row)">移除</el-link>
  90. <el-link type="primary" :underline="false" icon="el-icon-circle-plus-outline"
  91. v-else
  92. @click="addUserForm(scope.$index, scope.row)">添加</el-link>
  93. </template>
  94. </el-table-column>
  95. </el-table>
  96. </div>
  97. </div>
  98. </el-dialog>
  99. </div>
  100. </template>
  101. <script>
  102. import BreadcrumbView from '@/components/BreadcrumbView'
  103. import {customCompare, showAlertWin, showLoading} from "@/utils/cqcy";
  104. import {
  105. addUserGroup,
  106. delUserGroupById,
  107. getAllUserGroup,
  108. getClientUserByDepartmentId,
  109. getDepartmentTree, getUserByGroupId,
  110. updateUserGroup
  111. } from "@/api/user";
  112. export default {
  113. name: 'index',
  114. components: {
  115. BreadcrumbView
  116. },
  117. data() {
  118. return {
  119. breadcrumbList: ['用户组'],
  120. dialogAddUserGroup: false,
  121. dialogDepartmentTree: false,
  122. userGroupTitle: '新增用户组',
  123. groupDataList: [],
  124. userDataList: [],
  125. departments: [],
  126. userGroupForm: {
  127. id: null,
  128. userGroupName: null,
  129. sortNum: 1,
  130. userGroupDescribe: null,
  131. userNameList: [],
  132. userIdList: []
  133. },
  134. userGroupRules: {
  135. userGroupName: [
  136. {required: true, trigger: "blur", message: "用户组名称不能为空"}
  137. ]
  138. }
  139. }
  140. },
  141. watch: {
  142. },
  143. beforeDestroy() {
  144. },
  145. created() {
  146. this.getAllUserGroup()
  147. },
  148. methods: {
  149. sortChange({ prop, order }) {
  150. this.groupDataList.sort(customCompare(prop, order))
  151. },
  152. sortChange1({ prop, order }) {
  153. this.userDataList.sort(customCompare(prop, order))
  154. },
  155. /** 新增 */
  156. handleAdd() {
  157. this.userGroupForm = {
  158. id: null,
  159. userGroupName: null,
  160. sortNum: 1,
  161. userGroupDescribe: null,
  162. userNameList: [],
  163. userIdList: []
  164. }
  165. this.userGroupTitle = '新增用户组'
  166. this.dialogAddUserGroup = true
  167. },
  168. /** 详情 */
  169. handleDetail(index, row) {
  170. console.log(row)
  171. },
  172. /** 编辑 */
  173. handleEdit(index, row) {
  174. this.userGroupTitle = '编辑用户组'
  175. this.userGroupForm = {
  176. id: row.id,
  177. userGroupName: row.userGroupName,
  178. sortNum: row.sortNum,
  179. userGroupDescribe: row.userGroupDescribe,
  180. userNameList: [],
  181. userIdList: []
  182. }
  183. getUserByGroupId(row.id).then(res => {
  184. let data = res.data
  185. for (let i = 0; i < data.length; i ++) {
  186. this.userGroupForm.userIdList.push(data[i].userId)
  187. this.userGroupForm.userNameList.push(data[i].userName)
  188. }
  189. this.dialogAddUserGroup = true
  190. }).catch((e) => {
  191. this.dialogAddUserGroup = true
  192. showAlertWin(this, e)
  193. })
  194. },
  195. /** 删除 */
  196. handleDelete(index, row) {
  197. this.$confirm('您确定要删除该用户组吗?', '温馨提示', {
  198. confirmButtonText: '确定',
  199. cancelButtonText: '取消',
  200. customClass: 'close_confirm',
  201. closeOnClickModal: false
  202. }).then(() => {
  203. const loading = showLoading(this, '删除中,请稍候···')
  204. delUserGroupById(row.id).then(res => {
  205. loading.close()
  206. this.$message({
  207. message: '删除成功!',
  208. type: 'success'
  209. })
  210. this.getAllUserGroup()
  211. }).catch((e) => {
  212. loading.close()
  213. showAlertWin(this, e)
  214. })
  215. }).catch(() => {
  216. })
  217. },
  218. /** 修改选择用户信息 */
  219. updateChooseUserInfo(userId, flag) {
  220. for (let i = 0; i < this.userDataList.length; i ++) {
  221. if (this.userDataList[i].userId === userId) {
  222. this.$set(this.userDataList[i], 'hasAdd', flag)
  223. break
  224. }
  225. }
  226. },
  227. /** 添加用户 */
  228. addUserForm(index, row) {
  229. // 避免重复添加用户
  230. if (this.userGroupForm.userIdList.indexOf(row.userId) < 0) {
  231. this.userGroupForm.userIdList.push(row.userId)
  232. this.userGroupForm.userNameList.push(row.userName)
  233. }
  234. // 修改当前用户组数据
  235. this.updateChooseUserInfo(row.userId, true)
  236. },
  237. /** 移除用户 */
  238. removeUserForm(index, row) {
  239. // 判断用户是否存在
  240. let idx = this.userGroupForm.userIdList.indexOf(row.userId)
  241. if (idx < 0) {
  242. return
  243. }
  244. this.userGroupForm.userIdList.splice(idx, 1)
  245. this.userGroupForm.userNameList.splice(idx, 1)
  246. // 修改当前用户组数据
  247. this.updateChooseUserInfo(row.userId, false)
  248. },
  249. /** 组织树节点点击事件 */
  250. handleNodeClick(data, node, target) {
  251. getClientUserByDepartmentId(data.id).then(res => {
  252. if (!res.data) {
  253. return
  254. }
  255. this.userDataList = res.data
  256. let userIdList = this.userGroupForm.userIdList
  257. for (let i = 0; i < userIdList.length; i ++) {
  258. // 修改当前用户组数据
  259. this.updateChooseUserInfo(userIdList[i], true)
  260. }
  261. }).catch((e) => {
  262. showAlertWin(this, e)
  263. })
  264. },
  265. /** 选择用户事件 */
  266. chooseUserEvent() {
  267. this.userDataList = []
  268. this.loadDepartmentUser()
  269. this.dialogDepartmentTree = true
  270. },
  271. /** 用户树取消选择 */
  272. handleDeptCancel() {
  273. this.dialogDepartmentTree = false
  274. },
  275. /** 加载部门下客户端人员 */
  276. loadDepartmentUser() {
  277. getDepartmentTree().then(res => {
  278. if (!res.data) {
  279. return
  280. }
  281. this.departments = res.data
  282. }).catch((e) => {
  283. showAlertWin(this, e)
  284. })
  285. },
  286. /** 获取用户组 */
  287. getAllUserGroup() {
  288. getAllUserGroup().then(res => {
  289. if (!res || !res.data) {
  290. return
  291. }
  292. this.$nextTick(() => {
  293. this.groupDataList = res.data
  294. })
  295. }).catch((e) => {
  296. showAlertWin(this, e)
  297. })
  298. },
  299. /** 添加用户组 */
  300. addUserGroup() {
  301. this.$refs['userGroupForm'].validate((valid) => {
  302. if (valid) {
  303. const loading = showLoading(this, '保存中,请稍候···')
  304. if (this.userGroupForm.id) {
  305. updateUserGroup(this.userGroupForm).then(res => {
  306. loading.close()
  307. this.$message({
  308. message: '修改成功!',
  309. type: 'success'
  310. })
  311. this.dialogAddUserGroup = false
  312. this.userGroupForm.id = null
  313. this.$refs['userGroupForm'].resetFields()
  314. this.getAllUserGroup()
  315. }).catch((e) => {
  316. loading.close()
  317. showAlertWin(this, e)
  318. })
  319. return
  320. }
  321. addUserGroup(this.userGroupForm).then(res => {
  322. loading.close()
  323. this.$message({
  324. message: '保存成功!',
  325. type: 'success'
  326. })
  327. this.dialogAddUserGroup = false
  328. this.$refs['userGroupForm'].resetFields()
  329. this.getAllUserGroup()
  330. }).catch((e) => {
  331. loading.close()
  332. showAlertWin(this, e)
  333. })
  334. }
  335. })
  336. },
  337. /** 弹出层关闭事件 */
  338. dialogClose(done) {
  339. if (this.$refs['userGroupForm'])
  340. this.$refs['userGroupForm'].resetFields()
  341. if (typeof(done) === 'function') {
  342. done()
  343. } else {
  344. this.dialogAddUserGroup = false
  345. }
  346. this.getAllUserGroup()
  347. }
  348. }
  349. }
  350. </script>
  351. <style rel="stylesheet/scss" lang="scss">
  352. .breadcrumb-content {
  353. padding-bottom: 0;
  354. }
  355. .cy-main {
  356. margin: 10px 20px;
  357. width: calc(100% - 40px);
  358. height: calc(100% - 100px);
  359. }
  360. .cy-nav-btn {
  361. float: right!important;
  362. margin-right: 20px!important;
  363. margin-top: -20px!important;
  364. }
  365. </style>