app.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //app.js
  2. import config from './utils/config.js'
  3. import api from './utils/api.js'
  4. App({
  5. onLaunch: function (options) {
  6. console.log('onLaunch-----', options)
  7. this.globalData.shareQuery = options.query
  8. if (options.scene === 1044 || options.scene === 1007 || options.scene === 1008) {
  9. // wx.getShareInfo({
  10. // shareTicket: options.shareTicket,
  11. // success: function(res) {
  12. // var encryptedData = res.encryptedData;
  13. // var iv = res.iv;
  14. // }
  15. // })
  16. }
  17. const _self = this
  18. wx.checkSession({
  19. success: function() {
  20. console.log('session_key 未过期,并且在本生命周期一直有效')
  21. console.log('token', wx.getStorageSync('userInfo').user_token || '空')
  22. if (wx.getStorageSync('userInfo').user_token) {
  23. wx.getSetting({
  24. success: res => {
  25. if (res.authSetting['scope.userInfo']) {
  26. // 已经授权,可以直接调用 getUserInfo 获取用户信息,不会弹框
  27. wx.getUserInfo({
  28. success: res => {
  29. api.updateUserInfo({
  30. raw_data: res.rawData,
  31. signature: res.signature,
  32. encrypted_data: res.encryptedData,
  33. iv: res.iv,
  34. HideLoading: true,
  35. HideToast: true
  36. }).then(res => {
  37. _self.getMeInfo()
  38. }).catch(res => {
  39. _self.getMeInfo()
  40. })
  41. },
  42. fail: res => {
  43. _self.getMeInfo()
  44. }
  45. })
  46. } else {
  47. _self.getMeInfo()
  48. }
  49. },
  50. fail: res => {
  51. _self.getMeInfo()
  52. }
  53. })
  54. } else {
  55. console.log('未获取到token,重新登录')
  56. _self.againLogin()
  57. }
  58. },
  59. fail: function() {
  60. console.log('session_key 已经失效,需要重新执行登录流程')
  61. _self.againLogin()
  62. }
  63. })
  64. const originPage = Page
  65. let app = this
  66. Page = function (config) {
  67. let { onShareAppMessage } = config
  68. if (!onShareAppMessage) {
  69. config.onShareAppMessage = function (options) {
  70. let { shareinfo, state, mem_id } = app.globalData.userInfo
  71. if (shareinfo) {
  72. shareinfo = shareinfo[Math.floor(Math.random() * shareinfo.length)]
  73. } else {
  74. shareinfo = {
  75. title: '',
  76. image: ''
  77. }
  78. }
  79. return {
  80. title: shareinfo.title,
  81. path: `/pages/share/share?state=${state}&mem_id=${mem_id}`,
  82. imageUrl: shareinfo.image
  83. }
  84. }
  85. }
  86. return originPage(config)
  87. }
  88. },
  89. onShow: function (options) {
  90. console.log('app onshow', options)
  91. // 安卓系统桌面图标
  92. let sysInfo = wx.getSystemInfoSync()
  93. // if ((sysInfo.platform === 'ios' && options.scene === 1089) || options.scene === 1023) {
  94. if (options.scene === 1023) {
  95. this.globalData.windowOpen = true
  96. }
  97. },
  98. getMeInfo() {
  99. const _self = this
  100. console.log('开始获取用户信息...')
  101. api.getUserInfo().then(res => {
  102. if (res.code === 200) {
  103. _self.globalData.auth = res.data.auth
  104. }
  105. if (res.data.code === 1002) {
  106. console.log('登陆过期正在重新登陆...')
  107. _self.againLogin()
  108. }
  109. })
  110. },
  111. // 重新登陆
  112. againLogin: function () {
  113. if (this.globalData.againLogin) return false
  114. this.memLogin().then(() => {
  115. this.globalData.againLogin = false
  116. console.log('登录成功')
  117. this.getMeInfo()
  118. })
  119. },
  120. memLogin: function() {
  121. const _self = this
  122. console.log('登录中...')
  123. return new Promise((resolve, reject) => {
  124. wx.login({
  125. success: function (res) {
  126. console.log('wx login success', res)
  127. if (res.code) {
  128. let params = {
  129. code: res.code
  130. }
  131. if (_self.globalData.shareQuery) {
  132. params.state = _self.globalData.shareQuery.state || _self.globalData.shareQuery.scene || ''
  133. }
  134. api.menLogin(params).then(res => {
  135. console.log('login success', res)
  136. if (res.code === 200) {
  137. wx.setStorageSync('userInfo', res.data)
  138. resolve()
  139. } else {
  140. console.log('登陆失败...')
  141. }
  142. }, err => {
  143. console.log('login fail', err)
  144. })
  145. } else {
  146. console.log('登录失败!' + res.errMsg)
  147. reject()
  148. }
  149. },
  150. fail: function (res) {
  151. console.log('wx login fail', res)
  152. }
  153. });
  154. })
  155. },
  156. globalData: {
  157. windowOpen: false,
  158. openGZH: false,
  159. shareQuery: null,
  160. userInfo: {},
  161. // scene: 1001,
  162. // is_session: false,
  163. PageCallback: false,
  164. auth: {
  165. favorite: 1,
  166. gain: 1,
  167. guide: 1,
  168. memcenter: 1,
  169. red: 1,
  170. share: 1,
  171. active: 1
  172. },
  173. mpType: config.mpType,
  174. mpName: '欢乐游戏盒子',
  175. mpGzh: '欢乐游戏'
  176. }
  177. })