add.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const app = getApp()
  2. const { gender } = require('../../utils/dict')
  3. import WxValidate from '../../utils/wxValidate'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '教练信息', leftArrow: true, useBar: false },
  10. form: { icon: [] },
  11. // 性别
  12. genderList: gender,
  13. },
  14. initValidate() {
  15. const rules = { icon: { required: true }, name: { required: true }, card: { required: true }, gender: { required: true }, phone: { required: true, tel: true } }
  16. // 验证字段的提示信息,若不传则调用默认的信息
  17. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, card: { required: '请输入身份证号', }, gender: { required: '请选择性别', }, phone: { required: '请输入手机号', } };
  18. this.WxValidate = new WxValidate(rules, messages)
  19. },
  20. // 返回
  21. back: function () {
  22. wx.navigateBack({ delta: 1 })
  23. },
  24. imgUpl: function (e) {
  25. const that = this;
  26. let data = that.data.form.icon;
  27. data.push(e.detail)
  28. that.setData({ 'form.icon': data })
  29. },
  30. // 删除图片
  31. imgDel: function (e) {
  32. const that = this;
  33. let list = that.data.form.icon;
  34. let arr = list.filter((i, index) => index != e.detail.index)
  35. that.setData({ 'form.icon': arr })
  36. },
  37. // 选择性别
  38. genderChange: function (e) {
  39. const that = this;
  40. let data = that.data.genderList[e.detail.value];
  41. if (data) that.setData({ 'form.gender': data.value });
  42. },
  43. // 输入身份证号获取性别年龄
  44. toCard: function (e) {
  45. var IDCard = e.detail.value;
  46. var birth = IDCard.substring(6, 10) + "-" + IDCard.substring(10, 12) + "-" + IDCard.substring(12, 14);
  47. console.log('出生日期', birth);
  48. var sex = "";
  49. if (parseInt(IDCard.substr(16, 1)) % 2 == 1) {
  50. sex = "男";
  51. } else {
  52. sex = "女";
  53. }
  54. this.setData({ 'form.gender': sex })
  55. var myDate = new Date();
  56. var month = myDate.getMonth() + 1;
  57. var day = myDate.getDate();
  58. var age = myDate.getFullYear() - IDCard.substring(6, 10) - 1;
  59. if (IDCard.substring(10, 12) < month || IDCard.substring(10, 12) == month && IDCard.substring(12, 14) <= day) {
  60. age++;
  61. }
  62. this.setData({ 'form.age': age })
  63. },
  64. // 提交登录
  65. onSubmit: async function (e) {
  66. const that = this;
  67. const params = e.detail.value;
  68. const form = that.data.form;
  69. params.icon = form.icon;
  70. if (!this.WxValidate.checkForm(params)) {
  71. const error = this.WxValidate.errorList[0];
  72. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  73. return false
  74. } else {
  75. console.log('提交');
  76. }
  77. },
  78. /**
  79. * 生命周期函数--监听页面加载
  80. */
  81. onLoad: function (options) {
  82. const that = this;
  83. //验证规则函数
  84. that.initValidate();
  85. },
  86. /**
  87. * 生命周期函数--监听页面初次渲染完成
  88. */
  89. onReady: function () {
  90. },
  91. /**
  92. * 生命周期函数--监听页面显示
  93. */
  94. onShow: function () {
  95. },
  96. /**
  97. * 生命周期函数--监听页面隐藏
  98. */
  99. onHide: function () {
  100. },
  101. /**
  102. * 生命周期函数--监听页面卸载
  103. */
  104. onUnload: function () {
  105. },
  106. /**
  107. * 页面相关事件处理函数--监听用户下拉动作
  108. */
  109. onPullDownRefresh: function () {
  110. },
  111. /**
  112. * 页面上拉触底事件的处理函数
  113. */
  114. onReachBottom: function () {
  115. },
  116. /**
  117. * 用户点击右上角分享
  118. */
  119. onShareAppMessage: function () {
  120. }
  121. })