validator.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { dateUtil } from '/@/utils/dateUtil';
  2. import { duplicateCheck } from '/@/views/system/user/user.api';
  3. export const rules = {
  4. rule(type, required) {
  5. if (type === 'email') {
  6. return this.email(required);
  7. }
  8. if (type === 'phone') {
  9. return this.phone(required);
  10. }
  11. },
  12. email(required) {
  13. return [
  14. {
  15. required: required ? required : false,
  16. validator: async (_rule, value) => {
  17. if (required == true && !value) {
  18. return Promise.reject('请输入邮箱!');
  19. }
  20. if (
  21. value &&
  22. !new RegExp(
  23. /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  24. ).test(value)
  25. ) {
  26. return Promise.reject('请输入正确邮箱格式!');
  27. }
  28. return Promise.resolve();
  29. },
  30. trigger: 'change',
  31. },
  32. ] as ArrayRule;
  33. },
  34. phone(required) {
  35. return [
  36. {
  37. required: required,
  38. validator: async (_, value) => {
  39. if (required && !value) {
  40. return Promise.reject('请输入手机号码1!');
  41. }
  42. if (!new RegExp(/^1[3|4|5|7|8|9][0-9]\d{8}$/).test(value)) {
  43. return Promise.reject('手机号码格式有误');
  44. }
  45. return Promise.resolve();
  46. },
  47. trigger: 'change',
  48. },
  49. ];
  50. },
  51. startTime(endTime, required) {
  52. return [
  53. {
  54. required: required ? required : false,
  55. validator: (_, value) => {
  56. if (required && !value) {
  57. return Promise.reject('请选择开始时间');
  58. }
  59. if (endTime && value && dateUtil(endTime).isBefore(value)) {
  60. return Promise.reject('开始时间需小于结束时间');
  61. }
  62. return Promise.resolve();
  63. },
  64. trigger: 'change',
  65. },
  66. ];
  67. },
  68. endTime(startTime, required) {
  69. return [
  70. {
  71. required: required ? required : false,
  72. validator: (_, value) => {
  73. if (required && !value) {
  74. return Promise.reject('请选择结束时间');
  75. }
  76. if (startTime && value && dateUtil(value).isBefore(startTime)) {
  77. return Promise.reject('结束时间需大于开始时间');
  78. }
  79. return Promise.resolve();
  80. },
  81. trigger: 'change',
  82. },
  83. ];
  84. },
  85. confirmPassword(values, required) {
  86. return [
  87. {
  88. required: required ? required : false,
  89. validator: (_, value) => {
  90. if (!value) {
  91. return Promise.reject('密码不能为空');
  92. }
  93. if (value !== values.password) {
  94. return Promise.reject('两次输入的密码不一致!');
  95. }
  96. return Promise.resolve();
  97. },
  98. },
  99. ];
  100. },
  101. duplicateCheckRule(tableName, fieldName, model, schema, required?) {
  102. return [
  103. {
  104. validator: (_, value) => {
  105. if (!value && required) {
  106. return Promise.reject(`请输入${schema.label}`);
  107. }
  108. return new Promise<void>((resolve, reject) => {
  109. duplicateCheck({
  110. tableName,
  111. fieldName,
  112. fieldVal: value,
  113. dataId: model.id,
  114. })
  115. .then((res) => {
  116. res.success ? resolve() : reject(res.message || '校验失败');
  117. })
  118. .catch((err) => {
  119. reject(err.message || '验证失败');
  120. });
  121. });
  122. },
  123. },
  124. ] as ArrayRule;
  125. },
  126. };
  127. //update-begin-author:taoyan date:2022-6-16 for: 代码生成-原生表单用
  128. /**
  129. * 唯一校验函数,给原生<a-form>使用,vben的表单校验建议使用上述rules
  130. * @param tableName 表名
  131. * @param fieldName 字段名
  132. * @param fieldVal 字段值
  133. * @param dataId 数据ID
  134. */
  135. export async function duplicateValidate(tableName, fieldName, fieldVal, dataId) {
  136. try {
  137. let params = {
  138. tableName,
  139. fieldName,
  140. fieldVal,
  141. dataId: dataId,
  142. };
  143. const res = await duplicateCheck(params);
  144. if (res.success) {
  145. return Promise.resolve();
  146. } else {
  147. return Promise.reject(res.message || '校验失败');
  148. }
  149. } catch (e) {
  150. return Promise.reject('校验失败,可能是断网等问题导致的校验失败');
  151. }
  152. }
  153. //update-end-author:taoyan date:2022-6-16 for: 代码生成-原生表单用