实现最近游戏相关功能

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> <script>
import wepy from 'wepy' import wepy from 'wepy'
import 'wepy-async-function' 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 { export default class extends wepy.app {
config = { config = {
pages: [ pages: [
@ -36,34 +39,105 @@ export default class extends wepy.app {
} }
globalData = { globalData = {
userInfo: null userInfo: null,
gameToken: null
} }
constructor () { constructor () {
super(); super();
this.use('requestfix'); this.use('requestfix');
this.use('promisify'); 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() { onLaunch() {
} }
getUserInfo() { getUserInfo() {
return this.globalData.userInfo return this.globalData.userInfo
} }
checkClientLogin() { checkAuthorize() {
let self = this; let self = this;
let needLogin = true; let needAuthorize = true;
if (!self.globalData.userInfo) { if (!self.globalData.userInfo) {
if (wepy.getStorageSync('userInfo')) { if (wepy.getStorageSync('userInfo')) {
self.globalData.userInfo = JSON.parse(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; needLogin = false;
} }
} else { } else {
needLogin = false; needLogin = false;
} }
return needLogin; return needLogin;
// return true;
} }
updateGlobalData(name, obj) { updateGlobalData(name, obj) {
// 校验: globalData // 校验: 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="comp-title"><i class="icon-bookmark"></i> 最近在玩</view>
<view class="recent-game"> <view class="recent-game">
<repeat for="{{gameList}}" item="game"> <repeat for="{{gameList}}" item="game">
<view class="one-game" @tap="gameTap({{game.image}})"> <view class="one-game" @tap="gameTap({{game.gid}})">
<image src="{{game.image}}"/> <image src="{{game.icon}}"/>
</view> </view>
</repeat> </repeat>
</view> </view>

View File

@ -85,6 +85,13 @@
}, },
bottomMainTap(e) { bottomMainTap(e) {
console.log('bottomMainTap'); 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) { bottomSecondTap(e) {
console.log('bottomSecondTap'); console.log('bottomSecondTap');

View File

@ -31,8 +31,7 @@
import recordCell from '../components/game-cell'; import recordCell from '../components/game-cell';
import recentGame from '../components/recent-game'; import recentGame from '../components/recent-game';
import zanLoadmore from '../components/zan-loadmore'; import zanLoadmore from '../components/zan-loadmore';
import jcEvent from '../common/jc-event';
let sdkManage = require('../jcfw/SDKManage');
export default class Index extends wepy.page { export default class Index extends wepy.page {
mixins = [base, tips]; mixins = [base, tips];
@ -89,18 +88,14 @@
} }
async onLoad(options) { async onLoad(options) {
sdkManage.init();
sdkManage.Login(() => { }
let account = wepy.getStorageSync('account'); getAllData() {
console.log(account);
//TODO: login
});
if (this.$parent.checkClientLogin()) {
wepy.navigateTo({
url: '/pages/login'
})
}
this.getRecords(); this.getRecords();
this.getRecendGames();
}
onUnload() {
jcEvent.remove('login-finished', this);
} }
onShow() { onShow() {
let userInfo = this.$parent.getUserInfo(); let userInfo = this.$parent.getUserInfo();
@ -109,13 +104,34 @@
this.nickname = userInfo.nickName; this.nickname = userInfo.nickName;
this.avatar = userInfo.avatarUrl; 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() { initPageParam() {
this.all_count = 999; this.all_count = 999;
this.current = 0; this.current = 0;
this.records = []; 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() { async getRecords() {
let self = this; let self = this;
try { try {
@ -141,13 +157,6 @@
console.log(error); console.log(error);
self.loading = false 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.$apply();
this.noData = false; this.noData = false;
this.noMore = true; this.noMore = true;

View File

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