app.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. config.openGame = function (e) {
  87. let openGame
  88. if (e.currentTarget.id) {
  89. openGame = {
  90. id: e.currentTarget.id,
  91. type: 'navigator'
  92. }
  93. } else {
  94. openGame = {
  95. id: e.currentTarget.dataset.id,
  96. type: 'qrcode'
  97. }
  98. }
  99. if (app.globalData.openGameSet) {
  100. app.globalData.openGame = openGame
  101. } else {
  102. api.openGame({
  103. game_id: openGame.id
  104. }).then(res => {
  105. })
  106. }
  107. }
  108. return originPage(config)
  109. }
  110. },
  111. onShow: function (options) {
  112. console.log('app onshow', options)
  113. // 安卓系统桌面图标
  114. let sysInfo = wx.getSystemInfoSync()
  115. // if ((sysInfo.platform === 'ios' && options.scene === 1089) || options.scene === 1023) {
  116. if (options.scene === 1023) {
  117. this.globalData.windowOpen = true
  118. }
  119. let openGame = this.globalData.openGame
  120. console.log(openGame)
  121. // if (openGame && ((openGame.type === 'qrcode' && options.scene === 1038) || openGame.type === 'navigator')) {
  122. if (openGame && openGame.ts) {
  123. let ts = new Date().getTime()
  124. if (openGame.ts && ts - openGame.ts > this.globalData.openGameSet.duration * 1000) {
  125. console.log('前台请求领奖')
  126. api.openGame({
  127. game_id: openGame.id
  128. }).then(res => {
  129. if (res.data.status === 2) {
  130. this.globalData.openGame = null
  131. }
  132. })
  133. } else {
  134. console.log('未满试玩时长')
  135. wx.showModal({
  136. content: this.globalData.openGameSet.msg,
  137. showCancel: false
  138. })
  139. this.globalData.openGame = null
  140. }
  141. }
  142. },
  143. onHide: function () {
  144. let openGame = this.globalData.openGame
  145. if (openGame) {
  146. console.log('游戏计时开始')
  147. api.startGame({
  148. game_id: openGame.id
  149. }).then(res => {
  150. this.globalData.openGame.ts = new Date().getTime()
  151. })
  152. }
  153. let _t = setInterval(() => {
  154. let openGame = this.globalData.openGame
  155. if (openGame) {
  156. console.log('后台请求领奖')
  157. api.openGame({
  158. game_id: openGame.id
  159. }).then(res => {
  160. if (res.data.status === 2) {
  161. this.globalData.openGame = null
  162. }
  163. })
  164. } else {
  165. clearInterval(_t)
  166. }
  167. }, this.globalData.openGameSet.duration * 1000)
  168. },
  169. getMeInfo() {
  170. const _self = this
  171. console.log('开始获取用户信息...')
  172. api.getUserInfo().then(res => {
  173. if (res.code === 200) {
  174. _self.globalData.auth = res.data.auth
  175. _self.globalData.openGameSet = res.data.open_game_set
  176. }
  177. if (res.data.code === 1002) {
  178. console.log('登陆过期正在重新登陆...')
  179. _self.againLogin()
  180. }
  181. })
  182. },
  183. // 重新登陆
  184. againLogin: function () {
  185. if (this.globalData.againLogin) return false
  186. this.memLogin().then(() => {
  187. this.globalData.againLogin = false
  188. console.log('登录成功')
  189. this.getMeInfo()
  190. })
  191. },
  192. memLogin: function() {
  193. const _self = this
  194. console.log('登录中...')
  195. return new Promise((resolve, reject) => {
  196. wx.login({
  197. success: function (res) {
  198. console.log('wx login success', res)
  199. if (res.code) {
  200. let params = {
  201. code: res.code
  202. }
  203. if (_self.globalData.shareQuery) {
  204. params.state = _self.globalData.shareQuery.state || _self.globalData.shareQuery.scene || ''
  205. }
  206. api.menLogin(params).then(res => {
  207. console.log('login success', res)
  208. if (res.code === 200) {
  209. wx.setStorageSync('userInfo', res.data)
  210. resolve()
  211. } else {
  212. console.log('登陆失败...')
  213. }
  214. }, err => {
  215. console.log('login fail', err)
  216. })
  217. } else {
  218. console.log('登录失败!' + res.errMsg)
  219. reject()
  220. }
  221. },
  222. fail: function (res) {
  223. console.log('wx login fail', res)
  224. }
  225. });
  226. })
  227. },
  228. globalData: {
  229. windowOpen: false,
  230. openGZH: false,
  231. openGame: null,
  232. openGameSet: null,
  233. shareQuery: null,
  234. userInfo: {},
  235. // scene: 1001,
  236. // is_session: false,
  237. PageCallback: false,
  238. auth: {
  239. favorite: 1,
  240. gain: 1,
  241. guide: 1,
  242. memcenter: 1,
  243. red: 1,
  244. share: 1,
  245. active: 1
  246. },
  247. mpType: config.mpType,
  248. mpName: '多鱼游戏宝盒',
  249. mpGzh: '多鱼游戏'
  250. }
  251. })