index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. checkboxChange(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. onSubmit: 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. if (this.data.agreement) {
  40. wx.request({
  41. url: app.globalData.publicUrl + `/api/hc/user/login`,
  42. method: "post",
  43. data: { ...params },
  44. header: {},
  45. success: (res) => {
  46. if (res.data.errcode == '0') {
  47. wx.setStorage({ key: "user", data: res.data.data });
  48. app.globalData.userInfo = res.data.data;
  49. wx.showToast({ title: `账号登录成功`, icon: 'success', duration: 2000 })
  50. wx.navigateTo({ url: '/pages/home/index' })
  51. } else {
  52. wx.showToast({ title: res.data.errmsg, icon: 'error' })
  53. }
  54. },
  55. fail: (res) => { },
  56. complete: (res) => { },
  57. })
  58. } else {
  59. wx.showToast({ title: `请同意使用协议`, icon: 'error', duration: 1000 })
  60. }
  61. }
  62. },
  63. //微信登录
  64. wxlogin: function () {
  65. wx.showToast({ title: `暂未开放`, icon: 'error', duration: 2000 })
  66. },
  67. //注册
  68. register: function () {
  69. wx.showToast({ title: `暂未开放`, icon: 'error', duration: 2000 })
  70. },
  71. //忘记密码
  72. noopen: function () {
  73. wx.showToast({ title: `暂未开放`, icon: 'error', duration: 2000 })
  74. },
  75. /**
  76. * 生命周期函数--监听页面加载
  77. */
  78. onLoad: function (options) {
  79. //验证规则函数
  80. this.initValidate()
  81. // 计算高度
  82. this.searchHeight()
  83. },
  84. // 计算高度
  85. searchHeight: function () {
  86. let frameStyle = this.data.frameStyle;
  87. let client = app.globalData.client;
  88. let infoHeight = client.windowHeight;
  89. // 是否去掉状态栏
  90. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  91. // 是否减去底部菜单
  92. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  93. if (infoHeight) this.setData({ infoHeight: infoHeight })
  94. },
  95. /**
  96. * 生命周期函数--监听页面初次渲染完成
  97. */
  98. onReady: function () {
  99. },
  100. /**
  101. * 生命周期函数--监听页面显示
  102. */
  103. onShow: function () {
  104. },
  105. /**
  106. * 生命周期函数--监听页面隐藏
  107. */
  108. onHide: function () {
  109. },
  110. /**
  111. * 生命周期函数--监听页面卸载
  112. */
  113. onUnload: function () {
  114. },
  115. /**
  116. * 页面相关事件处理函数--监听用户下拉动作
  117. */
  118. onPullDownRefresh: function () {
  119. },
  120. /**
  121. * 页面上拉触底事件的处理函数
  122. */
  123. onReachBottom: function () {
  124. },
  125. /**
  126. * 用户点击右上角分享
  127. */
  128. onShareAppMessage: function () {
  129. }
  130. })