utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import AsyncValidator from 'async-validator'
  2. const utils = {
  3. validate: (model, rules, callback, options) => {
  4. const initOptions = {
  5. showMessage: true
  6. }
  7. options = Object.assign({}, initOptions, options || {})
  8. // 如果需要验证的fields为空,调用验证时立刻返回callback
  9. if ((!rules || rules.length === 0) && callback) {
  10. callback(true, null);
  11. return true
  12. }
  13. let errors = []
  14. const props = Object.keys(rules)
  15. let count = 0
  16. for (let i in props) {
  17. const prop = props[i]
  18. const value = utils.getValueByProp(model, prop)
  19. utils.validateItem(rules, prop, value, (err) => {
  20. if (err && err.length > 0) {
  21. errors = errors.concat(err)
  22. }
  23. // 处理异步校验,等所有校验都结束时再callback
  24. count++
  25. if (count === props.length) {
  26. if (errors.length > 0) {
  27. if (options.showMessage) {
  28. utils.showToast(errors[0].message)
  29. }
  30. callback(false, errors)
  31. } else {
  32. callback(true, null)
  33. }
  34. }
  35. })
  36. }
  37. },
  38. validateField: (model, rules, props, callback, options) => {
  39. const initOptions = {
  40. showMessage: true
  41. }
  42. options = Object.assign({}, initOptions, options || {})
  43. props = [].concat(props)
  44. if (props.length === 0) {
  45. return
  46. }
  47. let errors = []
  48. let count = 0
  49. for (let i in props) {
  50. const prop = props[i]
  51. const value = utils.getValueByProp(model, prop)
  52. utils.validateItem(rules, prop, value, (err) => {
  53. if (err && err.length > 0) {
  54. errors = errors.concat(err)
  55. }
  56. // 处理异步校验,等所有校验都结束时再callback
  57. count++
  58. if (count === props.length) {
  59. if (errors.length > 0) {
  60. if (options.showMessage) {
  61. utils.showToast(errors[0].message)
  62. }
  63. callback(false, errors)
  64. } else {
  65. callback(true, null)
  66. }
  67. }
  68. })
  69. }
  70. },
  71. validateItem(rules, prop, value, callback) {
  72. if (!rules || JSON.stringify(rules) === '{}') {
  73. if (callback instanceof Function) {
  74. callback();
  75. }
  76. return true;
  77. }
  78. const propRules = [].concat(rules[prop] || []);
  79. propRules.forEach((rule) => {
  80. if (rule.pattern) {
  81. rule.pattern = new RegExp(rule.pattern)
  82. }
  83. })
  84. const descriptor = {
  85. [prop]: propRules
  86. };
  87. const validator = new AsyncValidator(descriptor);
  88. const model = {
  89. [prop]: value
  90. };
  91. validator.validate(model, {
  92. firstFields: true
  93. }, (errors) => {
  94. callback(errors);
  95. });
  96. },
  97. getValueByProp: (obj, prop) => {
  98. let tempObj = obj;
  99. prop = prop.replace(/\[(\w+)\]/g, '.$1').replace(/^\./, '');
  100. let keyArr = prop.split('.');
  101. let i = 0;
  102. for (let len = keyArr.length; i < len - 1; ++i) {
  103. if (!tempObj) break;
  104. let key = keyArr[i];
  105. if (key in tempObj) {
  106. tempObj = tempObj[key];
  107. } else {
  108. break;
  109. }
  110. }
  111. return tempObj ? (typeof tempObj[keyArr[i]] === 'string' ? tempObj[keyArr[i]].trim() : tempObj[keyArr[i]]) :
  112. null
  113. },
  114. showToast: (message) => {
  115. uni.showToast({
  116. title: message,
  117. icon: 'none'
  118. })
  119. }
  120. }
  121. export default utils