uni-id-users.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // 表单校验规则由 schema2code 生成,不建议直接修改校验规则,而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema
  2. const validator = {
  3. "username": {
  4. "rules": [{
  5. "required": true,
  6. "errorMessage": '请输入用户名',
  7. },
  8. {
  9. "minLength": 3,
  10. "maxLength": 32,
  11. "errorMessage": '用户名长度在 {minLength} 到 {maxLength} 个字符',
  12. },
  13. {
  14. validateFunction: function(rule, value, data, callback) {
  15. // console.log(value);
  16. if (/^1\d{10}$/.test(value) || /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(value)) {
  17. callback('用户名不能是:手机号或邮箱')
  18. };
  19. if (/^\d+$/.test(value)) {
  20. callback('用户名不能为纯数字')
  21. };
  22. if(/[\u4E00-\u9FA5\uF900-\uFA2D]{1,}/.test(value)){
  23. callback('用户名不能包含中文')
  24. }
  25. return true
  26. }
  27. }
  28. ],
  29. "label": "用户名"
  30. },
  31. "nickname": {
  32. "rules": [{
  33. minLength: 3,
  34. maxLength: 32,
  35. errorMessage: '昵称长度在 {minLength} 到 {maxLength} 个字符',
  36. },
  37. {
  38. validateFunction: function(rule, value, data, callback) {
  39. // console.log(value);
  40. if (/^1\d{10}$/.test(value) || /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(value)) {
  41. callback('昵称不能是:手机号或邮箱')
  42. };
  43. if (/^\d+$/.test(value)) {
  44. callback('昵称不能为纯数字')
  45. };
  46. // if(/[\u4E00-\u9FA5\uF900-\uFA2D]{1,}/.test(value)){
  47. // callback('昵称不能包含中文')
  48. // }
  49. return true
  50. }
  51. }
  52. ],
  53. "label": "昵称"
  54. },
  55. "password": {
  56. "rules": [{
  57. "required": true,
  58. },
  59. {
  60. "format": "password"
  61. },
  62. {
  63. "minLength": 6
  64. }
  65. ],
  66. "label": "密码"
  67. },
  68. "mobile": {
  69. "rules": [{
  70. "format": "string"
  71. },
  72. {
  73. "pattern": "^\\+?[0-9-]{3,20}$"
  74. }
  75. ],
  76. "label": "手机号码"
  77. },
  78. "status": {
  79. "rules": [{
  80. "format": "int"
  81. },
  82. {
  83. "range": [{
  84. "text": "正常",
  85. "value": 0
  86. },
  87. {
  88. "text": "禁用",
  89. "value": 1
  90. },
  91. {
  92. "text": "审核中",
  93. "value": 2
  94. },
  95. {
  96. "text": "审核拒绝",
  97. "value": 3
  98. }
  99. ]
  100. }
  101. ],
  102. "defaultValue": 0,
  103. "label": "用户状态"
  104. },
  105. "email": {
  106. "rules": [{
  107. "format": "string"
  108. },
  109. {
  110. "format": "email"
  111. }
  112. ],
  113. "label": "邮箱"
  114. },
  115. "role": {
  116. "rules": [{
  117. "format": "array"
  118. }],
  119. "label": "角色"
  120. },
  121. "last_login_date": {
  122. "rules": [{
  123. "format": "timestamp"
  124. }]
  125. }
  126. }
  127. const enumConverter = {
  128. "status_valuetotext": {
  129. "0": "正常",
  130. "1": "禁用",
  131. "2": "审核中",
  132. "3": "审核拒绝"
  133. }
  134. }
  135. function filterToWhere(filter, command) {
  136. let where = {}
  137. for (let field in filter) {
  138. let {
  139. type,
  140. value
  141. } = filter[field]
  142. switch (type) {
  143. case "search":
  144. if (typeof value === 'string' && value.length) {
  145. where[field] = new RegExp(value)
  146. }
  147. break;
  148. case "select":
  149. if (value.length) {
  150. let selectValue = []
  151. for (let s of value) {
  152. selectValue.push(command.eq(s))
  153. }
  154. where[field] = command.or(selectValue)
  155. }
  156. break;
  157. case "range":
  158. if (value.length) {
  159. let gt = value[0]
  160. let lt = value[1]
  161. where[field] = command.and([command.gte(gt), command.lte(lt)])
  162. }
  163. break;
  164. case "date":
  165. if (value.length) {
  166. let [s, e] = value
  167. let startDate = new Date(s)
  168. let endDate = new Date(e)
  169. where[field] = command.and([command.gte(startDate), command.lte(endDate)])
  170. }
  171. break;
  172. case "timestamp":
  173. if (value.length) {
  174. let [startDate, endDate] = value
  175. where[field] = command.and([command.gte(startDate), command.lte(endDate)])
  176. }
  177. break;
  178. }
  179. }
  180. return where
  181. }
  182. export {
  183. validator,
  184. enumConverter,
  185. filterToWhere
  186. }