index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // pages/login/login.js
  2. import WxValidate from '../../utils/wxValidate'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: false, name: '', leftArrow: false, useBar: false },
  10. // 主体高度
  11. infoHeight: '',
  12. // 使用协议
  13. agreement: false
  14. },
  15. initValidate() {
  16. const rules = { phone: { required: true, tel: true }, password: { required: true, } }
  17. // 验证字段的提示信息,若不传则调用默认的信息
  18. const messages = { phone: { required: '请输入手机号', }, password: { required: '请输入密码', } };
  19. this.WxValidate = new WxValidate(rules, messages)
  20. },
  21. back: function () {
  22. wx.navigateBack({ url: '/pages/home/index' })
  23. },
  24. // 协议选择
  25. agreementChange: function (e) {
  26. const that = this;
  27. let value = e.detail.value;
  28. if (value.length > 0) that.setData({ agreement: true })
  29. else that.setData({ agreement: false })
  30. },
  31. // 协议查看
  32. agreementView: function () {
  33. wx.navigateTo({ url: '/pages/login/agreement' })
  34. },
  35. // 提交登录
  36. onSubmit: function (e) {
  37. const params = e.detail.value;
  38. if (!this.WxValidate.checkForm(params)) {
  39. const error = this.WxValidate.errorList[0];
  40. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  41. return false
  42. } else {
  43. if (this.data.agreement) {
  44. wx.request({
  45. url: app.globalData.publicUrl + `/api/hc/user/login`,
  46. method: "post",
  47. data: { ...params },
  48. header: {},
  49. success: (res) => {
  50. if (res.data.errcode == '0') {
  51. wx.setStorage({ key: "user", data: res.data.data });
  52. app.globalData.userInfo = res.data.data;
  53. wx.showToast({ title: `账号登录成功`, icon: 'success', duration: 2000 })
  54. wx.navigateTo({ url: '/pages/home/index' })
  55. } else {
  56. wx.showToast({ title: res.data.errmsg, icon: 'error' })
  57. }
  58. },
  59. fail: (res) => { },
  60. complete: (res) => { },
  61. })
  62. } else {
  63. wx.showToast({ title: `请同意使用协议`, icon: 'error', duration: 1000 })
  64. }
  65. }
  66. },
  67. // 微信登录
  68. wxLogin: function () {
  69. wx.showToast({ title: `暂未开放`, icon: 'error', duration: 2000 })
  70. // wx.navigateTo({ url: '/pages/home/index' })
  71. },
  72. /**
  73. * 生命周期函数--监听页面加载
  74. */
  75. onLoad: function (options) {
  76. //验证规则函数
  77. this.initValidate()
  78. // 计算高度
  79. this.searchHeight()
  80. },
  81. // 计算高度
  82. searchHeight: function () {
  83. let frameStyle = this.data.frameStyle;
  84. let client = app.globalData.client;
  85. let infoHeight = client.windowHeight;
  86. // 是否去掉状态栏
  87. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  88. // 是否减去底部菜单
  89. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  90. if (infoHeight) this.setData({ infoHeight: infoHeight })
  91. },
  92. /**
  93. * 生命周期函数--监听页面初次渲染完成
  94. */
  95. onReady: function () {
  96. },
  97. /**
  98. * 生命周期函数--监听页面显示
  99. */
  100. onShow: function () {
  101. },
  102. /**
  103. * 生命周期函数--监听页面隐藏
  104. */
  105. onHide: function () {
  106. },
  107. /**
  108. * 生命周期函数--监听页面卸载
  109. */
  110. onUnload: function () {
  111. },
  112. /**
  113. * 页面相关事件处理函数--监听用户下拉动作
  114. */
  115. onPullDownRefresh: function () {
  116. },
  117. /**
  118. * 页面上拉触底事件的处理函数
  119. */
  120. onReachBottom: function () {
  121. },
  122. /**
  123. * 用户点击右上角分享
  124. */
  125. onShareAppMessage: function () {
  126. }
  127. })