index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <!--
  2. - Copyright (c) 2018-2025, lengleng All rights reserved.
  3. -
  4. - Redistribution and use in source and binary forms, with or without
  5. - modification, are permitted provided that the following conditions are met:
  6. -
  7. - Redistributions of source code must retain the above copyright notice,
  8. - this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. - notice, this list of conditions and the following disclaimer in the
  11. - documentation and/or other materials provided with the distribution.
  12. - Neither the name of the pig4cloud.com developer nor the names of its
  13. - contributors may be used to endorse or promote products derived from
  14. - this software without specific prior written permission.
  15. - Author: lengleng (wangiegie@gmail.com)
  16. -->
  17. <template>
  18. <div class="user">
  19. <basic-container>
  20. <el-row :span="24">
  21. <el-col
  22. :xs="24"
  23. :sm="24"
  24. :md="5"
  25. class="user__tree">
  26. <avue-tree
  27. :option="treeOption"
  28. :data="treeData"
  29. @node-click="nodeClick"/>
  30. </el-col>
  31. <el-col
  32. :xs="24"
  33. :sm="24"
  34. :md="19"
  35. class="user__main">
  36. <avue-crud
  37. ref="crud"
  38. :option="option"
  39. v-model="form"
  40. :page="page"
  41. :table-loading="listLoading"
  42. :before-open="handleOpenBefore"
  43. :data="list"
  44. @on-load="getList"
  45. @search-change="handleFilter"
  46. @refresh-change="handleRefreshChange"
  47. @row-update="update"
  48. @row-save="create">
  49. <template slot="menuLeft">
  50. <el-button
  51. v-if="sys_user_add"
  52. class="filter-item"
  53. type="primary"
  54. size="mini"
  55. icon="el-icon-edit"
  56. @click="handleCreate">添加
  57. </el-button>
  58. </template>
  59. <template
  60. slot="username"
  61. slot-scope="scope">
  62. <span>{{ scope.row.username }}</span>
  63. </template>
  64. <template
  65. slot="role"
  66. slot-scope="scope">
  67. <span
  68. v-for="(role,index) in scope.row.roleList"
  69. :key="index">
  70. <el-tag>{{ role.roleName }} </el-tag>&nbsp;&nbsp;
  71. </span>
  72. </template>
  73. <template
  74. slot="deptId"
  75. slot-scope="scope">
  76. {{ scope.row.deptName }}
  77. </template>
  78. <template
  79. slot="lockFlag"
  80. slot-scope="scope">
  81. <el-tag>{{ scope.label }}</el-tag>
  82. </template>
  83. <template
  84. slot="menu"
  85. slot-scope="scope">
  86. <el-button
  87. v-if="sys_user_edit"
  88. type="text"
  89. size="mini"
  90. icon="el-icon-edit"
  91. @click="handleUpdate(scope.row,scope.index)">编辑
  92. </el-button>
  93. <el-button
  94. v-if="sys_user_del"
  95. type="text"
  96. size="mini"
  97. icon="el-icon-delete"
  98. @click="deletes(scope.row,scope.index)">删除
  99. </el-button>
  100. </template>
  101. <template
  102. slot="deptIdForm"
  103. slot-scope="scope">
  104. <avue-input
  105. v-model="form.deptId"
  106. :node-click="getNodeData"
  107. :dic="treeDeptData"
  108. :props="defaultProps"
  109. type="tree"
  110. placeholder="请选择所属部门"/>
  111. </template>
  112. <template
  113. slot="roleForm"
  114. slot-scope="scope">
  115. <avue-select
  116. v-model="role"
  117. :dic="rolesOptions"
  118. :props="roleProps"
  119. multiple
  120. placeholder="请选择角色"/>
  121. </template>
  122. </avue-crud>
  123. </el-col>
  124. </el-row>
  125. </basic-container>
  126. </div>
  127. </template>
  128. <script>
  129. import {addObj, delObj, fetchList, putObj} from '@/api/admin/user'
  130. import {deptRoleList} from '@/api/admin/role'
  131. import {fetchTree} from '@/api/admin/dept'
  132. import {tableOption} from '@/const/crud/admin/user'
  133. import {mapGetters} from 'vuex'
  134. export default {
  135. name: 'SysUser',
  136. data() {
  137. return {
  138. searchForm: {},
  139. treeOption: {
  140. nodeKey: 'id',
  141. addBtn: false,
  142. menu: false,
  143. props: {
  144. label: 'name',
  145. value: 'id'
  146. }
  147. },
  148. treeData: [],
  149. option: tableOption,
  150. treeDeptData: [],
  151. checkedKeys: [],
  152. roleProps: {
  153. label: 'roleName',
  154. value: 'roleId'
  155. },
  156. defaultProps: {
  157. label: 'name',
  158. value: 'id'
  159. },
  160. page: {
  161. total: 0, // 总页数
  162. currentPage: 1, // 当前页数
  163. pageSize: 20, // 每页显示多少条,
  164. isAsc: false// 是否倒序
  165. },
  166. list: [],
  167. listLoading: true,
  168. role: [],
  169. form: {},
  170. rolesOptions: []
  171. }
  172. },
  173. computed: {
  174. ...mapGetters(['permissions'])
  175. },
  176. watch: {
  177. role() {
  178. this.form.role = this.role
  179. }
  180. },
  181. created() {
  182. this.sys_user_add = this.permissions['sys_user_add']
  183. this.sys_user_edit = this.permissions['sys_user_edit']
  184. this.sys_user_del = this.permissions['sys_user_del']
  185. this.init()
  186. },
  187. methods: {
  188. init() {
  189. fetchTree().then(response => {
  190. this.treeData = response.data.data
  191. })
  192. },
  193. nodeClick(data) {
  194. this.page.page = 1
  195. this.getList(this.page, {deptId: data.id})
  196. },
  197. getList(page, params) {
  198. this.listLoading = true
  199. fetchList(Object.assign({
  200. current: page.currentPage,
  201. size: page.pageSize
  202. }, params, this.searchForm)).then(response => {
  203. this.list = response.data.data.records
  204. this.page.total = response.data.data.total
  205. this.listLoading = false
  206. })
  207. },
  208. getNodeData() {
  209. deptRoleList().then(response => {
  210. this.rolesOptions = response.data.data
  211. })
  212. },
  213. handleDept() {
  214. fetchTree().then(response => {
  215. this.treeDeptData = response.data.data
  216. })
  217. },
  218. handleFilter(param) {
  219. this.searchForm = param
  220. this.getList(this.page, param)
  221. },
  222. handleRefreshChange() {
  223. this.getList(this.page)
  224. },
  225. handleCreate() {
  226. this.$refs.crud.rowAdd()
  227. },
  228. handleOpenBefore(show, type) {
  229. window.boxType = type
  230. this.handleDept()
  231. if (['edit', 'views'].includes(type)) {
  232. this.role = []
  233. for (var i = 0; i < this.form.roleList.length; i++) {
  234. this.role[i] = this.form.roleList[i].roleId
  235. }
  236. deptRoleList().then(response => {
  237. this.rolesOptions = response.data.data
  238. })
  239. } else if (type === 'add') {
  240. this.role = []
  241. }
  242. show()
  243. },
  244. handleUpdate(row, index) {
  245. console.log(row)
  246. this.$refs.crud.rowEdit(row, index)
  247. this.form.password = undefined
  248. },
  249. create(row, done, loading) {
  250. if (this.form.phone.indexOf('*') > 0) {
  251. this.form.phone = undefined
  252. }
  253. addObj(this.form).then(() => {
  254. this.getList(this.page)
  255. done()
  256. this.$notify({
  257. title: '成功',
  258. message: '创建成功',
  259. type: 'success',
  260. duration: 2000
  261. })
  262. }).catch(() => {
  263. loading()
  264. })
  265. },
  266. update(row, index, done, loading) {
  267. if (this.form.phone && this.form.phone.indexOf('*') > 0) {
  268. this.form.phone = undefined
  269. }
  270. putObj(this.form).then(() => {
  271. this.getList(this.page)
  272. done()
  273. this.$notify({
  274. title: '成功',
  275. message: '修改成功',
  276. type: 'success',
  277. duration: 2000
  278. })
  279. }).catch(() => {
  280. loading()
  281. })
  282. },
  283. deletes(row, index) {
  284. this.$confirm(
  285. '此操作将永久删除该用户(用户名:' + row.username + '), 是否继续?',
  286. '提示',
  287. {
  288. confirmButtonText: '确定',
  289. cancelButtonText: '取消',
  290. type: 'warning'
  291. }
  292. ).then(() => {
  293. delObj(row.userId)
  294. .then(() => {
  295. this.list.splice(index, 1)
  296. this.$notify({
  297. title: '成功',
  298. message: '删除成功',
  299. type: 'success',
  300. duration: 2000
  301. })
  302. })
  303. .cache(() => {
  304. this.$notify({
  305. title: '失败',
  306. message: '删除失败',
  307. type: 'error',
  308. duration: 2000
  309. })
  310. })
  311. })
  312. }
  313. }
  314. }
  315. </script>
  316. <style lang="scss">
  317. .user {
  318. height: 100%;
  319. &__tree {
  320. padding-top: 3px;
  321. padding-right: 20px;
  322. }
  323. &__main {
  324. .el-card__body {
  325. padding-top: 0;
  326. }
  327. }
  328. }
  329. </style>