game/src/app.wpy

215 lines
5.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<style lang="less">
@import "style/zanui/index.wxss";
@import "style/font.wxss";
@import "style/btn.wxss";
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
box-sizing: border-box;
width: 749rpx;
overflow-x: hidden;
}
.hidden{
display: none!important;
}
</style>
<script>
import wepy from 'wepy'
import 'wepy-async-function'
import jcEvent from './common/jc-event';
import http from './utils/http';
import log, { hookConsoleLog } from './utils/logger';
import g from './common/global';
let sdkManage = require('./jcfw/SDKManage');
export default class extends wepy.app {
config = {
pages: [
'pages/index',
'pages/game',
'pages/gameInfo',
'pages/login',
'pages/search',
'pages/jumppage'
],
window: {
backgroundTextStyle: 'light',
navigationBarBackgroundColor: '#fff',
navigationBarTitleText: 'WeChat',
navigationBarTextStyle: 'black'
},
//0爆冰大人 1支付
navigateToMiniProgramAppIdList: ["wxc02bad35ad26165e", "wx815bf59d472c0a63"]
}
globalData = {
userInfo: null,
gameToken: null,
vip: false
}
constructor () {
super();
this.use('requestfix');
this.use('promisify');
// (g.env === 'test') && hookConsoleLog();
sdkManage.init();
sdkManage.Login(() => {
let account = wepy.getStorageSync('account');
console.log('finish parse jcfw');
console.log(account);
jcEvent.emit(jcEvent.events.NEED_UPDATE_CFG, {});
if (this.checkClientLogin()) {
this.login();
}
});
//全局拦截器
this.intercept('request', {
config(p) {
let token = wepy.getStorageSync('game_token');
if (token) {
p.header['Authorization'] = `JWT ${token}`;
}
return p;
},
success(response) {
if (response.data.errcode === 0) {
return response.data;
} else if (response.statusCode === '404') {
wepy.showToast({ title: '404 请求接口不存在' });
}
return response;
},
fail(response) {
wepy.showModal({
title: '提示',
content: `服务器错误,请稍候再试! ${response.errMsg}`
});
return response;
}
});
}
//登录流程
login() {
let self = this;
return new Promise(async function(resolve, reject) {
wepy.setStorageSync('game_token', null);
//获取code
try {
let account = wepy.getStorageSync('account');
let data = {
account_id: account.account_id,
session_id: account.session_id,
open_id: account.openid,
unionid: account.unionid
};
let res = await http.post('/api/emulated/user_login', data);
wepy.setStorageSync('game_token', res.token);
self.globalData.gameToken = res.token;
jcEvent.emit(jcEvent.events.LOGIN_FINISHED, {});
resolve();
} catch (error) {
reject(error);
}
})
}
onLaunch() {
}
onShow(options) {
console.log('app.onShow');
console.log(options);
if (options.scene === 1038) { //从小程序返回
jcEvent.emit(jcEvent.events.NEED_UPDATE_SCORE, {});
let referrerInfo = options.referrerInfo;
let data = referrerInfo.extraData;
if (data && data.event_type === 'pay_result') {
if (data.type === 'item' ) {
jcEvent.emit(jcEvent.events.BUY_ITEM_RESULT, data);
} else {
jcEvent.emit(jcEvent.events.BUY_VIP_RESULT, data);
}
}
}
}
getUserInfo() {
if (!this.globalData.userInfo) {
if (wepy.getStorageSync('userInfo')) {
this.globalData.userInfo = JSON.parse(wepy.getStorageSync('userInfo'));
}
}
return this.globalData.userInfo
}
checkAuthorize() {
let self = this;
let needAuthorize = true;
if (!self.globalData.userInfo) {
if (wepy.getStorageSync('userInfo')) {
self.globalData.userInfo = JSON.parse(wepy.getStorageSync('userInfo'));
needAuthorize = false;
}
} else {
needAuthorize = false;
}
return needAuthorize;
}
checkClientLogin() {
let self = this;
let needLogin = true;
if (!self.globalData.gameToken) {
if (wepy.getStorageSync('game_token')) {
self.globalData.gameToken = wepy.getStorageSync('game_token');
needLogin = false;
}
} else {
needLogin = false;
}
return needLogin;
// return true;
}
updateGlobalData(name, obj) {
// 校验: globalData
if (!this.globalData) return;
// 校验: 操作字段
if (typeof name !== 'string' || name === '') return {};
// 取已有信息
const info = this.globalData[name] || {};
// 更新缓存
if (obj) {
// Object合并第一层
this.globalData[name] = Object.assign({}, info, obj);
} else if (!this.isUndefined(obj)) {
// 其他非undefined数据直接覆盖
this.globalData[name] = obj;
}
this.$apply;
console.info(`[${obj ? 'UPDATE' : 'GET'} GlobalData ${name}]:`, this.globalData[name]);
return this.globalData[name];
}
getGlobalDate(name) {
return this.globalData[name];
}
showAll() {
return this.globalData.showAll;
}
isVip() {
return this.globalData.vip;
}
updateVip(vip) {
this.globalData.vip = vip;
}
getShareCount() {
return sdkManage.getAchievInviteeNum();
}
getShareDetail() {
return sdkManage.queryAchievementShareDetail();
}
updateUserInfo(allInfo) {
return sdkManage.updateUser(allInfo);
}
}
</script>