app.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const config = require('./config')
  2. const themeListeners = []
  3. global.isDemo = true
  4. App({
  5. onLaunch(opts, data) {
  6. // const that = this;
  7. // const canIUseSetBackgroundFetchToken = wx.canIUse('setBackgroundFetchToken')
  8. // if (canIUseSetBackgroundFetchToken) {
  9. // wx.setBackgroundFetchToken({
  10. // token: 'getBackgroundFetchToken',
  11. // })
  12. // }
  13. // if (wx.getBackgroundFetchData) {
  14. // wx.getBackgroundFetchData({
  15. // fetchType: 'pre',
  16. // success(res) {
  17. // that.globalData.backgroundFetchData = res;
  18. // console.log('读取预拉取数据成功')
  19. // },
  20. // fail() {
  21. // console.log('读取预拉取数据失败')
  22. // wx.showToast({
  23. // title: '无缓存数据',
  24. // icon: 'none'
  25. // })
  26. // },
  27. // complete() {
  28. // console.log('结束读取')
  29. // }
  30. // })
  31. // }
  32. console.log('App Launch', opts)
  33. if (data && data.path) {
  34. wx.navigateTo({
  35. url: data.path,
  36. })
  37. }
  38. if (!wx.cloud) {
  39. console.error('请使用 2.2.3 或以上的基础库以使用云能力')
  40. } else {
  41. wx.cloud.init({
  42. env: config.envId,
  43. traceUser: true,
  44. })
  45. }
  46. },
  47. onShow(opts) {
  48. console.log('App Show', opts)
  49. // console.log(wx.getSystemInfoSync())
  50. },
  51. onHide() {
  52. console.log('App Hide')
  53. },
  54. onThemeChange({ theme }) {
  55. this.globalData.theme = theme
  56. themeListeners.forEach((listener) => {
  57. listener(theme)
  58. })
  59. },
  60. watchThemeChange(listener) {
  61. if (themeListeners.indexOf(listener) < 0) {
  62. themeListeners.push(listener)
  63. }
  64. },
  65. unWatchThemeChange(listener) {
  66. const index = themeListeners.indexOf(listener)
  67. if (index > -1) {
  68. themeListeners.splice(index, 1)
  69. }
  70. },
  71. globalData: {
  72. theme: wx.getSystemInfoSync().theme,
  73. hasLogin: false,
  74. openid: null,
  75. iconTabbar: '/page/weui/example/images/icon_tabbar.png',
  76. },
  77. // lazy loading openid
  78. getUserOpenId(callback) {
  79. const self = this
  80. if (self.globalData.openid) {
  81. callback(null, self.globalData.openid)
  82. } else {
  83. wx.login({
  84. success(data) {
  85. wx.cloud.callFunction({
  86. name: 'login',
  87. data: {
  88. action: 'openid'
  89. },
  90. success: res => {
  91. console.log('拉取openid成功', res)
  92. self.globalData.openid = res.result.openid
  93. callback(null, self.globalData.openid)
  94. },
  95. fail: err => {
  96. console.log('拉取用户openid失败,将无法正常使用开放接口等服务', res)
  97. callback(res)
  98. }
  99. })
  100. },
  101. fail(err) {
  102. console.log('wx.login 接口调用失败,将无法正常使用开放接口等服务', err)
  103. callback(err)
  104. }
  105. })
  106. }
  107. },
  108. // 通过云函数获取用户 openid,支持回调或 Promise
  109. getUserOpenIdViaCloud() {
  110. return wx.cloud.callFunction({
  111. name: 'wxContext',
  112. data: {}
  113. }).then(res => {
  114. this.globalData.openid = res.result.openid
  115. return res.result.openid
  116. })
  117. }
  118. })