addUser.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div id="addUser" v-loading="isLoging">
  3. <el-dialog
  4. title="添加用户"
  5. width="40%"
  6. top="2rem"
  7. :close-on-click-modal="false"
  8. :visible.sync="showDialog"
  9. :destroy-on-close="true"
  10. @close="close()"
  11. >
  12. <div id="shared" style="margin-right: 20px;">
  13. <el-form ref="passwordForm" :rules="rules" status-icon label-width="80px">
  14. <el-form-item label="用户名" prop="username">
  15. <el-input v-model="username" autocomplete="off"></el-input>
  16. </el-form-item>
  17. <el-form-item label="用户类型" prop="roleId" >
  18. <el-select v-model="roleId" placeholder="请选择" style="width: 100%">
  19. <el-option
  20. v-for="item in options"
  21. :key="item.id"
  22. :label="item.name"
  23. :value="item.id">
  24. </el-option>
  25. </el-select>
  26. </el-form-item>
  27. <el-form-item label="密码" prop="password">
  28. <el-input v-model="password" autocomplete="off"></el-input>
  29. </el-form-item>
  30. <el-form-item label="确认密码" prop="confirmPassword">
  31. <el-input v-model="confirmPassword" autocomplete="off"></el-input>
  32. </el-form-item>
  33. <el-form-item>
  34. <div style="float: right;">
  35. <el-button type="primary" @click="onSubmit">保存</el-button>
  36. <el-button @click="close">取消</el-button>
  37. </div>
  38. </el-form-item>
  39. </el-form>
  40. </div>
  41. </el-dialog>
  42. </div>
  43. </template>
  44. <script>
  45. export default {
  46. name: "addUser",
  47. props: {},
  48. computed: {},
  49. created() {
  50. this.getAllRole();
  51. },
  52. data() {
  53. let validatePass1 = (rule, value, callback) => {
  54. if (value === '') {
  55. callback(new Error('请输入新密码'));
  56. } else {
  57. if (this.confirmPassword !== '') {
  58. this.$refs.passwordForm.validateField('confirmPassword');
  59. }
  60. callback();
  61. }
  62. };
  63. let validatePass2 = (rule, value, callback) => {
  64. if (this.confirmPassword === '') {
  65. callback(new Error('请再次输入密码'));
  66. } else if (this.confirmPassword !== this.password) {
  67. callback(new Error('两次输入密码不一致!'));
  68. } else {
  69. callback();
  70. }
  71. };
  72. return {
  73. value:"",
  74. options: [],
  75. loading: false,
  76. username: null,
  77. password: null,
  78. roleId: null,
  79. confirmPassword: null,
  80. listChangeCallback: null,
  81. showDialog: false,
  82. isLoging: false,
  83. rules: {
  84. newPassword: [{required: true, validator: validatePass1, trigger: "blur"}, {
  85. pattern: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,.\/]).{8,20}$/,
  86. message: "密码长度在8-20位之间,由字母+数字+特殊字符组成",
  87. },],
  88. confirmPassword: [{required: true, validator: validatePass2, trigger: "blur"}],
  89. },
  90. };
  91. },
  92. methods: {
  93. openDialog: function (callback) {
  94. this.listChangeCallback = callback;
  95. this.showDialog = true;
  96. },
  97. onSubmit: function () {
  98. this.$axios({
  99. method: 'post',
  100. url: "/api/user/add",
  101. params: {
  102. username: this.username,
  103. password: this.password,
  104. roleId: this.roleId
  105. }
  106. }).then((res) => {
  107. if (res.data.code === 0) {
  108. this.$message({
  109. showClose: true,
  110. message: '添加成功',
  111. type: 'success',
  112. });
  113. this.showDialog = false;
  114. this.listChangeCallback()
  115. } else {
  116. this.$message({
  117. showClose: true,
  118. message: res.data.msg,
  119. type: 'error'
  120. });
  121. }
  122. }).catch((error) => {
  123. console.error(error)
  124. });
  125. },
  126. close: function () {
  127. this.showDialog = false;
  128. this.password = null;
  129. this.confirmPassword = null;
  130. this.username = null;
  131. this.roleId = null;
  132. },
  133. getAllRole:function () {
  134. this.$axios({
  135. method: 'get',
  136. url: "/api/role/all"
  137. }).then((res) => {
  138. this.loading = true;
  139. if (res.data.code === 0) {
  140. this.options=res.data.data
  141. }
  142. }).catch((error) => {
  143. console.error(error)
  144. });
  145. }
  146. },
  147. };
  148. </script>