index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const app = getApp();
  2. Page({
  3. data: {
  4. frameStyle: { useTop: false, name: '系统', leftArrow: false, useBar: false },
  5. fileUrl: app.globalData.fileUrl,
  6. },
  7. /**
  8. * 生命周期函数--监听页面加载
  9. */
  10. onLoad: function (options) {
  11. const that = this;
  12. // 监听用户是否登录
  13. that.watchLogin();
  14. },
  15. // 监听用户是否登录
  16. watchLogin: async function () {
  17. // 监听用户是否登录,
  18. wx.getStorage({
  19. key: 'user',
  20. success: async (res) => {
  21. if (res.data) wx.redirectTo({ url: '/pages/home/index' })
  22. },
  23. fail: async (res) => {
  24. wx.login({
  25. success: async (arr) => {
  26. if (app.globalData?.wxInfo?.openid) {
  27. await this.toLogin(app.globalData?.wxInfo?.openid);
  28. return;
  29. }
  30. const { code: js_code } = arr;
  31. const aee = await app.$get('/wechat/api/login/app', { js_code: js_code, config: 'newCourtApp' });
  32. if (aee.errcode === 0 && aee.data?.openid) {
  33. const openid = aee.data.openid;
  34. app.globalData.wxInfo = { openid };
  35. await this.toLogin(openid);
  36. } else {
  37. wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 });
  38. wx.redirectTo({ url: '/pages/login/index' });
  39. }
  40. },
  41. });
  42. },
  43. });
  44. },
  45. async toLogin(openid) {
  46. const res = await app.$post('/newCourt/api/user/wxAppLogin', { openid });
  47. if (app.$checkRes(res)) {
  48. const { data } = res;
  49. // 没有用户,去注册
  50. if (!data) wx.redirectTo({ url: '/pages/register/index' });
  51. else {
  52. // 有用户,存起来,跳转
  53. wx.setStorageSync('user', data);
  54. wx.redirectTo({ url: '/pages/home/index' });
  55. }
  56. }
  57. },
  58. /**
  59. * 生命周期函数--监听页面初次渲染完成
  60. */
  61. onReady: function () { },
  62. /**
  63. * 生命周期函数--监听页面显示
  64. */
  65. onShow: function () { },
  66. /**
  67. * 生命周期函数--监听页面隐藏
  68. */
  69. onHide: function () { },
  70. /**
  71. * 生命周期函数--监听页面卸载
  72. */
  73. onUnload: function () { },
  74. /**
  75. * 页面相关事件处理函数--监听用户下拉动作
  76. */
  77. onPullDownRefresh: function () { },
  78. /**
  79. * 页面上拉触底事件的处理函数
  80. */
  81. onReachBottom: function () { },
  82. /**
  83. * 用户点击右上角分享
  84. */
  85. onShareAppMessage: function () { },
  86. });