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. initValidate() {
  18. const rules = { account: { required: true }, password: { required: true, } }
  19. // 验证字段的提示信息,若不传则调用默认的信息
  20. const messages = { account: { required: '请输入账号', }, password: { required: '请输入密码', } };
  21. this.WxValidate = new WxValidate(rules, messages)
  22. },
  23. // 选择用户类别
  24. typeChange: function (e) {
  25. const that = this;
  26. let index = e.detail.value;
  27. let data = that.data.typeList[index];
  28. if (data) that.setData({ 'form.type': data.value });
  29. that.setData({ 'form.type_name': data.label });
  30. },
  31. // 提交登录
  32. onSubmit: async function (e) {
  33. const params = e.detail.value;
  34. if (!this.WxValidate.checkForm(params)) {
  35. const error = this.WxValidate.errorList[0];
  36. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  37. return false
  38. } else {
  39. params.openid = that.data.form.openid
  40. const res = await app.$api('user/login', 'POST', params);
  41. if (res.errcode === 0) {
  42. app.globalData.userInfo = res.data;//存用户信息到app.js
  43. wx.setStorage({ key: "token", data: res.data })// 存用户信息到storage,以便之后判断用户是否登录
  44. wx.showToast({ title: `账号登录成功`, icon: 'success', duration: 2000 }) //登录成功提示
  45. wx.redirectTo({ url: '/pagesHome/home/index' })
  46. } else {
  47. wx.showToast({ title: res.errmsg, icon: 'none', duration: 2000 })
  48. }
  49. }
  50. },
  51. // 提交注册
  52. reSubmit: async function (e) {
  53. const that = this;
  54. const params = e.detail.value;
  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 (params.password !== params.is_password) {
  61. wx.showToast({ title: '密码输入不一致', duration: 2000, icon: 'error', })
  62. } else {
  63. params.openid = that.data.form.openid
  64. delete params.is_password
  65. const res = await app.$api('user', 'POST', params);
  66. if (res.errcode === 0) {
  67. wx.showToast({ title: `账号注册成功`, icon: 'success', duration: 2000 }) //登录成功提示
  68. that.setData({ current: 1 })
  69. } else {
  70. wx.showToast({ title: res.errmsg, icon: 'none', duration: 2000 })
  71. }
  72. }
  73. }
  74. },
  75. // 忘记密码
  76. forgot() {
  77. wx.showToast({ title: `暂未开放`, icon: 'error', duration: 2000 })
  78. },
  79. /**
  80. * 生命周期函数--监听页面加载
  81. */
  82. async onLoad(options) {
  83. const that = this;
  84. wx.showLoading({ title: '加载中', mask: true })
  85. //验证规则函数
  86. await that.initValidate()
  87. await that.searchOther()
  88. await that.search()
  89. wx.hideLoading()
  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. })