// pages/home/home.js import NumberTimeChange from '../../utils/ntc.js' const app = getApp() var lock = require('../../utils/lock.js') var net = require('../../utils/net.js') var url = require('../../utils/url.js') var common = require('../../utils/common.js') Page({ /** * 页面的初始数据 */ data: { route: null, wxuser: {}, user: { cell: 0, playGameC: 0, withdrawalbleFee: 0 }, pageHideTime: 0, getRoomTime: 0, room: [], interval: null, rankInfo: {}, rankNumber: 0, rankDanName: '', addNum: 0, maxAddNum: 0, addTime: 12000, addTimeShow: '00:00:00', addDefTime: 12000, lastGameId: 0, brainCountDomRect: {}, goldCoinDomRect: {}, goldCoinAnim1: {}, goldCoinAnim2: {}, goldCoinAnim3: {}, goldCoinAnim4: {}, goldCoinAnim5: {}, goldCoinAnim6: {}, brainFillAnim: {}, brainFillAnimTime: 0, hideOpenRoomView: true, dialogGeneral: { hidden: true, title: '官方公告', content: '为保障发布内容的安全,冲顶王者不支持用户出题。\n冲顶王者内部建立了完备的内容审核机制,为用户精选每一道题。' }, dialogLogin: { hidden: true }, dialogBrainLack: { hidden: true, title: '哎呀,你的脑力值不足', subtitle: '无法参与娱乐赛,并争夺现金奖励', button: '分享到群 赚取100万脑力值', }, dialogRank: { hidden: true }, dialogFriend: { hidden: true }, dialogRoom: { hidden: true, title: '今天3次娱乐赛次数已用完', content: '你可以通过参加排位赛、提高段位来增加娱乐赛次数,段位越高、可玩次数越多! ', tip: '段位越高,翻倍奖励也越高哦', button: '现在就去参加排位赛', share: '', tap: 'toRank', }, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { app.globalData.isFirstLoad = true wx.showShareMenu({ withShareTicket: true }) if (options.recomCode) { app.globalData.identify.recomCode = options.recomCode app.globalData.identify.shareIdentify = options.shareIdentify delete options.recomCode delete options.shareIdentify } if (options.page) { var route = '../' + options.page + '/' + options.page + '?' delete options.page for (var key in options) { route += (key + '=' + options[key] + '&') } route = route.substring(0, route.length - 1) this.data.route = route } app.globalData.nextDayTime = common.getNextDayTime() // 登录 this.wxLogin() }, onShow: function () { //判断是否第一次加载 if (app.globalData.isFirstLoad) { app.globalData.isFirstLoad = false } else { this.setData({ user: app.globalData.user }) this.updateRankDanInfo() this.getUserDataByBack() this.getRoomList() if (this.data.rankInfo.title == null) { //若获取排位赛赛季信息失败,返回首页时重新获取 this.data.route = null this.getRankInfo() } } //判断是否存在隐藏页面时间,存在则倒计时减去差值 if (this.pageHideTime > 0) { var offset = Math.floor((new Date().getTime() - this.pageHideTime) / 1000) this.timeline(offset) this.pageHideTime = 0 } //页面显示时,启动倒计时定时器 this.interval = setInterval(function (data) { this.timeline(1) }.bind(this), 1000) }, onHide: function () { //页面隐藏时,停止倒计时定时器 if (this.interval) { clearInterval(this.interval) this.pageHideTime = new Date().getTime() } }, onUnload: function () { if (this.interval) { clearInterval(this.interval) } }, onReady: function () { var query = wx.createSelectorQuery() //获取脑力值节点中心位置 query.select('#brainCount').boundingClientRect(function (rect) { var x = Math.ceil(rect.left + rect.width / 2) var y = Math.ceil(rect.top + rect.height / 2) this.data.brainCountDomRect = { x: x, y: y } }.bind(this)).exec() //获取脑力加油站节点中心位置 query.select('.add-img').boundingClientRect(function (rect) { var x = Math.ceil(rect.left + rect.width / 2) var y = Math.ceil(rect.top + rect.height / 2) this.data.goldCoinDomRect = { x: x, y: y } }.bind(this)).exec() }, onShareAppMessage: function (res) { if ('menu' === res.from) { return common.shareAction({ title: this.data.wxuser.nickName + '正在用脑力征服世界并掠夺高额奖金哦', path: '', type: 1, success: res => { } }) } else { if (res.target.id === 'friend') { return common.shareAction({ title: this.data.wxuser.nickName + '向你发起脑力挑战,输的罚XXX!', path: '&page=pk&type=2&uid=' + app.server.userId, imageUrl: '../../resource/share_friend_pk.png', type: 8, success: res => { wx.navigateTo({ url: '/pages/pk/pk?type=2' }) } }) } else { this.hideBrainLackDialog() return common.shareAction({ title: this.data.wxuser.nickName + '邀请你来瓜分8万8千8百8十8元现金奖励!', path: '', type: 2, success: res => { this.getUserDataByBack() this.setData({ user: app.globalData.user }) if (res.data.data.addCell > 0) { wx.showToast({ title: '分享+' + common.unit(res.data.data.addCell) + '脑力值', icon: 'none', duration: 2500 }) } } }) } } }, route: function () { //路由操作 if (this.data.route) { wx.navigateTo({ url: this.data.route, }) this.data.route = null } }, wxLogin: function () { wx.showLoading({ title: '正在登录', mask: true, success: res => { app.onHomeStart(lock) } }) wx.login({ success: res => { // res.code 可以到后台换取 openId, sessionKey, unionId app.globalData.code = res.code //获取用户信息 this.getUserInfo() }, fail: e => { this.loginAgain() } }) }, loginAgain: function () { wx.hideLoading() wx.showModal({ title: '提示', content: '登录失败,请重试', showCancel: false, success: res => { this.wxLogin() } }) }, getUserInfo: function () { wx.getUserInfo({ success: res => { this.afterGetUserInfo(res) }, fail: e => { //获取用户信息失败,弹出登录框 wx.hideLoading() this.showLoginDialog() } }) }, bindGetUserInfo: function (res) { //登录弹框登录按钮的绑定函数 if (res.detail.userInfo == null) { return } //用户允许授权,隐藏登录弹框 this.hideLoginDialog() wx.showLoading({ title: '正在登录', }) this.afterGetUserInfo(res.detail) }, afterGetUserInfo(res) { app.onHomeReady(common) //用户允许授权后,汇总操作 app.globalData.userInfo = res.userInfo this.setData({ wxuser: res.userInfo }) //传递code 用户昵称 头像到服务端 执行登录操作 var state = '' if (app.globalData.identify.recomCode) { state = app.globalData.identify.shareIdentify + '#' + app.globalData.identify.recomCode } net.connect({ url: url.host + url.login, data: { code: app.globalData.code, rawData: res.rawData, signature: res.signature, state: state }, success: res => { wx.hideLoading() if (res.data.code != 200) { this.loginAgain() return } var _result = res.data.data var nowTime = _result.nowTime || new Date().getTime() app.globalData.timeDifference = nowTime - new Date().getTime() app.globalData.nextDayTime = common.getNextDayTime() app.server = _result this.getUserData() this.updateRankDanInfo() this.getRoomList() } }, true) }, getUserData: function () { net.connect({ url: url.host + url.get_user_info, success: res => { // 展示审核弹出框 var isOneShow = wx.getStorageSync('isOneShow') || false if (!isOneShow) { this.showGeneralDialog() wx.setStorage({ key: 'isOneShow', data: true, }) } else { var notice = app.server.userNotice if (notice != null) { var id = wx.getStorageSync('noticeId') || 0 if (id != notice.noticeId) { this.data.dialogGeneral.title = notice.title this.data.dialogGeneral.content = notice.cont this.showGeneralDialog() wx.setStorage({ key: 'noticeId', data: notice.noticeId, }) } } } if (res.data.code != 200) { return } this.updateUserData(res.data.data) this.getRankInfo() } }, true) }, getUserDataByBack: function () { net.connect({ url: url.host + url.get_user_info, success: res => { if (res.data.code == 200) { this.updateUserData(res.data.data) } } }, true) }, getRankInfo: function () { net.connect({ url: url.host + url.get_rank, success: res => { if (res.data.code == 200) { if (res.data.data != null) { this.data.rankInfo = res.data.data this.data.rankInfo.canPlayRank = true } this.route() } if (!this.data.rankInfo.canPlayRank) { this.data.rankInfo = { canPlayRank: false, logo: app.server.config.noRankLogo, title: app.server.config.noRankName } } app.globalData.rankInfo = this.data.rankInfo this.setData({ rankInfo: this.data.rankInfo }) } }, true) }, getRoomList: function () { if (app.server.key == null) { return; } net.connect({ url: url.host + url.get_room_list, success: res => { if (res.data.code != 200) { return } this.data.getRoomTime = 0 //服务器与本地时间差值计算 var nowTime = res.data.data.nowTime || new Date().getTime() app.globalData.timeDifference = nowTime - new Date().getTime() //计算倒计时时间 var rooms = res.data.data.roomList || [] var len = rooms.length || 0 for (var i = 0; i < len; i++) { var time = (rooms[i].playBeginTime - nowTime) / 1000 rooms[i].countDown = time > 0 ? Math.ceil(time) : 0 } this.setData({ room: rooms, hideOpenRoomView: false }) } }, true) }, timeline: function (offset) { if (app.server.key == null) { return } //页面倒计时,秒轮询 if (new Date().getTime() > app.globalData.nextDayTime) { //零点处理事件 this.resetShareNum() } //脑力加油站倒计时 var addTimeShow, addNum this.data.addTime = this.data.addTime - offset if (this.data.addTime > 0) { //计算当前增加脑力值,每分钟增加 var time = this.data.addDefTime - this.data.addTime time = Math.floor(time / 60) addNum = Math.floor(time * 60 * (this.data.maxAddNum / this.data.addDefTime)) addTimeShow = this.secondToHms(this.data.addTime) } else { addNum = this.data.maxAddNum this.data.addTime = 0 addTimeShow = '00:00:00' //脑力加油站达到最大值,执行动画 if (this.data.brainFillAnimTime == 0) { this.playBrainFillAnimation() } this.data.brainFillAnimTime = this.data.brainFillAnimTime >= 3 ? 0 : this.data.brainFillAnimTime + 1 } //房间倒计时 var rooms = this.data.room || [] var len = rooms.length || 0 for (var i = 0; i < len; i++) { var room = rooms[i] var time = room.countDown - offset room.countDown = time < 0 ? 0 : time } //数据更新 this.setData({ room: rooms, addNum: addNum, addTimeShow: addTimeShow }) //获取房间数据列表 if (this.data.getRoomTime >= app.server.config.schedRoomList) { this.data.getRoomTime = 0 this.getRoomList() } else { this.data.getRoomTime++ } }, resetShareNum: function () { app.server.shareCellLack = 0 app.server.shareRoomResult = 0 app.server.shareRoomResult2 = 0 app.server.shareRankPkResult = 0 app.server.shareRankCellLack = 0 app.globalData.nextDayTime = common.getNextDayTime() }, updateUserData: function (user) { this.data.user = user app.globalData.user = user var addTime = parseInt((this.data.user.nowTime - this.data.user.splitTime) / 1000) addTime = addTime > this.data.addDefTime ? this.data.addDefTime : addTime this.data.addTime = this.data.addDefTime - addTime; this.setData({ user: this.data.user }) }, updateRankDanInfo: function () { var arr = common.starToFragment(app.server.config.rankDanList, app.server.starC) var rankDan = app.server.config.rankDanList[arr[0]] this.data.rankDan = rankDan this.data.rankDanName = rankDan.name this.data.maxAddNum = rankDan.cellSplitMax this.data.addDefTime = rankDan.cellSplitMax / rankDan.cellSplitSpeed * 120 this.setData({ rankDanName: this.data.rankDanName, maxAddNum: this.data.maxAddNum, }) if (this.data.rankNumber != app.server.rank) { var ntc = new NumberTimeChange({ start: this.data.rankNumber, end: app.server.rank, update: n => { this.setData({ rankNumber: n }) }, complete: n => { this.setData({ rankNumber: n }) } }) } }, secondToHms: function (second) { //秒数转换成时分秒格式 var h, m, s h = Math.floor(second / 3600) h = h < 10 ? '0' + h : h m = Math.floor(second % 3600 / 60) m = m < 10 ? '0' + m : m s = Math.floor(second % 3600 % 60) s = s < 10 ? '0' + s : s return h + ':' + m + ':' + s }, getCurrentAddNum: function (e) { //点击金币,获取脑力值 if (this.data.addNum <= 0) { this.toAdd() return } if (lock.lockTapDelay(1000)) { return } net.connect({ url: url.host + url.receive_cell, method: 'POST', data: { formId: e.detail.formId, }, success: res => { this.playGoldCoinAnimation() this.data.user.cell = this.data.user.cell + res.data.data app.globalData.user.cell = this.data.user.cell this.setData({ addNum: 0, addTime: this.data.addDefTime, addTimeShow: '03:20:00' }) setTimeout(function () { this.setData({ user: this.data.user }) }.bind(this), 1200) } }) }, playBrainFillAnimation: function () { var animation = wx.createAnimation() animation.opacity(1).step({ duration: 1400 }) animation.opacity(0).step({ duration: 1400 }) this.setData({ brainFillAnim: animation.export() }) }, playGoldCoinAnimation: function () { //播放获取金币动画 var rect = this.data.brainCountDomRect var rect2 = this.data.goldCoinDomRect var targetX = rect.x - rect2.x var targetY = rect.y - rect2.y var animations = new Array(6) for (var i = 0; i < 6; i++) { var animation = wx.createAnimation() var x = -60 + i * 25 var y = 42 + (i % 2) * (13 - i) animation.translate(0, 0).step({ duration: 50 }) animation.opacity(1).translate(x, y).scale(1.5).rotate3d(1, 0, 0, 0).step({ duration: 500, timingFunction: 'ease' }) animation.opacity(0).translate(targetX, targetY).scale(1).rotate3d(0, 0, 0, 0).step({ duration: i * 120, timingFunction: 'ease-out' }) animations[i] = animation } this.setData({ goldCoinAnim1: animations[0].export(), goldCoinAnim2: animations[1].export(), goldCoinAnim3: animations[2].export(), goldCoinAnim4: animations[3].export(), goldCoinAnim5: animations[4].export(), goldCoinAnim6: animations[5].export() }) }, toCash: function () { if (lock.lockTapDelay()) { return } wx.navigateTo({ url: '../cash/cash' }) }, toRoom: function (e) { if (lock.lockTapDelay()) { return } var room = this.data.room[e.currentTarget.id] if (room.curUser >= room.maxUser || room.countDown <= 0) { wx.showToast({ title: '此房间已爆满,无法进入', icon: 'none' }) return } if (this.data.lastGameId != room.gameId) { if (this.data.user.cell < this.data.rankDan.cellRoomCost) { if (app.server.shareCellLack < app.server.config.shareCellLackMax) { this.showBrainLackDialog() } else { wx.showToast({ title: '脑力值不足', icon: 'none' }) } return } var arr = common.starToFragment(app.server.config.rankDanList, app.server.starC) var rankDan = app.server.config.rankDanList[arr[0]] var limit = rankDan.roomLimit || 3 if (app.server.playRoom >= limit){ this.data.dialogRoom.title = '今天' + limit +'次娱乐赛次数已用完' this.showRoomDialog() return } } this.data.lastGameId = room.gameId wx.navigateTo({ url: '../room/room?gameId=' + room.gameId }) }, toOpen: function (event) { if (lock.lockTapDelay()) { return } wx.navigateTo({ url: '../open/open' }) }, toRule: function () { if (lock.lockTapDelay()) { return } wx.navigateTo({ url: '../rules/rules' }) }, toRank: function () { if (lock.lockTapDelay()) { return } this.hideRoomDialog() if (this.data.rankInfo.canPlayRank) { wx.navigateTo({ url: '../fragmentsList/fragmentsList' }) } else { this.showRankDialog() } }, toAdd: function () { if (lock.lockTapDelay()) { return } wx.navigateTo({ url: '../add/add' }) }, wxvoid: function () { }, showBrainLackDialog: function () { this.data.dialogBrainLack.hidden = false this.data.dialogBrainLack.button = '分享到群 赚取' + common.unit(this.data.maxAddNum) + '脑力值' this.setData({ dialogBrainLack: this.data.dialogBrainLack }) }, hideBrainLackDialog: function () { this.data.dialogBrainLack.hidden = true this.setData({ dialogBrainLack: this.data.dialogBrainLack }) }, showLoginDialog: function () { this.setData({ dialogLogin: { hidden: false } }) }, hideLoginDialog: function () { this.setData({ dialogLogin: { hidden: true } }) }, showRankDialog: function () { this.setData({ dialogRank: { hidden: false, headImg: '../../resource/head_rank_no.png' } }) }, hideRankDialog: function () { this.setData({ dialogRank: { hidden: true } }) }, showGeneralDialog: function () { this.data.dialogGeneral.hidden = false this.setData({ dialogGeneral: this.data.dialogGeneral }) }, hideGeneralDialog: function () { this.setData({ dialogGeneral: { hidden: true } }) }, showRoomDialog: function () { this.data.dialogRoom.hidden = false this.setData({ dialogRoom: this.data.dialogRoom }) }, hideRoomDialog: function () { this.data.dialogRoom.hidden = true this.setData({ dialogRoom: this.data.dialogRoom }) }, })