index.js 3.3 KB

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