index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import WxValidate from '../../utils/wxValidate'
  2. const app = getApp()
  3. Page({
  4. data: {
  5. form: {},
  6. current: 1,
  7. // 用户类别
  8. typeList: [],
  9. },
  10. // 登陆注册监听
  11. click(e) {
  12. const that = this;
  13. let index = e.currentTarget.dataset.code;
  14. that.setData({ current: index })
  15. },
  16. // 选择用户类别
  17. typeChange: function (e) {
  18. const that = this;
  19. let index = e.detail.value;
  20. let data = that.data.typeList[index];
  21. if (data) that.setData({ 'form.type': data.value });
  22. that.setData({ 'form.type_name': data.label });
  23. },
  24. // 提交登录
  25. onSubmit: async function (e) {
  26. const params = e.detail.value;
  27. if (!this.WxValidate.checkForm(params)) {
  28. const error = this.WxValidate.errorList[0];
  29. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  30. return false
  31. } else {
  32. const res = await app.$api('user/login', 'POST', params);
  33. if (res.errcode === 0) {
  34. app.globalData.userInfo = res.data;//存用户信息到app.js
  35. wx.setStorage({ key: "user", data: res.data })// 存用户信息到storage,以便之后判断用户是否登录
  36. wx.showToast({ title: `账号登录成功`, icon: 'success', duration: 2000 }) //登录成功提示
  37. wx.navigateBack({ delta: 1 });
  38. } else wx.showToast({ title: res.errmsg, icon: 'none', duration: 2000 })
  39. }
  40. },
  41. // 提交注册
  42. reSubmit: async function (e) {
  43. const that = this;
  44. const params = e.detail.value;
  45. if (!this.WxValidate.checkForm(params)) {
  46. const error = this.WxValidate.errorList[0];
  47. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  48. return false
  49. } else {
  50. if (params.password !== params.is_password) {
  51. wx.showToast({ title: '密码输入不一致', duration: 2000, icon: 'error', })
  52. } else {
  53. params.openid = that.data.form.openid
  54. delete params.is_password
  55. const res = await app.$api('user', 'POST', params);
  56. if (res.errcode === 0) {
  57. wx.showToast({ title: `账号注册成功`, icon: 'success', duration: 2000 }) //登录成功提示
  58. that.setData({ current: 1, form: { openid: that.data.form.openid } })
  59. } else wx.showToast({ title: res.errmsg, icon: 'none', duration: 2000 })
  60. }
  61. }
  62. },
  63. // 忘记密码
  64. forgot() {
  65. wx.showToast({ title: `暂未开放`, icon: 'error', duration: 2000 })
  66. },
  67. /**
  68. * 生命周期函数--监听页面加载
  69. */
  70. async onLoad(options) {
  71. const that = this;
  72. wx.showLoading({ title: '加载中', mask: true })
  73. //验证规则函数
  74. await that.initValidate()
  75. await that.searchOther()
  76. await that.search()
  77. wx.hideLoading()
  78. },
  79. // 验证表单
  80. initValidate() {
  81. const rules = { account: { required: true }, password: { required: true, } }
  82. // 验证字段的提示信息,若不传则调用默认的信息
  83. const messages = { account: { required: '请输入账号', }, password: { required: '请输入密码', } };
  84. this.WxValidate = new WxValidate(rules, messages)
  85. },
  86. async searchOther() {
  87. const that = this;
  88. let res;
  89. // 类别
  90. res = await app.$api('dictData', 'GET', { type: 'type', is_use: '0' })
  91. if (res.errcode == '0') that.setData({ typeList: res.data })
  92. },
  93. // 查询通知
  94. async search() {
  95. const that = this;
  96. wx.getStorage({
  97. key: 'openid',
  98. async success(res) { that.setData({ 'form.openid': res.data }) },
  99. fail(err) {
  100. // console.log(err);
  101. }
  102. })
  103. },
  104. /**
  105. * 生命周期函数--监听页面初次渲染完成
  106. */
  107. onReady() {
  108. },
  109. /**
  110. * 生命周期函数--监听页面显示
  111. */
  112. onShow() { },
  113. /**
  114. * 生命周期函数--监听页面隐藏
  115. */
  116. onHide() {
  117. },
  118. /**
  119. * 生命周期函数--监听页面卸载
  120. */
  121. onUnload() {
  122. },
  123. /**
  124. * 页面相关事件处理函数--监听用户下拉动作
  125. */
  126. onPullDownRefresh() {
  127. },
  128. /**
  129. * 页面上拉触底事件的处理函数
  130. */
  131. onReachBottom() {
  132. },
  133. /**
  134. * 用户点击右上角分享
  135. */
  136. onShareAppMessage() {
  137. }
  138. })