index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. from: {},
  13. },
  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. // 提交登录
  22. onSubmit: async function (e) {
  23. const params = e.detail.value;
  24. if (!this.WxValidate.checkForm(params)) {
  25. const error = this.WxValidate.errorList[0];
  26. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  27. return false
  28. } else {
  29. const res = await app.$post('/courtAdmin/api/user/login', params);
  30. if (res.errcode === 0) {
  31. app.globalData.userInfo = res.data;//存用户信息到app.js
  32. wx.setStorage({ key: "token", data: res.data })// 存用户信息到storage,以便之后判断用户是否登录
  33. wx.showToast({ title: `账号登录成功`, icon: 'success', duration: 2000 }) //登录成功提示
  34. wx.navigateTo({ url: '/pages/home/index' })// 跳转页面
  35. } else {
  36. wx.showToast({ title: res.errmsg, icon: 'none', duration: 2000 })
  37. }
  38. }
  39. },
  40. // 微信登录
  41. phoneLogin: function () {
  42. wx.showToast({ title: `暂未开放`, icon: 'error', duration: 2000 })
  43. },
  44. //去注册
  45. register: function () {
  46. wx.navigateTo({ url: '/pages/register/index', })
  47. },
  48. /**
  49. * 生命周期函数--监听页面加载
  50. */
  51. onLoad: function (options) {
  52. //验证规则函数
  53. this.initValidate()
  54. // 计算高度
  55. this.searchHeight()
  56. },
  57. // 计算高度
  58. searchHeight: function () {
  59. let frameStyle = this.data.frameStyle;
  60. let client = app.globalData.client;
  61. let infoHeight = client.windowHeight;
  62. // 减去状态栏
  63. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2)
  64. // 是否减去底部菜单
  65. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  66. if (infoHeight) this.setData({ infoHeight: infoHeight })
  67. },
  68. /**
  69. * 生命周期函数--监听页面初次渲染完成
  70. */
  71. onReady: function () {
  72. },
  73. /**
  74. * 生命周期函数--监听页面显示
  75. */
  76. onShow: function () {
  77. },
  78. /**
  79. * 生命周期函数--监听页面隐藏
  80. */
  81. onHide: function () {
  82. },
  83. /**
  84. * 生命周期函数--监听页面卸载
  85. */
  86. onUnload: function () {
  87. },
  88. /**
  89. * 页面相关事件处理函数--监听用户下拉动作
  90. */
  91. onPullDownRefresh: function () {
  92. },
  93. /**
  94. * 页面上拉触底事件的处理函数
  95. */
  96. onReachBottom: function () {
  97. },
  98. /**
  99. * 用户点击右上角分享
  100. */
  101. onShareAppMessage: function () {
  102. }
  103. })