add.js 5.2 KB

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