index.js 4.6 KB

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