index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // logo
  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. wx.login({
  45. success: async (arr) => {
  46. const { code: js_code } = arr;
  47. const { wx_config } = app.globalData;
  48. wx.getStorage({
  49. key: 'openid',
  50. success(res) {
  51. if (res.data) wx.redirectTo({ url: '/pagesHome/home/index' })
  52. },
  53. async fail(err) {
  54. const aee = await app.$api('token/app', 'GET', { js_code: js_code, config: wx_config.config });
  55. if (aee.errcode == '0') {
  56. wx.setStorage({ key: "openid", data: aee.data.openid })
  57. wx.redirectTo({ url: '/pagesHome/home/index' })
  58. } else {
  59. wx.showToast({ title: `${aee.errmsg}`, icon: 'none' });
  60. }
  61. }
  62. })
  63. },
  64. });
  65. },
  66. /**
  67. * 生命周期函数--监听页面隐藏
  68. */
  69. onHide() {
  70. },
  71. /**
  72. * 生命周期函数--监听页面卸载
  73. */
  74. onUnload() {
  75. },
  76. /**
  77. * 页面相关事件处理函数--监听用户下拉动作
  78. */
  79. onPullDownRefresh() {
  80. },
  81. /**
  82. * 页面上拉触底事件的处理函数
  83. */
  84. onReachBottom() {
  85. },
  86. /**
  87. * 用户点击右上角分享
  88. */
  89. onShareAppMessage() {
  90. }
  91. })