game/src/pages/index.wpy
2019-04-04 09:53:02 +08:00

550 lines
18 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/index.wxss";
.zan-btn--small {
padding-left: 10px;
padding-right: 10px;
}
</style>
<template>
<view class="container">
<view class="top-view">
<view class="userinfo">
<view class="userinfo-user">
<image class="userinfo-avatar" src="{{avatar}}"></image>
<view class="userinfo-nickname">{{ nickname }}</view>
</view>
<view class="vip-view {{showAll? '' : 'hidden'}}">
<navigator class="vip-btn zan-btn zan-btn--small zan-btn--primary zan-btn--plain button-hover {{showBuy ? '' : 'hidden'}}"
target="miniProgram"
open-type="navigate"
app-id="wx815bf59d472c0a63"
path="/pages/buyvip?{{vip_params}}"
extra-data=""
@tap = 'vipBtnTap'
version="{{payVer}}"
>
{{vip_btn_title}}</navigator>
<image src="{{vipImg}}"></image>
<text class="vip-expire-txt">{{expire_str}}</text>
</view>
</view>
<view class="user-info-ext {{(showAll && !vip)? '' : 'hidden'}}">
<view class="info-part">
<view class="info-part-top">拥有游戏: <text class="info">{{my_game_list.length}}</text></view>
<view class="zan-btn zan-btn--small zan-btn--plain zan-btn--warn button-hover" @tap="showInviteView">免费获取游戏</view>
</view>
<view class="info-part">
<view class="info-part-top">金币: <text class="info">{{score}}</text></view>
<view class="zan-btn zan-btn--small zan-btn--plain zan-btn--warn button-hover" @tap="toGetMoney">免费获取金币</view>
</view>
</view>
</view>
<myGame class="{{(showAll && showMyGame)? '' : 'hidden'}}" :idx.sync="recentCurrent" :titleList.sync="myGameTitles"
:gameList.sync="myGameRecords" @recommendTap.user="recommendTap"
@gameTap.user="gameTap" @selectTitle.user="barListTitleChange"></myGame>
<recommendGame class="{{(showAll && showRecommend)? '' : 'hidden'}}" :gameList.sync="recommendGames"
:titleList.sync = "recommendTitles" @gameTap.user="gameTap" @recommendTap.user="recommendTap"></recommendGame>
<view class="list-title">
<view class="left"><i class="icon-bookmark"></i>热门游戏</view>
<view class="right" @tap="toSearch"><i class="icon-search"></i> 搜索</view>
</view>
<view class="record-list">
<repeat for="{{records}}" item="item" >
<recordCell :item="item" @gameCellTap.user="gameTap" :showAll.sync = 'showAll'/>
</repeat>
<zanLoadmore :loading.sync="loading" :nodata.sync="noData" :nomore.sync="noMore" nodata_str="暂无数据"></zanLoadmore>
</view>
<toast/>
<inviteView :config.sync="inviteViewCfg" :btnList.sync="inviteBtns" @inviteBtnClick.user="inviteBtnClick" @needHideMe.user="hideInviteView"></inviteView>
<zanToptips/>
</view>
</template>
<script>
import wepy from 'wepy';
import Toast from 'wepy-com-toast';
import http from '../utils/http';
import recordCell from '../components/game-cell';
import recentGame from '../components/recent-game';
import zanLoadmore from '../components/zan-loadmore';
import inviteView from '../components/invite-view';
import zanToptips from '../components/zan-toptips'
import jcEvent from '../common/jc-event';
import image from '../common/images';
import global from '../common/global';
import base from '../mixins/base';
import tips from '../mixins/tips';
import cfg from '../mixins/cfg';
import nav from '../mixins/nav';
export default class Index extends wepy.page {
mixins = [base, tips, cfg, nav];
config = {
navigationBarTitleText: '游戏大厅',
enablePullDownRefresh: true,
backgroundTextStyle: 'dark'
};
components = {
toast: Toast,
recordCell: recordCell,
recommendGame: recentGame,
myGame: recentGame,
zanLoadmore: zanLoadmore,
inviteView: inviteView,
zanToptips: zanToptips
};
data = {
nickname: '加载中',
avatar: '',
score: 0,
records: [],
recent_game_list: [],
my_game_list: [],
all_count: 0,
current: 0,
loading: false,
noData: true,
noMore: false,
showAll: false,
showMyGame: false,
myGameRecords: [],
recommendGames: [],
showRecommend: false,
vip: false,
expire_str: '',
vipImg: image.vip_s,
vip_btn_title: '激活VIP, 畅玩所有游戏',
vip_params: '',
showBuy: false,
payVer: 'develop',
achivement: false,
inviteBtns: [],
inviteViewCfg: {
hide: true
},
myGameTitles: [],
recommendTitles: [],
recentCurrent: 0,
};
methods = {
gameTap(gid, e) {
this.$parent.log('game_tap', {gid: gid});
this.$toGameInfo(gid);
},
async toSearch() {
// wepy.navigateTo({
// url: '/pages/search'
// })
let movie = 'http://iqiyi.kuyun-bofang.com/20190208/eebojrT3/index.m3u8'
this.$toMovie(movie, '流浪地球');
},
async showInviteView() {
await this.updateInviteViewData();
this.inviteViewCfg.hide = false;
this.$apply();
this.$parent.log('show_invite_btn', {});
},
hideInviteView() {
this.inviteViewCfg.hide = true;
this.$apply();
},
inviteBtnClick(index) {
console.log(`invite btn clicked: ${index}`);
this.getRandomGame();
this.$parent.log('get_random_btn', {});
},
vipBtnTap() {
this.$parent.log('buy_vip_btn', {});
},
barListTitleChange(idx) {
this.recentCurrent = idx;
if (idx === 1) {
this.myGameRecords = this.recent_game_list;
} else {
this.myGameRecords = this.my_game_list;
}
this.$apply();
},
recommendTap(url) {
wepy.navigateTo({
url: '/pages/game?link=' + encodeURIComponent(url) + '&gid=0&category=ad'
});
},
toGetMoney() {
this.$parent.log('main_get_coin_btn', {});
this.requestAd('');
}
};
onPullDownRefresh() {
this.initPageParam();
this.getAllData();
}
onReachBottom() {
if (this.current < this.all_count) {
this.loading = true;
this.$apply();
let self = this;
setTimeout(() => {
self.getRecords();
}, 200);
}
}
async onLoad(options) {
console.log('index.onload, is logined: ' + this.$parent.checkJcfwLogin());
console.log(options);
this.myGameTitles = [];
this.recommendTitles = ['推荐'];
this.$apply();
this.$parent.log('index_onload', options);
let self = this;
wepy.showShareMenu({
withShareTicket: true
});
this.payVer = global.env === 'product' ? 'release' : 'develop';
if (this.$parent.checkJcfwLogin()) {
this.joinRoomPlay(options);
}
if (!this.$parent.checkClientLogin()) {
this.getAllData();
this.vip_params = `token=${this.$parent.getGlobalDate('gameToken')}`;
} else {
jcEvent.on(jcEvent.events.LOGIN_FINISHED, this, data => {
this.getAllData()
this.vip_params = `token=${this.$parent.getGlobalDate('gameToken')}`;
});
}
jcEvent.on(jcEvent.events.UPDATE_RECENT_GAMES, this, data => {
this.getRecentGames()
});
jcEvent.on(jcEvent.events.UPDATE_GAME_STATUS, this, data => {
for(let g of self.records) {
if (g.gid == data.gid) {
g.owned = true;
break;
}
}
this.getMyGames();
self.$apply();
});
jcEvent.on(jcEvent.events.NEED_UPDATE_SCORE, this, data => {
this.getUserInfo();
})
jcEvent.on(jcEvent.events.NEED_UPDATE_CFG, this, data => {
this.showAll = this.$showAll();
this.showBuy = !!this.vip_btn_title && this.$showBuy();
this.$apply();
this.joinRoomPlay(options);
})
jcEvent.on(jcEvent.events.BUY_VIP_RESULT, this, data => {
if (data.success) {
wepy.showModal({
content: '购买成功',
showCancel: false
});
this.getUserInfo();
this.$parent.log('buy_vip_success', data);
} else {
this.showTopTips('购买失败')
this.$parent.log('buy_vip_fail', data);
}
})
}
getAllData() {
this.getRecords();
this.getRecentGames();
this.getUserInfo();
this.getMyGames();
this.getRecommendGames();
}
onUnload() {
jcEvent.remove(jcEvent.events.LOGIN_FINISHED, this);
jcEvent.remove(jcEvent.events.UPDATE_RECENT_GAMES, this);
jcEvent.remove(jcEvent.events.UPDATE_GAME_STATUS, this);
jcEvent.remove(jcEvent.events.NEED_UPDATE_SCORE, this);
jcEvent.remove(jcEvent.events.NEED_UPDATE_CFG, this);
jcEvent.remove(jcEvent.events.BUY_VIP_RESULT, this);
}
onShow(options) {
console.log('onshow');
console.log(options);
this.$parent.log('index_onshow', {});
let cfg = wepy.getStorageSync('cfg');
if (!cfg._getrewardtype) {
wepy.hideShareMenu();
} else {
wepy.showShareMenu({
withShareTicket: true
});
}
this.showAll = this.$showAll();
let userInfo = this.$parent.getUserInfo();
if (userInfo) {
this.nickname = userInfo.nickName;
this.avatar = userInfo.avatarUrl;
this.$apply();
}
(this.$parent.checkAuthorize()) && this.$toLogin();
}
initPageParam() {
this.all_count = 999;
this.current = 0;
this.records = [];
}
/**
* 处理加入多人游戏的逻辑
* 如果超时5分钟则直接提示超时
* 否则加入房间
* */
joinRoomPlay(options) {
if (options.togame) {
let timestamp = options.t;
let now = Date.now();
if (now - timestamp > 60*1000*5) {
this.showTopTips('加入房间失败')
this.$parent.log('join_room_fail', {reason: 'timeout'});
} else {
let cfg = wepy.getStorageSync('cfg');
let fcUrl = cfg.fcurl;
let roomId = options.roomId;
let gid = options.gid;
let link = `${fcUrl}?id=${gid}&roomId=${roomId}`;
this.$parent.log('join_room_success', {gid,roomId});
wepy.navigateTo({
url: '/pages/game?link=' + encodeURIComponent(link) + '&gid=' + gid +'&name=multiplayer&category=fc'
});
}
}
}
prepareTopGameBar() {
if (this.my_game_list.length > 0 && !this.vip && this.recent_game_list.length > 0) {
this.myGameRecords = this.my_game_list;
this.showMyGame = true;
this.myGameTitles = ['我的游戏', '最近在玩'];
} else if ((this.my_game_list.length === 0 || this.vip) && this.recent_game_list.length > 0) {
this.myGameRecords = this.recent_game_list;
this.showMyGame = true;
this.myGameTitles = ['最近在玩'];
} else if (this.my_game_list.length > 0 && !this.vip && this.recent_game_list.length === 0){
this.myGameRecords = this.my_game_list;
this.showMyGame = true;
this.myGameTitles = ['我的游戏'];
} else {
this.showMyGame = false;
}
if (this.myGameTitles.length > 1) {
this.recentCurrent = 0;
}
}
showTopTips(msg, type = 'error') {
this.$invoke('zanToptips', 'showZanTopTips', { content: msg, type: type})
}
async getRecentGames() {
try {
let res = await http.post('/api/emulated/recent_games');
this.recent_game_list = res.records;
this.prepareTopGameBar();
this.$apply();
} catch (err) {
console.log('error get recent games');
}
}
async getRecommendGames() {
try {
let res = await http.post('/api/emulated/recommend_game');
this.recommendGames = res.records;
this.showRecommend = this.recommendGames.length > 0;
this.$apply();
} catch (err) {
console.log('error get recommend games');
}
}
async getRandomGame() {
let self = this;
try {
let res = await http.post('/api/emulated/give_me_game');
await this.updateInviteViewData();
let modal = await wepy.showModal({
title: '领取成功',
content: `您已成功领取: ${res.record.name}`,
confirmText: '立即去玩',
cancelText: '继续'
});
if (modal.confirm) {
self.$toGameInfo(res.record.gid);
}
} catch (err) {
console.log('error get recent games');
}
}
async myAchieveGames() {
let res = await http.post('/api/emulated/my_achieve_game');
return res.records;
}
async getMyGames() {
try {
let res = await http.post('/api/emulated/my_games');
this.my_game_list = res.records;
this.prepareTopGameBar();
this.$apply();
wepy.setStorageSync('game_count', this.my_game_list.length);
} catch (err) {
console.log('error get recent games');
}
}
async getUserInfo() {
try {
let res = await http.post('/api/emulated/user_info', {type: 'all'});
if (res.userInfo) {
let user = res.userInfo;
this.score = user.score;
this.vip = user.vip;
this.$parent.updateVip(user.vip);
if (user.vip) {
this.vipImg = image.vip;
if (user.vip_expire_day === 99999) {
this.expire_str = '(永久)';
this.vip_btn_title = '';
} else if (user.vip_expire_day === 0){
this.expire_str = '(不到1天)';
this.vip_btn_title = '续费';
} else {
this.expire_str = `(${user.vip_expire_day}天)`;
this.vip_btn_title = '续费';
}
} else {
this.vip_btn_title = '激活VIP, 畅玩所有游戏';
this.vipImg = image.vip_s;
}
this.showBuy = !!this.vip_btn_title && this.$showBuy();
}
this.$apply();
} catch (err) {
console.log('error get user info');
this.showTopTips('获取用户信息失败');
}
}
async getRecords() {
let self = this;
try {
let res = await http.post(`/api/emulated/games`, {start: self.current});
wepy.stopPullDownRefresh();
if (res.errcode === 0) {
if (res.count > 0) {
self.all_count = res.count;
}
self.current += 10;
for (let obj of res.records) {
(obj.open) && (obj.owned = true);
if (obj.images && obj.images.length > 0) {
obj.icon = obj.images[0];
}
self.records.push(obj);
}
self.loading = false;
self.noMore = (self.records.length >= self.all_count);
self.noData = (self.records.length === 0);
self.$apply();
} else {
this.showTopTips('获取游戏列表失败');
self.loading = false
}
} catch (error) {
console.log(error);
this.showTopTips('获取游戏列表失败');
self.loading = false
}
this.$apply();
this.noData = false;
this.noMore = true;
}
async updateInviteViewData() {
let userList = [];
let gameList = [];
this.inviteBtns = [];
try {
userList = await this.$parent.getShareDetail();
gameList = await this.myAchieveGames();
} catch (err) {
console.log('error get invite user list');
}
let achieveArr = this.$getAchieveShareCfg();
const userCount = userList.length;
for (let i = 0; i < achieveArr.length; i ++ ) {
let btnTitle = '邀请';
let avatar = '';
let status = 0;
let contentTxt = '邀请好友获得游戏';
if (achieveArr[i] <= userCount) {
btnTitle = '领取';
avatar = userList[i].avatar;
status = 1;
contentTxt = '点击领取游戏'
}
if (i < gameList.length) {
status = 2;
contentTxt = '已领取';
btnTitle = '已领取'
}
this.inviteBtns.push({
index: achieveArr[i],
btnTitle: btnTitle,
contentTxt: contentTxt,
avatar: avatar,
status: status,
});
}
this.$apply();
}
async requestAd(text) {
try {
let res = await http.post('/api/emulated/request_ad', {});
if (res.errcode === 0) {
wepy.navigateTo({
url: `/pages/jumppage?text=${text}&aid=${res.aid}`
});
} else {
console.log('request ad with errmsg: ' + res.errmsg);
}
} catch (err) {
console.log(err);
}
}
onShareAppMessage() {
let account = wepy.getStorageSync('account');
let shareObj = this.$getShareCfg();
let shareUrl = `/pages/index?isShare=1&inviter_id=${account.account_id}`;
if (this.achivement) {
shareUrl += '&shareType=achivement'
}
this.$parent.log('onShareAppMessage', {page: 'index', shareUrl: shareUrl});
return {
title: shareObj.str,
path: shareUrl,
imageUrl: shareObj.image,
success: function (res) {
wepy.getShareInfo({
shareTicket: res.shareTickets[0]
})
.then(res => {
console.log('分享成功');
})
.catch(err => {
console.log(err)
})
},
fail: function (res) {
// 分享失败
console.log(res)
}
}
}
}
</script>