index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const app = getApp()
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. logo: '',
  8. },
  9. /**
  10. * 生命周期函数--监听页面加载
  11. */
  12. onLoad(options) {
  13. },
  14. /**
  15. * 生命周期函数--监听页面初次渲染完成
  16. */
  17. onReady() {
  18. },
  19. /**
  20. * 生命周期函数--监听页面显示
  21. */
  22. async onShow() {
  23. const that = this;
  24. wx.showLoading({ title: '加载中', mask: true })
  25. await that.searchConfig()
  26. // 监听用户是否登录
  27. await that.watchLogin();
  28. wx.hideLoading()
  29. },
  30. async searchConfig() {
  31. const that = this;
  32. const res = await app.$api('config', 'GET', {});
  33. if (res.errcode == '0') {
  34. // index,长图。
  35. if (res.data && res.data.logo_url && res.data.logo_url.length > 0) {
  36. let logo = res.data.logo_url[0].url
  37. that.setData({ logo })
  38. }
  39. wx.setStorage({ key: "config", data: res.data })
  40. }
  41. },
  42. // 监听用户是否登录
  43. watchLogin: async function () {
  44. const that = this;
  45. wx.login({
  46. success: async (arr) => {
  47. const { code: js_code } = arr;
  48. const { wx_config } = app.globalData;
  49. wx.getStorage({
  50. key: 'openid',
  51. success(res) {
  52. if (res.data) wx.redirectTo({ url: '/pagesHome/home/index' })
  53. },
  54. async fail(err) {
  55. const aee = await app.$api('token/app', 'GET', { js_code: js_code, config: wx_config.config });
  56. if (aee.errcode == '0') {
  57. wx.setStorage({ key: "openid", data: aee.data.openid })
  58. wx.redirectTo({ url: '/pagesHome/home/index' })
  59. } else {
  60. wx.showToast({ title: `${aee.errmsg}`, icon: 'none' });
  61. }
  62. }
  63. })
  64. },
  65. });
  66. },
  67. /**
  68. * 生命周期函数--监听页面隐藏
  69. */
  70. onHide() {
  71. },
  72. /**
  73. * 生命周期函数--监听页面卸载
  74. */
  75. onUnload() {
  76. },
  77. /**
  78. * 页面相关事件处理函数--监听用户下拉动作
  79. */
  80. onPullDownRefresh() {
  81. },
  82. /**
  83. * 页面上拉触底事件的处理函数
  84. */
  85. onReachBottom() {
  86. },
  87. /**
  88. * 用户点击右上角分享
  89. */
  90. onShareAppMessage() {
  91. }
  92. })