user.vue 7.9 KB

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