edit.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div class="app-container menus-edit">
  3. <el-form ref="userInfoFormRef" label-width="100px" :model="userInfo" :rules="rules">
  4. <!-- <el-form-item label="用户头像:">
  5. <el-input v-show="false" v-model="user.avatar" />
  6. <upload-image tip="建议尺寸:60 * 60px" />
  7. </el-form-item> -->
  8. <el-form-item label="登录名:" prop="username">
  9. <el-input v-model="userInfo.username" placeholder="登录名" />
  10. </el-form-item>
  11. <el-form-item v-if="!userInfo.id" label="密码:" prop="password">
  12. <el-input v-model="userInfo.password" placeholder="密码" />
  13. </el-form-item>
  14. <el-form-item label="联系人:">
  15. <el-input v-model="userInfo.linkMan" placeholder="联系人" />
  16. </el-form-item>
  17. <el-form-item label="手机号:" prop="mobile">
  18. <el-input v-model="userInfo.mobile" placeholder="手机号" />
  19. </el-form-item>
  20. <el-form-item label="状态:">
  21. <el-radio-group v-model="userInfo.status">
  22. <el-radio :label="1">启用</el-radio>
  23. <el-radio :label="0">停用</el-radio>
  24. </el-radio-group>
  25. </el-form-item>
  26. <el-form-item label="用户角色:" prop="roleList">
  27. <el-checkbox-group v-model="userInfo.roleList">
  28. <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.roleName }}</el-checkbox>
  29. </el-checkbox-group>
  30. </el-form-item>
  31. <el-form-item>
  32. <!-- 确认 取消 -->
  33. <div class="control-box">
  34. <el-button type="warning" @click="navigateBack">返回</el-button>
  35. <el-button type="primary" @click="onSubmit">保存</el-button>
  36. </div>
  37. </el-form-item>
  38. </el-form>
  39. </div>
  40. </template>
  41. <script>
  42. import { fetchRoleList, createUser, getUser, updateUser } from '@/api/system'
  43. import { isMobile } from '@/utils/validate'
  44. export default {
  45. data() {
  46. // 校验角色
  47. const validateRoleList = (rule, value, callback) => {
  48. if (value instanceof Array) {
  49. if (value.length === 0) {
  50. callback(new Error('请至少选择一个角色'))
  51. } else {
  52. callback()
  53. }
  54. } else {
  55. callback(new Error('角色列表为一个数组'))
  56. }
  57. }
  58. // 校验手机号
  59. const validateMobile = (rule, value, callback) => {
  60. if (value && isMobile(value)) {
  61. callback()
  62. } else {
  63. callback(new Error('请输入合法的手机号'))
  64. }
  65. }
  66. return {
  67. editType: 'add',
  68. userInfo: {
  69. id: 0,
  70. username: '',
  71. password: '',
  72. linkMan: '',
  73. mobile: '',
  74. status: 1,
  75. roleList: [],
  76. roleIds: ''
  77. },
  78. roleList: [],
  79. rules: {
  80. username: [
  81. { required: true, message: '请输入登录名', trigger: 'blur' },
  82. { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
  83. ],
  84. mobile: [{ validator: validateMobile, trigger: 'blur' }],
  85. password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  86. roleList: [{ required: true, validator: validateRoleList, trigger: 'change' }]
  87. }
  88. }
  89. },
  90. created() {
  91. this.editType = this.$route.query.type
  92. this.fetchRoleList()
  93. if (this.editType === 'edit') {
  94. this.userInfo.id = this.$route.query.id
  95. this.fetchUserInfo()
  96. }
  97. },
  98. methods: {
  99. // 获取角色列表
  100. fetchRoleList() {
  101. fetchRoleList().then(res => {
  102. this.roleList = [...this.roleList, ...res.data.list]
  103. })
  104. },
  105. // 修改保存用户信息
  106. updateUserInfo() {
  107. this.userInfo.roleIds = this.userInfo.roleList.join(',')
  108. updateUser(this.userInfo.id, this.userInfo).then(res => {
  109. this.$message.success('修改用户成功')
  110. this.$store.dispatch('tagsView/delView', this.$route)
  111. this.$router.back()
  112. })
  113. },
  114. // 保存用户信息
  115. createUserInfo() {
  116. this.userInfo.roleIds = this.userInfo.roleList.join(',')
  117. createUser(this.userInfo).then(res => {
  118. this.$message.success('添加用户成功')
  119. this.$store.dispatch('tagsView/delView', this.$route)
  120. this.$router.back()
  121. })
  122. },
  123. // 提交表单
  124. onSubmit() {
  125. this.$refs.userInfoFormRef.validate(valide => {
  126. if (!valide) return
  127. if (this.editType === 'add') {
  128. this.createUserInfo()
  129. } else {
  130. this.updateUserInfo()
  131. }
  132. })
  133. },
  134. // 获取用户信息
  135. fetchUserInfo() {
  136. getUser(this.userInfo.id).then(res => {
  137. this.setUserInfo(res.data)
  138. })
  139. },
  140. // 设置用户信息
  141. setUserInfo(data) {
  142. console.log(data)
  143. for (const key in this.userInfo) {
  144. if (Object.hasOwnProperty.call(data, key)) {
  145. this.userInfo[key] = data[key]
  146. }
  147. }
  148. this.userInfo.roleList = data.roleIds.split(',').map(item => parseInt(item))
  149. }
  150. }
  151. }
  152. </script>
  153. <style scoped lang="scss">
  154. .menus-edit {
  155. width: 600px;
  156. margin: 0 auto;
  157. margin-top: 100px;
  158. }
  159. </style>