user.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <div>
  3. <div class="button-box clearflex">
  4. <el-button size="mini" type="primary" icon="el-icon-plus" @click="addUser">新增用户</el-button>
  5. </div>
  6. <el-table :data="tableData" border stripe>
  7. <el-table-column label="头像" min-width="50">
  8. <template #default="scope">
  9. <div :style="{'textAlign':'center'}">
  10. <CustomPic :pic-src="scope.row.headerImg" />
  11. </div>
  12. </template>
  13. </el-table-column>
  14. <el-table-column label="uuid" min-width="250" prop="uuid" />
  15. <el-table-column label="用户名" min-width="150" prop="userName" />
  16. <el-table-column label="昵称" min-width="150" prop="nickName" />
  17. <el-table-column label="用户角色" min-width="150">
  18. <template #default="scope">
  19. <el-cascader
  20. v-model="scope.row.authorityIds"
  21. :options="authOptions"
  22. :show-all-levels="false"
  23. :props="{ multiple:true,checkStrictly: true,label:'authorityName',value:'authorityId',disabled:'disabled',emitPath:false}"
  24. filterable
  25. :clearable="false"
  26. @visible-change="(flag)=>{changeAuthority(scope.row,flag)}"
  27. @remove-tag="()=>{changeAuthority(scope.row,false)}"
  28. />
  29. </template>
  30. </el-table-column>
  31. <el-table-column label="操作" min-width="150">
  32. <template #default="scope">
  33. <el-popover v-model:visible="scope.row.visible" placement="top" width="160">
  34. <p>确定要删除此用户吗</p>
  35. <div style="text-align: right; margin: 0">
  36. <el-button size="mini" type="text" @click="scope.row.visible = false">取消</el-button>
  37. <el-button type="primary" size="mini" @click="deleteUser(scope.row)">确定</el-button>
  38. </div>
  39. <template #reference>
  40. <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
  41. </template>
  42. </el-popover>
  43. </template>
  44. </el-table-column>
  45. </el-table>
  46. <span style="color: red;font-size: 12px">注:右上角头像下拉可切换角色</span>
  47. <el-pagination
  48. :current-page="page"
  49. :page-size="pageSize"
  50. :page-sizes="[10, 30, 50, 100]"
  51. :style="{float:'right',padding:'20px'}"
  52. :total="total"
  53. layout="total, sizes, prev, pager, next, jumper"
  54. @current-change="handleCurrentChange"
  55. @size-change="handleSizeChange"
  56. />
  57. <el-dialog v-model="addUserDialog" custom-class="user-dialog" title="新增用户">
  58. <el-form ref="userForm" :rules="rules" :model="userInfo">
  59. <el-form-item label="用户名" label-width="80px" prop="username">
  60. <el-input v-model="userInfo.username" />
  61. </el-form-item>
  62. <el-form-item label="密码" label-width="80px" prop="password">
  63. <el-input v-model="userInfo.password" />
  64. </el-form-item>
  65. <el-form-item label="别名" label-width="80px" prop="nickName">
  66. <el-input v-model="userInfo.nickName" />
  67. </el-form-item>
  68. <el-form-item label="头像" label-width="80px">
  69. <div style="display:inline-block" @click="openHeaderChange">
  70. <img v-if="userInfo.headerImg" class="header-img-box" :src="(userInfo.headerImg && userInfo.headerImg.slice(0, 4) !== 'http')?path+userInfo.headerImg:userInfo.headerImg">
  71. <div v-else class="header-img-box">从媒体库选择</div>
  72. </div>
  73. </el-form-item>
  74. <el-form-item label="用户角色" label-width="80px" prop="authorityId">
  75. <el-cascader
  76. v-model="userInfo.authorityIds"
  77. :options="authOptions"
  78. :show-all-levels="false"
  79. :props="{ multiple:true,checkStrictly: true,label:'authorityName',value:'authorityId',disabled:'disabled',emitPath:false}"
  80. filterable
  81. :clearable="false"
  82. />
  83. </el-form-item>
  84. </el-form>
  85. <template #footer>
  86. <div class="dialog-footer">
  87. <el-button @click="closeAddUserDialog">取 消</el-button>
  88. <el-button type="primary" @click="enterAddUserDialog">确 定</el-button>
  89. </div>
  90. </template>
  91. </el-dialog>
  92. <ChooseImg ref="chooseImg" :target="userInfo" :target-key="`headerImg`" />
  93. </div>
  94. </template>
  95. <script>
  96. // 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
  97. const path = import.meta.env.VITE_BASE_API
  98. import {
  99. getUserList,
  100. setUserAuthorities,
  101. register,
  102. deleteUser
  103. } from '@/api/user'
  104. import { getAuthorityList } from '@/api/authority'
  105. import infoList from '@/mixins/infoList'
  106. import { mapGetters } from 'vuex'
  107. import CustomPic from '@/components/customPic/index.vue'
  108. import ChooseImg from '@/components/chooseImg/index.vue'
  109. export default {
  110. name: 'Api',
  111. components: { CustomPic, ChooseImg },
  112. mixins: [infoList],
  113. data() {
  114. return {
  115. listApi: getUserList,
  116. path: path,
  117. authOptions: [],
  118. addUserDialog: false,
  119. userInfo: {
  120. username: '',
  121. password: '',
  122. nickName: '',
  123. headerImg: '',
  124. authorityId: '',
  125. authorityIds: []
  126. },
  127. rules: {
  128. username: [
  129. { required: true, message: '请输入用户名', trigger: 'blur' },
  130. { min: 5, message: '最低5位字符', trigger: 'blur' }
  131. ],
  132. password: [
  133. { required: true, message: '请输入用户密码', trigger: 'blur' },
  134. { min: 6, message: '最低6位字符', trigger: 'blur' }
  135. ],
  136. nickName: [
  137. { required: true, message: '请输入用户昵称', trigger: 'blur' }
  138. ],
  139. authorityId: [
  140. { required: true, message: '请选择用户角色', trigger: 'blur' }
  141. ]
  142. }
  143. }
  144. },
  145. computed: {
  146. ...mapGetters('user', ['token'])
  147. },
  148. async created() {
  149. await this.getTableData()
  150. this.setAuthorityIds()
  151. const res = await getAuthorityList({ page: 1, pageSize: 999 })
  152. this.setOptions(res.data.list)
  153. },
  154. methods: {
  155. setAuthorityIds() {
  156. this.tableData && this.tableData.forEach((user) => {
  157. const authorityIds = user.authorities && user.authorities.map(i => {
  158. return i.authorityId
  159. })
  160. user.authorityIds = authorityIds
  161. })
  162. },
  163. openHeaderChange() {
  164. this.$refs.chooseImg.open()
  165. },
  166. setOptions(authData) {
  167. this.authOptions = []
  168. this.setAuthorityOptions(authData, this.authOptions)
  169. },
  170. setAuthorityOptions(AuthorityData, optionsData) {
  171. AuthorityData &&
  172. AuthorityData.map(item => {
  173. if (item.children && item.children.length) {
  174. const option = {
  175. authorityId: item.authorityId,
  176. authorityName: item.authorityName,
  177. children: []
  178. }
  179. this.setAuthorityOptions(item.children, option.children)
  180. optionsData.push(option)
  181. } else {
  182. const option = {
  183. authorityId: item.authorityId,
  184. authorityName: item.authorityName
  185. }
  186. optionsData.push(option)
  187. }
  188. })
  189. },
  190. async deleteUser(row) {
  191. const res = await deleteUser({ id: row.ID })
  192. if (res.code === 0) {
  193. this.$message.success('删除成功')
  194. await this.getTableData()
  195. this.setAuthorityIds()
  196. row.visible = false
  197. }
  198. },
  199. async enterAddUserDialog() {
  200. this.userInfo.authorityId = this.userInfo.authorityIds[0]
  201. this.$refs.userForm.validate(async valid => {
  202. if (valid) {
  203. const res = await register(this.userInfo)
  204. if (res.code === 0) {
  205. this.$message({ type: 'success', message: '创建成功' })
  206. }
  207. await this.getTableData()
  208. this.setAuthorityIds()
  209. this.closeAddUserDialog()
  210. }
  211. })
  212. },
  213. closeAddUserDialog() {
  214. this.$refs.userForm.resetFields()
  215. this.userInfo.headerImg = ''
  216. this.userInfo.authorityIds = []
  217. this.addUserDialog = false
  218. },
  219. addUser() {
  220. this.addUserDialog = true
  221. },
  222. async changeAuthority(row, flag) {
  223. if (flag) {
  224. return
  225. }
  226. this.$nextTick(async() => {
  227. const res = await setUserAuthorities({
  228. ID: row.ID,
  229. authorityIds: row.authorityIds
  230. })
  231. if (res.code === 0) {
  232. this.$message({ type: 'success', message: '角色设置成功' })
  233. }
  234. })
  235. }
  236. }
  237. }
  238. </script>
  239. <style lang="scss">
  240. .button-box {
  241. padding: 10px 20px;
  242. .el-button {
  243. float: right;
  244. }
  245. }
  246. .user-dialog {
  247. .header-img-box {
  248. width: 200px;
  249. height: 200px;
  250. border: 1px dashed #ccc;
  251. border-radius: 20px;
  252. text-align: center;
  253. line-height: 200px;
  254. cursor: pointer;
  255. }
  256. .avatar-uploader .el-upload:hover {
  257. border-color: #409eff;
  258. }
  259. .avatar-uploader-icon {
  260. border: 1px dashed #d9d9d9 !important;
  261. border-radius: 6px;
  262. font-size: 28px;
  263. color: #8c939d;
  264. width: 178px;
  265. height: 178px;
  266. line-height: 178px;
  267. text-align: center;
  268. }
  269. .avatar {
  270. width: 178px;
  271. height: 178px;
  272. display: block;
  273. }
  274. }
  275. </style>