user.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. <CustomPic :picSrc="scope.row.headerImg" />
  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. <div style="display:inline-block" @click="openHeaderChange">
  65. <img class="header-img-box" v-if="userInfo.headerImg" :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. ></el-cascader>
  77. </el-form-item>
  78. </el-form>
  79. <div class="dialog-footer" slot="footer">
  80. <el-button @click="closeAddUserDialog">取 消</el-button>
  81. <el-button @click="enterAddUserDialog" type="primary">确 定</el-button>
  82. </div>
  83. </el-dialog>
  84. <ChooseImg ref="chooseImg" :target="userInfo" :targetKey="`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. mixins: [infoList],
  104. components: { CustomPic,ChooseImg },
  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: 6, message: "最低6位字符", 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. methods: {
  140. openHeaderChange(){
  141. this.$refs.chooseImg.open()
  142. },
  143. setOptions(authData) {
  144. this.authOptions = [];
  145. this.setAuthorityOptions(authData, this.authOptions);
  146. },
  147. setAuthorityOptions(AuthorityData, optionsData) {
  148. AuthorityData &&
  149. AuthorityData.map(item => {
  150. if (item.children && item.children.length) {
  151. const option = {
  152. authorityId: item.authorityId,
  153. authorityName: item.authorityName,
  154. children: []
  155. };
  156. this.setAuthorityOptions(item.children, option.children);
  157. optionsData.push(option);
  158. } else {
  159. const option = {
  160. authorityId: item.authorityId,
  161. authorityName: item.authorityName
  162. };
  163. optionsData.push(option);
  164. }
  165. });
  166. },
  167. async deleteUser(row) {
  168. const res = await deleteUser({ id: row.ID });
  169. if (res.code == 0) {
  170. this.getTableData();
  171. row.visible = false;
  172. }
  173. },
  174. async enterAddUserDialog() {
  175. this.$refs.userForm.validate(async valid => {
  176. if (valid) {
  177. const res = await register(this.userInfo);
  178. if (res.code == 0) {
  179. this.$message({ type: "success", message: "创建成功" });
  180. }
  181. await this.getTableData();
  182. this.closeAddUserDialog();
  183. }
  184. });
  185. },
  186. closeAddUserDialog() {
  187. this.$refs.userForm.resetFields();
  188. this.addUserDialog = false;
  189. },
  190. handleAvatarSuccess(res) {
  191. this.userInfo.headerImg = res.data.file.url;
  192. },
  193. addUser() {
  194. this.addUserDialog = true;
  195. },
  196. async changeAuthority(row) {
  197. const res = await setUserAuthority({
  198. uuid: row.uuid,
  199. authorityId: row.authority.authorityId
  200. });
  201. if (res.code == 0) {
  202. this.$message({ type: "success", message: "角色设置成功" });
  203. }
  204. }
  205. },
  206. async created() {
  207. this.getTableData();
  208. const res = await getAuthorityList({ page: 1, pageSize: 999 });
  209. this.setOptions(res.data.list);
  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>