index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: false, useBar: false },
  7. form: { icon: [] },
  8. // 性别
  9. genderList: gender,
  10. agree: false
  11. },
  12. initValidate() {
  13. const rules = { icon: { required: true }, name: { required: true }, gender: { required: true }, phone: { required: true, tel: true } }
  14. // 验证字段的提示信息,若不传则调用默认的信息
  15. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, gender: { required: '请选择性别', }, phone: { required: '请输入手机号', } };
  16. this.WxValidate = new WxValidate(rules, messages)
  17. },
  18. imgUpl: function (e) {
  19. const that = this;
  20. let data = that.data.form.icon;
  21. data.push(e.detail)
  22. that.setData({ 'form.icon': data })
  23. },
  24. // 删除图片
  25. imgDel: function (e) {
  26. const that = this;
  27. let list = that.data.form.icon;
  28. let arr = list.filter((i, index) => index != e.detail.index)
  29. that.setData({ 'form.icon': arr })
  30. },
  31. // 选择性别
  32. genderChange: function (e) {
  33. const that = this;
  34. let data = that.data.genderList[e.detail.value];
  35. if (data) that.setData({ 'form.gender': data.value });
  36. },
  37. // 公共跳转
  38. toCommon: function (e) {
  39. // 登录
  40. const { route } = e.currentTarget.dataset;
  41. if (route) wx.navigateTo({ url: `/pages/${route}` });
  42. },
  43. // 同意
  44. changeAgree: function () {
  45. const that = this;
  46. if (that.data.agree) that.setData({ agree: false })
  47. else that.setData({ agree: true })
  48. },
  49. // 注册
  50. toRegister: async function (e) {
  51. const that = this;
  52. const form = that.data.form;
  53. const params = e.detail.value;
  54. params.icon = form.icon;
  55. if (!this.WxValidate.checkForm(params)) {
  56. const error = this.WxValidate.errorList[0];
  57. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  58. return false
  59. } else {
  60. if (that.data.agree) {
  61. const wxInfo = app.globalData.wxInfo;
  62. if (!wxInfo.openid) { wx.showToast({ title: '缺少微信关联', icon: 'error' }); return }
  63. params.openid = wxInfo.openid;
  64. const arr = await app.$post('/user', params)
  65. if (arr.errcode == '0') {
  66. wx.showToast({ title: `注册成功`, icon: 'fail', duration: 2000 });
  67. wx.redirectTo({ url: '/pages/index/index' });
  68. } else {
  69. wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  70. }
  71. } else { wx.showToast({ title: `请同意用户协议`, icon: 'fail', duration: 2000 }); }
  72. }
  73. },
  74. /**
  75. * 生命周期函数--监听页面加载
  76. */
  77. onLoad: function (options) {
  78. const that = this;
  79. //验证规则函数
  80. that.initValidate();
  81. },
  82. /**
  83. * 生命周期函数--监听页面初次渲染完成
  84. */
  85. onReady: function () {
  86. },
  87. /**
  88. * 生命周期函数--监听页面显示
  89. */
  90. onShow: function () {
  91. },
  92. /**
  93. * 生命周期函数--监听页面隐藏
  94. */
  95. onHide: function () {
  96. },
  97. /**
  98. * 生命周期函数--监听页面卸载
  99. */
  100. onUnload: function () {
  101. },
  102. /**
  103. * 页面相关事件处理函数--监听用户下拉动作
  104. */
  105. onPullDownRefresh: function () {
  106. },
  107. /**
  108. * 页面上拉触底事件的处理函数
  109. */
  110. onReachBottom: function () {
  111. },
  112. /**
  113. * 用户点击右上角分享
  114. */
  115. onShareAppMessage: function () {
  116. }
  117. })