add.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate';
  3. const { gender } = require('../../utils/dict')
  4. Page({
  5. data: {
  6. frameStyle: { useTop: true, name: '学员信息管理', leftArrow: true, useBar: false },
  7. form: { icon: [] },
  8. // 性别
  9. genderList: gender,
  10. id: ''
  11. },
  12. initValidate() {
  13. const rules = { icon: { required: true }, name: { required: true }, card: { required: true, idcard: true }, age: { required: true }, gender: { required: true }, phone: { required: true, tel: true } }
  14. // 验证字段的提示信息,若不传则调用默认的信息
  15. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入学员姓名', }, card: { required: '请输入身份证号', }, age: { required: '请输入年龄', }, gender: { required: '请选择性别', }, phone: { required: '请输入手机号', } };
  16. this.WxValidate = new WxValidate(rules, messages)
  17. },
  18. // 返回
  19. back: function () {
  20. wx.navigateBack({ delta: 1 })
  21. },
  22. //上传图片
  23. imgUpl: function (e) {
  24. const that = this;
  25. let data = that.data.form.icon;
  26. data.push(e.detail)
  27. that.setData({ 'form.icon': data })
  28. },
  29. // 删除图片
  30. imgDel: function (e) {
  31. const that = this;
  32. let list = that.data.form.icon;
  33. let arr = list.filter((i, index) => index != e.detail.index)
  34. that.setData({ 'form.icon': arr })
  35. },
  36. //填写身份信息
  37. cardChange: function (e) {
  38. const that = this;
  39. let card = e.detail.value;
  40. if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card)) {
  41. wx.showToast({ title: `请输入18位的有效身份证`, icon: 'error', duration: 2000 })
  42. } else {
  43. //获取出生日期
  44. let birth = card.substring(6, 10) + "-" + card.substring(10, 12) + "-" + card.substring(12, 14);
  45. //获取性别
  46. if (parseInt(card.substr(16, 1)) % 2 == 1) that.setData({ 'form.gender': '0' });
  47. else that.setData({ 'form.gender': '1' });
  48. //获取年龄
  49. var myDate = new Date();
  50. var month = myDate.getMonth() + 1;
  51. var day = myDate.getDate();
  52. var age = myDate.getFullYear() - card.substring(6, 10) - 1;
  53. if (card.substring(10, 12) < month || card.substring(10, 12) == month && card.substring(12, 14) <= day) age++;
  54. that.setData({ 'form.age': age });
  55. }
  56. },
  57. // 选择性别
  58. genderChange: function (e) {
  59. const that = this;
  60. let data = that.data.genderList[e.detail.value];
  61. if (data) that.setData({ 'form.gender': data.value });
  62. },
  63. //提交
  64. onSubmit: async function (e) {
  65. const that = this;
  66. const form = that.data.form;
  67. const params = e.detail.value;
  68. params.icon = form.icon;
  69. if (!this.WxValidate.checkForm(params)) {
  70. const error = this.WxValidate.errorList[0];
  71. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  72. return false
  73. } else {
  74. console.log(params);
  75. // if (form._id) arr = await app.$post(`/newCourt/api/student/${form._id}`, params);
  76. // else arr = await app.$post(`/newCourt/api/student`, params);
  77. // if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  78. // else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  79. }
  80. },
  81. /**
  82. * 生命周期函数--监听页面加载
  83. */
  84. onLoad: function (options) {
  85. const that = this;
  86. that.setData({ id: options.id || '' })
  87. //验证规则函数
  88. that.initValidate();
  89. // 监听用户是否登录
  90. that.watchLogin();
  91. },
  92. // 监听用户是否登录
  93. watchLogin: async function () {
  94. const that = this;
  95. wx.getStorage({
  96. key: 'user',
  97. success: async res => {
  98. if (that.data.id) {
  99. const form = { _id: '1', icon: [{ url: '/image/beijing.jpeg' }], name: "测试姓名", card: '220581200003302462', phone: '12345678901', age: '18', gender: '0' };
  100. that.setData({ form: form });
  101. // const arr = await app.$get(`/newCourt/api/student/${that.data.id}`);
  102. // if (arr.errcode == '0') {
  103. // that.setData({ form: arr.data });
  104. // }
  105. }
  106. },
  107. fail: async res => {
  108. wx.redirectTo({ url: '/pages/index/index' })
  109. }
  110. })
  111. },
  112. /**
  113. * 生命周期函数--监听页面初次渲染完成
  114. */
  115. onReady: function () {
  116. },
  117. /**
  118. * 生命周期函数--监听页面显示
  119. */
  120. onShow: function () {
  121. },
  122. /**
  123. * 生命周期函数--监听页面隐藏
  124. */
  125. onHide: function () {
  126. },
  127. /**
  128. * 生命周期函数--监听页面卸载
  129. */
  130. onUnload: function () {
  131. },
  132. /**
  133. * 页面相关事件处理函数--监听用户下拉动作
  134. */
  135. onPullDownRefresh: function () {
  136. },
  137. /**
  138. * 页面上拉触底事件的处理函数
  139. */
  140. onReachBottom: function () {
  141. },
  142. /**
  143. * 用户点击右上角分享
  144. */
  145. onShareAppMessage: function () {
  146. }
  147. })