user.vue 7.9 KB

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