index.js 3.2 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: 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: 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. wx.request({
  30. url: `${app.globalData.publicUrl}/courtAdmin/api/user/login`, //接口地址
  31. method: 'post',
  32. data: params,
  33. success(res) {
  34. if (res.data.errcode == 0) {
  35. app.globalData.userInfo = res.data.data;//存用户信息到app.js
  36. wx.setStorage({ key: "token", data: res.data.data })// 存用户信息到storage,以便之后判断用户是否登录
  37. wx.showToast({ title: `账号登录成功`, icon: 'success', duration: 2000 }) //登录成功提示
  38. wx.navigateTo({ url: '/pages/home/index' })// 跳转页面
  39. } else {
  40. wx.showToast({
  41. title: res.data.errmsg,
  42. icon: 'none',
  43. duration: 2000
  44. })
  45. }
  46. }
  47. })
  48. }
  49. },
  50. // 微信登录
  51. phoneLogin: function () {
  52. wx.showToast({ title: `暂未开放`, icon: 'error', duration: 2000 })
  53. },
  54. //去注册
  55. register: function () {
  56. wx.navigateTo({ url: '/pages/register/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. let infoHeight = client.windowHeight;
  72. // 减去状态栏
  73. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2)
  74. // 是否减去底部菜单
  75. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  76. if (infoHeight) this.setData({ infoHeight: infoHeight })
  77. },
  78. /**
  79. * 生命周期函数--监听页面初次渲染完成
  80. */
  81. onReady: function () {
  82. },
  83. /**
  84. * 生命周期函数--监听页面显示
  85. */
  86. onShow: function () {
  87. },
  88. /**
  89. * 生命周期函数--监听页面隐藏
  90. */
  91. onHide: function () {
  92. },
  93. /**
  94. * 生命周期函数--监听页面卸载
  95. */
  96. onUnload: function () {
  97. },
  98. /**
  99. * 页面相关事件处理函数--监听用户下拉动作
  100. */
  101. onPullDownRefresh: function () {
  102. },
  103. /**
  104. * 页面上拉触底事件的处理函数
  105. */
  106. onReachBottom: function () {
  107. },
  108. /**
  109. * 用户点击右上角分享
  110. */
  111. onShareAppMessage: function () {
  112. }
  113. })