实现最近游戏相关功能

This commit is contained in:
zhl 2019-02-20 13:49:43 +08:00
parent 147ce37d5b
commit 5dd1bc521c
6 changed files with 148 additions and 32 deletions

View File

@ -17,7 +17,10 @@
<script>
import wepy from 'wepy'
import 'wepy-async-function'
import jcEvent from './common/jc-event';
import http from './utils/http';
let sdkManage = require('./jcfw/SDKManage');
export default class extends wepy.app {
config = {
pages: [
@ -36,34 +39,105 @@ export default class extends wepy.app {
}
globalData = {
userInfo: null
userInfo: null,
gameToken: null
}
constructor () {
super();
this.use('requestfix');
this.use('promisify');
sdkManage.init();
sdkManage.Login(() => {
let account = wepy.getStorageSync('account');
console.log('finish parse jcfw');
console.log(account);
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('login-finished', {})
resolve();
} catch (error) {
reject(error);
}
})
}
onLaunch() {
}
getUserInfo() {
return this.globalData.userInfo
}
checkClientLogin() {
checkAuthorize() {
let self = this;
let needLogin = true;
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

27
src/common/jc-event.js Normal file
View File

@ -0,0 +1,27 @@
let events = {};
export default {
on (name, self, cb) {
let cbArr = events[name];
if (Array.isArray(cbArr)) {
cbArr.push({self, cb})
} else {
events[name] = [{self, cb}]
}
},
remove (name, self) {
let cbArr = events[name];
if (Array.isArray(cbArr)) {
events[name] = cbArr.filter(({target, cb}) => {
return target !== self
})
}
},
emit (name, data) {
let cbArr = events[name];
if (Array.isArray(cbArr)) {
cbArr.map(({target, cb}) => {
cb.call(target, data)
})
}
}
}

View File

@ -32,8 +32,8 @@
<view class="comp-title"><i class="icon-bookmark"></i> 最近在玩</view>
<view class="recent-game">
<repeat for="{{gameList}}" item="game">
<view class="one-game" @tap="gameTap({{game.image}})">
<image src="{{game.image}}"/>
<view class="one-game" @tap="gameTap({{game.gid}})">
<image src="{{game.icon}}"/>
</view>
</repeat>
</view>

View File

@ -85,6 +85,13 @@
},
bottomMainTap(e) {
console.log('bottomMainTap');
http.post('/api/emulated/play_game', {gid: this.id})
.then(res => {
console.log('success update game history');
})
.catch(err => {
console.log('err update game history');
})
},
bottomSecondTap(e) {
console.log('bottomSecondTap');

View File

@ -31,8 +31,7 @@
import recordCell from '../components/game-cell';
import recentGame from '../components/recent-game';
import zanLoadmore from '../components/zan-loadmore';
let sdkManage = require('../jcfw/SDKManage');
import jcEvent from '../common/jc-event';
export default class Index extends wepy.page {
mixins = [base, tips];
@ -89,18 +88,14 @@
}
async onLoad(options) {
sdkManage.init();
sdkManage.Login(() => {
let account = wepy.getStorageSync('account');
console.log(account);
//TODO: login
});
if (this.$parent.checkClientLogin()) {
wepy.navigateTo({
url: '/pages/login'
})
}
}
getAllData() {
this.getRecords();
this.getRecendGames();
}
onUnload() {
jcEvent.remove('login-finished', this);
}
onShow() {
let userInfo = this.$parent.getUserInfo();
@ -109,13 +104,34 @@
this.nickname = userInfo.nickName;
this.avatar = userInfo.avatarUrl;
}
if (this.$parent.checkAuthorize()) {
wepy.navigateTo({
url: '/pages/login'
})
}
if (!this.$parent.checkClientLogin()) {
this.getAllData();
} else {
jcEvent.on('login-finished', this, data => {
console.log('catch login-finished event');
this.getAllData()
});
}
}
initPageParam() {
this.all_count = 999;
this.current = 0;
this.records = [];
}
async getRecendGames() {
try {
let res = await http.post('/api/emulated/recent_games');
this.recent_game_list = res.records;
this.$apply();
} catch (err) {
console.log('error get recent games');
}
}
async getRecords() {
let self = this;
try {
@ -141,13 +157,6 @@
console.log(error);
self.loading = false
}
this.recent_game_list = [
{image: 'http://pub.hoh8.cn/files/game/fc/7002058/icon.jpg'},
{image: 'http://pub.hoh8.cn/files/game/fc/7002058/icon.jpg'},
{image: 'http://pub.hoh8.cn/files/game/fc/7002058/icon.jpg'},
{image: 'http://pub.hoh8.cn/files/game/fc/7002058/icon.jpg'},
{image: 'http://pub.hoh8.cn/files/game/fc/7002058/icon.jpg'}
];
this.$apply();
this.noData = false;
this.noMore = true;

View File

@ -26,11 +26,10 @@ const Ajax = (url, method, data, header) => {
header: header
}).then(res => {
console.log(res)
let data = res.data;
if (data.errcode === 0) {
resolve(data)
if (res.errcode === 0) {
resolve(res)
} else {
reject(data)
reject(res)
}
}, error => {
reject(error)