user.vue 8.8 KB

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