index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const app = getApp();
  2. Page({
  3. data: {
  4. frameStyle: { useTop: true, name: '系统', leftArrow: false, useBar: false },
  5. },
  6. /**
  7. * 生命周期函数--监听页面加载
  8. */
  9. onLoad: function (options) {
  10. const that = this;
  11. // 监听用户是否登录
  12. that.watchLogin();
  13. },
  14. // 监听用户是否登录
  15. watchLogin: async function () {
  16. // 监听用户是否登录,
  17. wx.getStorage({
  18. key: 'user',
  19. success: async (res) => {
  20. // if (res.data) wx.redirectTo({ url: '/pages/home/index' })
  21. },
  22. fail: async (res) => {
  23. wx.redirectTo({ url: '/pages/home/index' });
  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. });