index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. const verify = require('../../utils/Check');
  2. Component({
  3. externalClasses: [],
  4. properties: {
  5. form: {
  6. type: Array
  7. },
  8. showAll: {
  9. type: Boolean,
  10. value: true
  11. }, //不显示必填项
  12. onConfirm: {
  13. type: Function
  14. },
  15. interrupt: {
  16. type: Function
  17. }, //打断处理,用于条件判断 把form返回到上个页面处理重新传入
  18. },
  19. data: {
  20. temporary: null, //route选择暂存选中项
  21. },
  22. options: {
  23. multipleSlots: true //允许使用多个slot
  24. },
  25. methods: {
  26. route(e) {
  27. const {
  28. item
  29. } = e.currentTarget.dataset;
  30. getApp().globalData.handleSelect = this.handleRoute.bind(this);
  31. wx.navigateTo({
  32. url: item.url + '?params=' + JSON.stringify(item.params) + item.query
  33. })
  34. this.setData({
  35. temporary: {
  36. item: item,
  37. index: this.data.form.findIndex(v => v.valueName == item.valueName)
  38. }
  39. })
  40. },
  41. /* 处理路由返回结果 */
  42. handleRoute(data) {
  43. let temporary = this.data.temporary;
  44. if (temporary.item.interrupt) {
  45. this.triggerEvent("interrupt", {
  46. data,
  47. form: this.data.form,
  48. temporary
  49. });
  50. } else {
  51. wx.navigateBack();
  52. this.setData({
  53. [`form[${temporary.index}].value`]: data.value
  54. });
  55. this.confirm()
  56. }
  57. },
  58. /* 改变值 */
  59. inputChange(e) {
  60. let item = e.target.dataset.item,
  61. index = this.data.form.findIndex(v => v.valueName === item.valueName),
  62. value = e.detail;
  63. //开始校验 //校验规则 不填:不校验 "base":默认校验 "phone":手机号 "mail":邮箱 "正则表达式":以自定义内容为校验标准
  64. if (item.checking) {
  65. let reg = item.checking;
  66. switch (item.checking) {
  67. case 'base':
  68. value = verify.queryStr(value);
  69. break;
  70. case 'phone':
  71. reg = /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;
  72. this.setData({
  73. [`form[${index}].errMsg`]: !reg.test(value) ? '请输入正确11位手机号码' : ''
  74. });
  75. break;
  76. case 'mail':
  77. reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  78. this.setData({
  79. [`form[${index}].errMsg`]: !reg.test(value) ? '请输入正确的邮箱格式' : ''
  80. });
  81. break;
  82. default:
  83. reg = new RegExp(reg);
  84. this.setData({
  85. [`form[${index}].errMsg`]: !reg.test(value) ? item.hint || '输入文本不符合条件!' : ''
  86. });
  87. break;
  88. };
  89. };
  90. this.setData({
  91. [`form[${index}].value`]: value,
  92. [`form[${index}].error`]: false,
  93. });
  94. if (!item.required && value == '') this.setData({
  95. [`form[${index}].errMsg`]: "",
  96. });
  97. this.confirm();
  98. },
  99. /* 日期,时间 选择器 */
  100. bindDateChange(e) {
  101. let item = e.target.dataset.item,
  102. index = this.data.form.findIndex(v => v.valueName === item.valueName),
  103. value = e.detail.value;
  104. this.setData({
  105. [`form[${index}].value`]: value,
  106. [`form[${index}].error`]: false,
  107. });
  108. if (item.interrupt) this.triggerEvent("interrupt", {
  109. data: this.data.form[index],
  110. form: this.data.form
  111. });
  112. this.confirm();
  113. },
  114. /* 时间范围选择器 */
  115. rangeDateChange(e) {
  116. let item = e.target.dataset.item,
  117. i = e.target.dataset.index,
  118. index = this.data.form.findIndex(v => v.valueName === item.valueName),
  119. value = e.detail.value;
  120. this.setData({
  121. [`form[${index}].value[${i}]`]: value,
  122. [`form[${index}].error`]: false,
  123. });
  124. this.confirm();
  125. },
  126. /* 省市县 */
  127. bindRegionChange(e) {
  128. let item = e.currentTarget.dataset.item,
  129. index = this.data.form.findIndex(v => v.valueName === item.valueName),
  130. value = e.detail.value;
  131. this.setData({
  132. [`form[${index}].value`]: value,
  133. [`form[${index}].error`]: false,
  134. });
  135. this.confirm();
  136. },
  137. toOptions(e) {
  138. const {
  139. item
  140. } = e.currentTarget.dataset;
  141. wx.navigateTo({
  142. url: '/packageA/options/index?data=' + JSON.stringify(item),
  143. })
  144. },
  145. /* 自定义选项 */
  146. setOption(item) {
  147. let i = this.data.form.findIndex(v => v.valueName == item.valueName);
  148. this.setData({
  149. [`form[${i}].value`]: item.value
  150. });
  151. this.confirm();
  152. },
  153. /* 是否完成必填项 */
  154. confirm() {
  155. this.triggerEvent("onConfirm", this.data.form.some(v => {
  156. if (v.type == "dateRange" && v.required) {
  157. return v.value[0] == "" || v.value[1] == "";
  158. } else {
  159. return v.required && v.value === '';
  160. }
  161. }));
  162. this.setData({
  163. temporary: null
  164. })
  165. },
  166. /* 性别 */
  167. sexChange(e) {
  168. let item = e.currentTarget.dataset.item,
  169. index = this.data.form.findIndex(v => v.valueName === item.valueName);
  170. this.setData({
  171. [`form[${index}].value`]: e.detail,
  172. [`form[${index}].error`]: false,
  173. });
  174. this.confirm();
  175. },
  176. /* 单选 选择器改变 */
  177. radioChange(e) {
  178. let item = e.currentTarget.dataset.item,
  179. index = this.data.form.findIndex(v => v.valueName == item.valueName);
  180. this.setData({
  181. [`form[${index}].value`]: e.detail,
  182. [`form[${index}].error`]: false,
  183. });
  184. if (item.interrupt) this.triggerEvent("interrupt", {
  185. data: this.data.form[index],
  186. form: this.data.form
  187. });
  188. this.confirm();
  189. },
  190. /* 提交 */
  191. submit() {
  192. let obj = {},
  193. isPass = false;
  194. this.data.form.forEach((v, i) => {
  195. obj[v.valueName] = v.value;
  196. if (v.errMsg != '') {
  197. this.setData({
  198. [`form[${i}].error`]: true
  199. });
  200. isPass = true;
  201. }
  202. });
  203. if (isPass) {
  204. wx.showToast({
  205. title: '请检查表单内容',
  206. icon: "none"
  207. })
  208. } else {
  209. return obj;
  210. }
  211. },
  212. /* 查询结果 不验证是否必填 */
  213. query() {
  214. let obj = {};
  215. this.data.form.forEach(v => {
  216. obj[v.valueName] = v.value;
  217. });
  218. return obj;
  219. }
  220. }
  221. })