index.js 4.7 KB

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