123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //app.js
- import config from './utils/config.js'
- import api from './utils/api.js'
- App({
- onLaunch: function (options) {
- console.log('onLaunch-----', options)
- this.globalData.shareQuery = options.query
- if (options.scene === 1044 || options.scene === 1007 || options.scene === 1008) {
- // wx.getShareInfo({
- // shareTicket: options.shareTicket,
- // success: function(res) {
- // var encryptedData = res.encryptedData;
- // var iv = res.iv;
- // }
- // })
- }
- const _self = this
- wx.checkSession({
- success: function() {
- console.log('session_key 未过期,并且在本生命周期一直有效')
- console.log('token', wx.getStorageSync('userInfo').user_token || '空')
- if (wx.getStorageSync('userInfo').user_token) {
- wx.getSetting({
- success: res => {
- if (res.authSetting['scope.userInfo']) {
- // 已经授权,可以直接调用 getUserInfo 获取用户信息,不会弹框
- wx.getUserInfo({
- success: res => {
- api.updateUserInfo({
- raw_data: res.rawData,
- signature: res.signature,
- encrypted_data: res.encryptedData,
- iv: res.iv,
- HideLoading: true,
- HideToast: true
- }).then(res => {
- _self.getMeInfo()
- }).catch(res => {
- _self.getMeInfo()
- })
- },
- fail: res => {
- _self.getMeInfo()
- }
- })
- } else {
- _self.getMeInfo()
- }
- },
- fail: res => {
- _self.getMeInfo()
- }
- })
- } else {
- console.log('未获取到token,重新登录')
- _self.againLogin()
- }
- },
- fail: function() {
- console.log('session_key 已经失效,需要重新执行登录流程')
- _self.againLogin()
- }
- })
- const originPage = Page
- let app = this
- Page = function (config) {
- let { onShareAppMessage } = config
- if (!onShareAppMessage) {
- config.onShareAppMessage = function (options) {
- let { shareinfo, state, mem_id } = app.globalData.userInfo
- if (shareinfo) {
- shareinfo = shareinfo[Math.floor(Math.random() * shareinfo.length)]
- } else {
- shareinfo = {
- title: '',
- image: ''
- }
- }
- return {
- title: shareinfo.title,
- path: `/pages/share/share?state=${state}&mem_id=${mem_id}`,
- imageUrl: shareinfo.image
- }
- }
- }
- config.openGame = function (e) {
- let openGame
- if (e.currentTarget.id) {
- openGame = {
- id: e.currentTarget.id,
- type: 'navigator'
- }
- } else {
- openGame = {
- id: e.currentTarget.dataset.id,
- type: 'qrcode'
- }
- }
- if (app.globalData.openGameSet) {
- app.globalData.openGame = openGame
- } else {
- api.openGame({
- game_id: openGame.id
- }).then(res => {
- })
- }
- }
- return originPage(config)
- }
- },
- onShow: function (options) {
- console.log('app onshow', options)
- // 安卓系统桌面图标
- let sysInfo = wx.getSystemInfoSync()
- // if ((sysInfo.platform === 'ios' && options.scene === 1089) || options.scene === 1023) {
- if (options.scene === 1023) {
- this.globalData.windowOpen = true
- }
- let openGame = this.globalData.openGame
- console.log(openGame)
- // if (openGame && ((openGame.type === 'qrcode' && options.scene === 1038) || openGame.type === 'navigator')) {
- if (openGame && openGame.ts) {
- let ts = new Date().getTime()
- if (openGame.ts && ts - openGame.ts > this.globalData.openGameSet.duration * 1000) {
- console.log('前台请求领奖')
- api.openGame({
- game_id: openGame.id
- }).then(res => {
- if (res.data.status === 2) {
- this.globalData.openGame = null
- }
- })
- } else {
- console.log('未满试玩时长')
- wx.showModal({
- content: this.globalData.openGameSet.msg,
- showCancel: false
- })
- this.globalData.openGame = null
- }
- }
- },
- onHide: function () {
- let openGame = this.globalData.openGame
- if (openGame) {
- console.log('游戏计时开始')
- api.startGame({
- game_id: openGame.id
- }).then(res => {
- this.globalData.openGame.ts = new Date().getTime()
- })
- }
- let _t = setInterval(() => {
- let openGame = this.globalData.openGame
- if (openGame) {
- console.log('后台请求领奖')
- api.openGame({
- game_id: openGame.id
- }).then(res => {
- if (res.data.status === 2) {
- this.globalData.openGame = null
- }
- })
- } else {
- clearInterval(_t)
- }
- }, this.globalData.openGameSet.duration * 1000)
- },
- getMeInfo() {
- const _self = this
- console.log('开始获取用户信息...')
- api.getUserInfo().then(res => {
- if (res.code === 200) {
- _self.globalData.auth = res.data.auth
- _self.globalData.openGameSet = res.data.open_game_set
- }
- if (res.data.code === 1002) {
- console.log('登陆过期正在重新登陆...')
- _self.againLogin()
- }
- })
- },
- // 重新登陆
- againLogin: function () {
- if (this.globalData.againLogin) return false
- this.memLogin().then(() => {
- this.globalData.againLogin = false
- console.log('登录成功')
- this.getMeInfo()
- })
- },
- memLogin: function() {
- const _self = this
- console.log('登录中...')
- return new Promise((resolve, reject) => {
- wx.login({
- success: function (res) {
- console.log('wx login success', res)
- if (res.code) {
- let params = {
- code: res.code
- }
- if (_self.globalData.shareQuery) {
- params.state = _self.globalData.shareQuery.state || _self.globalData.shareQuery.scene || ''
- }
- api.menLogin(params).then(res => {
- console.log('login success', res)
- if (res.code === 200) {
- wx.setStorageSync('userInfo', res.data)
- resolve()
- } else {
- console.log('登陆失败...')
- }
- }, err => {
- console.log('login fail', err)
- })
- } else {
- console.log('登录失败!' + res.errMsg)
- reject()
- }
- },
- fail: function (res) {
- console.log('wx login fail', res)
- }
- });
- })
- },
- globalData: {
- windowOpen: false,
- openGZH: false,
- openGame: null,
- openGameSet: null,
- shareQuery: null,
- userInfo: {},
- // scene: 1001,
- // is_session: false,
- PageCallback: false,
- auth: {
- favorite: 1,
- gain: 1,
- guide: 1,
- memcenter: 1,
- red: 1,
- share: 1,
- active: 1
- },
- mpType: config.mpType,
- mpName: '多鱼游戏宝盒',
- mpGzh: '多鱼游戏'
- }
- })
|