index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import WxValidate from '../../utils/WxValidate.js';
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. formData: {
  8. type: Object,
  9. value: {}
  10. }
  11. },
  12. /**
  13. * 组件的初始数据
  14. */
  15. data: {
  16. form: {},
  17. formList: [],
  18. dateParams: {
  19. year: true,
  20. month: true,
  21. day: true,
  22. hour: false,
  23. minute: false,
  24. second: false
  25. },
  26. rules: {},
  27. time: "12:00",
  28. },
  29. attached() {
  30. let formList = this.data.formData.form_list || [];
  31. let form = {};
  32. let fileList = [];
  33. let rules = {};
  34. let messages = {};
  35. Object.keys(formList).length && Object.keys(formList).forEach((index) => {
  36. let item = formList[index];
  37. let formKey = item.type + '_' + item.random_code;
  38. form[formKey] = '';
  39. fileList.push([]);
  40. let message = "请填写正确的" + item.title;
  41. if(item.type=='image') {
  42. message = "请上传"+item.title;
  43. }
  44. if(item.type=='select') {
  45. item.index = -1;
  46. message = "请选择"+item.title;
  47. }
  48. if(item.type=='area') {
  49. item.region = [ item.province_id, item.city_id, item.country_id];
  50. form[formKey] = item.region.join(",");
  51. message = "请选择"+item.title;
  52. }
  53. if(item.type=='date') {
  54. if(item.date_type=="appoint_date") {
  55. item.default_time = item.appoint_date;
  56. form[formKey] = item.appoint_date;
  57. } else if(item.date_type=="same_day") {
  58. item.default_time = new Date().toLocaleDateString();
  59. form[formKey] = item.default_time;
  60. }
  61. message = "请选择"+item.title;
  62. }
  63. if(item.type=='date_range') {
  64. if(item.begin_date_type=="appoint_date") {
  65. item.default_begin_date = item.begin_appoint_date;
  66. } else if(item.begin_date_type=="same_day") {
  67. let cur_date = this.getDate();
  68. item.default_begin_date = cur_date;
  69. }
  70. if(item.end_date_type=="appoint_date") {
  71. item.default_end_date = item.end_appoint_date;
  72. } else if(item.end_date_type=="same_day") {
  73. let cur_date = this.getDate();
  74. item.default_end_date = cur_date;
  75. }
  76. if(item.default_begin_date&&item.default_end_date) {
  77. form[formKey] = item.default_begin_date+'~'+item.default_end_date;
  78. }
  79. item.pickerConfig = {
  80. endDate: true,
  81. column: "day",
  82. dateLimit: true,
  83. initStartTime: item.default_begin_date,
  84. initEndTime: item.default_end_date,
  85. limitStartTime: "2015-05-06",
  86. limitEndTime: "2065-05-06"
  87. }
  88. message = "请选择"+item.title;
  89. }
  90. if(item.type=='time') {
  91. let myDate = new Date();
  92. if(item.time_type=="appoint_time") {
  93. item.default_time = item.appoint_time;
  94. form[item.type+'_'+item.random_code] = item.appoint_time;
  95. } else if(item.time_type=="same_time") {
  96. item.default_time = myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
  97. form[item.type+'_'+item.random_code] = item.default_time;
  98. }
  99. message = "请选择"+item.title;
  100. }
  101. if(item.type=='time_range') {
  102. let myDate = new Date();
  103. item.beginTime = "";
  104. item.endTime = "";
  105. if(item.begin_time_type=="appoint_time") {
  106. item.beginTime = item.begin_appoint_time;
  107. } else if(item.begin_time_type=="same_time") {
  108. item.beginTime = myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
  109. }
  110. if(item.end_time_type=="appoint_time") {
  111. item.endTime = item.end_appoint_time;
  112. } else if(item.end_time_type=="same_time") {
  113. let timestamp = Date.parse(new Date());
  114. let nextDate = new Date(timestamp+1000);
  115. item.endTime = nextDate.getHours()+':'+nextDate.getMinutes()+':'+nextDate.getSeconds();
  116. }
  117. if(item.beginTime&&item.endTime) form[item.type+'_'+item.random_code] = item.beginTime+'~'+item.endTime;
  118. message = "请选择"+item.title;
  119. }
  120. //手机格式验证
  121. if(item.type=='telephone'&&item.required==1) {
  122. rules[formKey] = {
  123. required: Boolean(item.required),
  124. tel: true
  125. }
  126. messages[formKey] = {
  127. required: message,
  128. tel: message
  129. }
  130. } else if (item.type=='idcard'&&item.required==1) {
  131. rules[formKey] = {
  132. required: Boolean(item.required),
  133. idcard: true
  134. }
  135. messages[formKey] = {
  136. required: message,
  137. idcard: message
  138. }
  139. } else {
  140. rules[formKey] = {
  141. required: Boolean(item.required)
  142. }
  143. messages[formKey] = {
  144. required: message
  145. }
  146. }
  147. item.show = false;
  148. })
  149. this.setData({
  150. form,
  151. formList,
  152. fileList,
  153. formList,
  154. rules
  155. })
  156. console.log(rules)
  157. this.WxValidate = new WxValidate(rules, messages)
  158. },
  159. /**
  160. * 组件的方法列表
  161. */
  162. methods: {
  163. getDate() {
  164. let myDate = new Date();
  165. let y = myDate.getFullYear();
  166. let m = myDate.getMonth()+1;
  167. let d = myDate.getDate();
  168. m = m>10?m:'0'+m;
  169. d = d>10?d:'0'+d;
  170. return y+'-'+m+'-'+d;
  171. },
  172. changeImg: function (e) {
  173. // console.log(e.detail)
  174. let key = e.detail.key;
  175. let form = this.data.form;
  176. form[key] = e.detail.value.join(",");
  177. this.setData({
  178. form
  179. })
  180. },
  181. bindPickerChange(e) {
  182. // console.log(e)
  183. let idx = e.currentTarget.dataset.idx || "";
  184. let value = e.detail.value;
  185. let { form, formList } = this.data;
  186. let item = formList[idx];
  187. let key = item.type+'_'+item.random_code;
  188. item.index = value;
  189. formList[idx] = item;
  190. form[key] = item.option_val[value];
  191. this.setData({
  192. form,
  193. formList
  194. })
  195. },
  196. radioChange(e) {
  197. // console.log(e)
  198. let key = e.currentTarget.dataset.idx || "";
  199. let value = e.detail.value;
  200. let form = this.data.form;
  201. form[key] = value;
  202. // console.log(form)
  203. this.setData({
  204. form
  205. })
  206. },
  207. checkboxChange(e) {
  208. // console.log(e)
  209. let key = e.currentTarget.dataset.idx || "";
  210. let value = e.detail.value;
  211. let form = this.data.form;
  212. form[key] = value.join(",");
  213. // console.log(form)
  214. this.setData({
  215. form
  216. })
  217. },
  218. bindRegionChange(e) {
  219. // console.log(e)
  220. let idx = e.currentTarget.dataset.idx || "";
  221. let value = e.detail.value;
  222. let { form, formList } = this.data;
  223. let item = formList[idx];
  224. let key = item.type+'_'+item.random_code;
  225. item.region = value;
  226. formList[idx] = item;
  227. form[key] = value.join(",");
  228. this.setData({
  229. form,
  230. formList
  231. })
  232. },
  233. bindDateChange(e) {
  234. // console.log(e)
  235. let key = e.currentTarget.dataset.idx || "";
  236. let form = this.data.form;
  237. form[key] = e.detail.value;
  238. // console.log(form)
  239. this.setData({
  240. form
  241. })
  242. },
  243. bindKeyInput(e) {
  244. // console.log(e)
  245. let key = e.currentTarget.dataset.idx || "";
  246. let form = this.data.form;
  247. form[key] = e.detail.value;
  248. // console.log(form)
  249. this.setData({
  250. form
  251. })
  252. },
  253. showDateRange(e) {
  254. let idx = e.currentTarget.dataset.idx || "";
  255. let formList = this.data.formList;
  256. formList[idx].show = !formList[idx].show;
  257. this.setData({
  258. formList
  259. })
  260. },
  261. pickerHide(e) {
  262. // console.log(e)
  263. let idx = e.currentTarget.dataset.idx || "";
  264. let formList = this.data.formList;
  265. formList[idx].show = !formList[idx].show;
  266. this.setData({
  267. formList
  268. })
  269. },
  270. setPickerTime(e) {
  271. // console.log(e)
  272. let key = e.currentTarget.id || "";
  273. let form = this.data.form;
  274. let startTime = e.detail.startTime.slice(0, -9);
  275. let endTime = e.detail.endTime.slice(0, -9);
  276. form[key] = startTime + '~' + endTime;
  277. this.setData({
  278. form
  279. })
  280. },
  281. showTimeRange(e) {
  282. let idx = e.currentTarget.dataset.idx || "";
  283. let formList = this.data.formList;
  284. formList[idx].show = !formList[idx].show;
  285. this.setData({
  286. formList
  287. })
  288. },
  289. timeRangeConfirm(e) {
  290. // console.log(e)
  291. let idx = e.currentTarget.dataset.idx || "";
  292. let value = e.detail.time;
  293. let { form, formList } = this.data;
  294. let item = formList[idx];
  295. let key = item.type+'_'+item.random_code;
  296. item.show = false;
  297. formList[idx] = item;
  298. form[key] = value;
  299. this.setData({
  300. form,
  301. formList
  302. })
  303. },
  304. formSubmit(e) {
  305. const form = this.data.form
  306. console.log('form发生了submit事件,携带数据为:', form)
  307. //校验表单
  308. if (!this.WxValidate.checkForm(form)) {
  309. const error = this.WxValidate.errorList[0];
  310. wx.showToast({
  311. title: error.msg,
  312. icon: "none"
  313. })
  314. return false;
  315. } else {
  316. console.log('验证成功');
  317. let {formData, form, formList} = this.data;
  318. let allform_list = [];
  319. formList.forEach(item=>{
  320. let params = {
  321. title: item.title,
  322. type: item.type,
  323. item_value: form[item.type+'_'+item.random_code],
  324. random_code: item.random_code
  325. }
  326. allform_list.push(params);
  327. })
  328. console.log(allform_list);
  329. let formParams = {
  330. allform_id: formData.order_allform_id,
  331. allform_list: JSON.stringify(allform_list)
  332. };
  333. this.triggerEvent('success', formParams)
  334. }
  335. }
  336. }
  337. })