index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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-and-zoom
  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, showAlertMsgWin, showAlertWin, showConfirmWin, 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, null, e)
  193. })
  194. },
  195. /** 删除 */
  196. handleDelete(index, row) {
  197. showConfirmWin(this, null, '您确定要删除该用户组吗?', () => {
  198. const loading = showLoading(this, '删除中,请稍候···')
  199. delUserGroupById(row.id).then(res => {
  200. loading.close()
  201. showAlertMsgWin(this, null, '删除成功!')
  202. this.getAllUserGroup()
  203. }).catch((e) => {
  204. loading.close()
  205. showAlertWin(this, null, e)
  206. })
  207. })
  208. },
  209. /** 修改选择用户信息 */
  210. updateChooseUserInfo(userId, flag) {
  211. for (let i = 0; i < this.userDataList.length; i ++) {
  212. if (this.userDataList[i].userId === userId) {
  213. this.$set(this.userDataList[i], 'hasAdd', flag)
  214. break
  215. }
  216. }
  217. },
  218. /** 添加用户 */
  219. addUserForm(index, row) {
  220. // 避免重复添加用户
  221. if (this.userGroupForm.userIdList.indexOf(row.userId) < 0) {
  222. this.userGroupForm.userIdList.push(row.userId)
  223. this.userGroupForm.userNameList.push(row.userName)
  224. }
  225. // 修改当前用户组数据
  226. this.updateChooseUserInfo(row.userId, true)
  227. },
  228. /** 移除用户 */
  229. removeUserForm(index, row) {
  230. // 判断用户是否存在
  231. let idx = this.userGroupForm.userIdList.indexOf(row.userId)
  232. if (idx < 0) {
  233. return
  234. }
  235. this.userGroupForm.userIdList.splice(idx, 1)
  236. this.userGroupForm.userNameList.splice(idx, 1)
  237. // 修改当前用户组数据
  238. this.updateChooseUserInfo(row.userId, false)
  239. },
  240. /** 组织树节点点击事件 */
  241. handleNodeClick(data, node, target) {
  242. getClientUserByDepartmentId(data.id).then(res => {
  243. if (!res.data) {
  244. return
  245. }
  246. this.userDataList = res.data
  247. let userIdList = this.userGroupForm.userIdList
  248. for (let i = 0; i < userIdList.length; i ++) {
  249. // 修改当前用户组数据
  250. this.updateChooseUserInfo(userIdList[i], true)
  251. }
  252. }).catch((e) => {
  253. showAlertWin(this, null, e)
  254. })
  255. },
  256. /** 选择用户事件 */
  257. chooseUserEvent() {
  258. this.userDataList = []
  259. this.loadDepartmentUser()
  260. this.dialogDepartmentTree = true
  261. },
  262. /** 用户树取消选择 */
  263. handleDeptCancel() {
  264. this.dialogDepartmentTree = false
  265. },
  266. /** 加载部门下客户端人员 */
  267. loadDepartmentUser() {
  268. getDepartmentTree().then(res => {
  269. if (!res.data) {
  270. return
  271. }
  272. this.departments = res.data
  273. }).catch((e) => {
  274. showAlertWin(this, null, e)
  275. })
  276. },
  277. /** 获取用户组 */
  278. getAllUserGroup() {
  279. getAllUserGroup().then(res => {
  280. if (!res || !res.data) {
  281. return
  282. }
  283. this.$nextTick(() => {
  284. this.groupDataList = res.data
  285. })
  286. }).catch((e) => {
  287. showAlertWin(this, null, e)
  288. })
  289. },
  290. /** 添加用户组 */
  291. addUserGroup() {
  292. this.$refs['userGroupForm'].validate((valid) => {
  293. if (valid) {
  294. const loading = showLoading(this, '保存中,请稍候···')
  295. if (this.userGroupForm.id) {
  296. updateUserGroup(this.userGroupForm).then(res => {
  297. loading.close()
  298. showAlertMsgWin(this, null, '修改成功!')
  299. this.dialogAddUserGroup = false
  300. this.userGroupForm.id = null
  301. this.$refs['userGroupForm'].resetFields()
  302. this.getAllUserGroup()
  303. }).catch((e) => {
  304. loading.close()
  305. showAlertWin(this, null, e)
  306. })
  307. return
  308. }
  309. addUserGroup(this.userGroupForm).then(res => {
  310. loading.close()
  311. showAlertMsgWin(this, null, '保存成功!')
  312. this.dialogAddUserGroup = false
  313. this.$refs['userGroupForm'].resetFields()
  314. this.getAllUserGroup()
  315. }).catch((e) => {
  316. loading.close()
  317. showAlertWin(this, null, e)
  318. })
  319. }
  320. })
  321. },
  322. /** 弹出层关闭事件 */
  323. dialogClose(done) {
  324. if (this.$refs['userGroupForm'])
  325. this.$refs['userGroupForm'].resetFields()
  326. if (typeof(done) === 'function') {
  327. done()
  328. } else {
  329. this.dialogAddUserGroup = false
  330. }
  331. this.getAllUserGroup()
  332. }
  333. }
  334. }
  335. </script>
  336. <style rel="stylesheet/scss" lang="scss">
  337. .breadcrumb-content {
  338. padding-bottom: 0;
  339. }
  340. .cy-main {
  341. margin: 10px 20px;
  342. width: calc(100% - 40px);
  343. height: calc(100% - 100px);
  344. }
  345. .cy-nav-btn {
  346. float: right!important;
  347. margin-right: 20px!important;
  348. margin-top: -20px!important;
  349. }
  350. </style>