add.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <view style="padding: 20rpx;">
  3. <view style="padding: 30rpx;background-color: white;border-radius:50rpx">
  4. <u--form labelPosition="left" :model="userInfo" ref="uForm" labelWidth='120'>
  5. <u-form-item label="用户账号:" prop="account" borderBottom ref="uItem" :required="true">
  6. <u--input v-model="userInfo.account" clearable border="none" placeholder="请输入用户账号"></u--input>
  7. </u-form-item>
  8. <u-form-item label="用户密码:" prop="password" borderBottom ref="uItem" :required="true">
  9. <u--input v-model="userInfo.password" clearable password border="none" placeholder="请输入用户密码">
  10. </u--input>
  11. </u-form-item>
  12. <u-form-item label="用户名称:" prop="name" borderBottom ref="uItem" :required="true">
  13. <u--input v-model="userInfo.name" border="none" placeholder="请输入用户名称"></u--input>
  14. </u-form-item>
  15. <u-form-item label="联系电话:" prop="phone" borderBottom ref="uItem">
  16. <u-input v-model="userInfo.phone" border="none" placeholder="请输入联系电话"></u-input>
  17. </u-form-item>
  18. <u-form-item label="是否管理员:" prop="adminType" borderBottom ref="uItem">
  19. <u-switch v-model="userInfo.adminType"></u-switch>
  20. </u-form-item>
  21. <u-tag text="默认密码:123456" style="margin-left:auto;width: 40%;"></u-tag>
  22. <u-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>
  23. <u-button type="error" text="重置" customStyle="margin-top: 10px" @click="reset"></u-button>
  24. </u--form>
  25. </view>
  26. <u-toast ref="uToast"></u-toast>
  27. </view>
  28. </template>
  29. <script>
  30. import {
  31. add,
  32. checkUserAccountExist
  33. } from '@/api/system/user.js'
  34. export default {
  35. data() {
  36. return {
  37. userInfo: {
  38. account: null,
  39. password: '123456',
  40. name: null,
  41. phone: null,
  42. adminType: false
  43. },
  44. rules: {
  45. 'account': [{
  46. type: 'string',
  47. required: true,
  48. message: '请填写用户账号',
  49. trigger: ['blur', 'change']
  50. }, {
  51. asyncValidator: (rule, value, callback) => {
  52. checkUserAccountExist(value).then(res => {
  53. if (res.success && res.data) {
  54. // 如果校验通过,也要执行callback()回调
  55. callback();
  56. } else {
  57. // 如果验证不通过,需要在callback()抛出new Error('错误提示信息')
  58. callback(true);
  59. }
  60. })
  61. },
  62. message: '用户名已存在',
  63. // 触发器可以同时用blur和change,二者之间用英文逗号隔开
  64. trigger: ["blur"],
  65. }],
  66. 'password': {
  67. type: 'string',
  68. required: true,
  69. message: '请填写用户密码',
  70. trigger: ['blur', 'change']
  71. },
  72. 'name': {
  73. type: 'string',
  74. required: true,
  75. message: '请填写用户名称',
  76. trigger: ['blur', 'change']
  77. },
  78. }
  79. }
  80. },
  81. methods: {
  82. submit() {
  83. this.$refs.uForm.validate().then(val => {
  84. add(this.userInfo).then(res => {
  85. if (res.success) {
  86. this.$refs.uToast.show({
  87. type: 'success',
  88. message: "新增成功",
  89. iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
  90. })
  91. this.reset()
  92. } else {
  93. this.$refs.uToast.show({
  94. type: 'error',
  95. message: res.message,
  96. iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
  97. })
  98. }
  99. })
  100. }).catch(errors => {
  101. console.log(errors)
  102. this.$refs.uToast.show({
  103. type: 'error',
  104. message: "校验失败",
  105. iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
  106. })
  107. })
  108. },
  109. reset() {
  110. this.userInfo = {
  111. account: null,
  112. password: '123456',
  113. name: null,
  114. phone: null,
  115. adminType: false
  116. }
  117. const validateList = ['userInfo.account', 'userInfo.name']
  118. setTimeout(() => {
  119. this.$refs.uForm.clearValidate()
  120. }, 100)
  121. },
  122. checkUserAccount(value) {
  123. checkUserAccountExist(value).then(res => {
  124. if (res.success) {
  125. return res.data
  126. }
  127. })
  128. }
  129. },
  130. onReady() {
  131. this.$refs.uForm.setRules(this.rules)
  132. }
  133. }
  134. </script>
  135. <style>
  136. </style>