user-edit.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <!-- 用户编辑弹窗 -->
  2. <template>
  3. <ele-modal
  4. width="720px"
  5. :visible="visible"
  6. :append-to-body="true"
  7. :close-on-click-modal="true"
  8. custom-class="ele-dialog-form"
  9. :title="isUpdate ? '修改用户' : '添加用户'"
  10. @update:visible="updateVisible"
  11. >
  12. <el-form ref="form" :model="form" :rules="rules" label-width="100px">
  13. <el-row>
  14. <el-col :span="12">
  15. <el-form-item label="用户账号:" prop="loginName">
  16. <el-input
  17. clearable
  18. :maxlength="20"
  19. :disabled="isUpdate"
  20. v-model="form.loginName"
  21. placeholder="请输入用户账号"
  22. />
  23. </el-form-item>
  24. </el-col>
  25. <el-col :span="12">
  26. <el-form-item label="用户角色:" prop="roleId">
  27. <role-select v-model="form.roleId" />
  28. </el-form-item>
  29. </el-col>
  30. <el-col :span="12">
  31. <el-form-item v-if="!isUpdate" label="登录密码:" prop="loginPwd">
  32. <el-input
  33. show-password
  34. :maxlength="20"
  35. v-model="form.loginPwd"
  36. placeholder="请输入登录密码"
  37. />
  38. </el-form-item>
  39. </el-col>
  40. <el-col :span="12">
  41. <el-form-item label="启用状态:" prop="enable">
  42. <el-switch
  43. v-model="form.enable"
  44. active-color="#13ce66"
  45. inactive-color="#ff4949"
  46. :active-value="1"
  47. :inactive-value="0"
  48. >
  49. </el-switch>
  50. </el-form-item>
  51. </el-col>
  52. </el-row>
  53. </el-form>
  54. <template v-slot:footer>
  55. <el-button @click="updateVisible(false)">取消</el-button>
  56. <el-button type="primary" :loading="loading" @click="save">
  57. 保存
  58. </el-button>
  59. </template>
  60. </ele-modal>
  61. </template>
  62. <script>
  63. // import { emailReg, phoneReg } from 'ele-admin';
  64. import RoleSelect from './role-select.vue';
  65. import { addUsers, putUsers } from '@/api/system/user';
  66. export default {
  67. components: { RoleSelect },
  68. props: {
  69. // 弹窗是否打开
  70. visible: Boolean,
  71. // 修改回显的数据
  72. data: Object
  73. },
  74. data() {
  75. const defaultForm = {
  76. id: null,
  77. loginName: '',
  78. roleId: [],
  79. loginPwd: '',
  80. enable: 1
  81. };
  82. return {
  83. defaultForm,
  84. // 表单数据
  85. form: { ...defaultForm },
  86. // 表单验证规则
  87. rules: {
  88. loginName: [
  89. { required: true, message: '请输入用户账号', trigger: 'blur' }
  90. ],
  91. roleId: [
  92. { required: true, message: '请选择角色', trigger: 'change' }
  93. ],
  94. loginPwd: [
  95. {
  96. required: true,
  97. pattern: /^[\S]{5,18}$/,
  98. message: '密码必须为5-18位非空白字符',
  99. trigger: 'blur'
  100. }
  101. ]
  102. },
  103. // 提交状态
  104. loading: false,
  105. // 是否是修改
  106. isUpdate: false
  107. };
  108. },
  109. computed: {
  110. // 是否开启响应式布局
  111. styleResponsive() {
  112. return this.$store.state.theme.styleResponsive;
  113. }
  114. },
  115. methods: {
  116. /* 保存编辑 */
  117. save() {
  118. this.$refs.form.validate((valid) => {
  119. if (!valid) {
  120. return false;
  121. }
  122. this.loading = true;
  123. if (!this.isUpdate) {
  124. delete this.form.id;
  125. }
  126. const data = {
  127. ...this.form
  128. // roleId: this.form.roleIds.map((d) => {
  129. // return { roleId: d };
  130. // })
  131. };
  132. const saveOrUpdate = this.isUpdate ? putUsers : addUsers;
  133. saveOrUpdate(data)
  134. .then((msg) => {
  135. this.loading = false;
  136. this.$message.success(msg);
  137. this.updateVisible(false);
  138. this.$emit('done');
  139. })
  140. .catch((e) => {
  141. this.loading = false;
  142. // this.$message.error(e.message);
  143. });
  144. });
  145. },
  146. /* 更新visible */
  147. updateVisible(value) {
  148. this.$emit('update:visible', value);
  149. }
  150. },
  151. watch: {
  152. visible(visible) {
  153. if (visible) {
  154. if (this.data) {
  155. this.$util.assignObject(this.form, {
  156. ...this.data,
  157. roleId: this.data.roleList.map((d) => d.id),
  158. loginPwd: ''
  159. });
  160. this.isUpdate = true;
  161. } else {
  162. this.isUpdate = false;
  163. }
  164. } else {
  165. this.$refs.form.clearValidate();
  166. this.form = { ...this.defaultForm };
  167. }
  168. }
  169. }
  170. };
  171. </script>