// pages/game/game.js const util = require('../../utils/util.js') const api = require('../../utils/api.js') const app = getApp() Page({ // 页面的初始数据 data: { first: true, scrollTop: 0, scrollTime: 0, redStyle: '', favStyle: '', auth: {}, authEnter: false, playGame: { count: 0, // 分类游戏数量 list: [], // 分类游戏列表 topads: { // 轮播图 count: 0, list: [] } }, system: '', // devtools:PC ios:苹果 android:安卓 ShowStoreUpMask: false, // 收藏 ShowRedPackMask: false, // 红包 ShowShareMask: false, // 分享 getSuccess: false, // 红包是否领取 ShowshareCirclesMask: false, // 小程序码 redAnimation: false, // 红包动画 shareImgSrc: '', userInfo: {}, shareInfo: {}, sharePath: 'pages/share/share', redIcon: '', redName: '', hideLoadingStatus: false, mpType: app.globalData.mpType }, // 生命周期函数--监听页面加载 onLoad: function(options) { this.getSystem() let timer = setInterval(() => { if (app.globalData.PageCallback) { this.PageInit() this.getShareInfo() clearInterval(timer) } }, 200) }, getShareInfo: function () { api.getShareInfo({ data: { path: this.data.sharePath } }).then(res => { console.log('getShare', res) this.setData({ shareInfo: res.data }) }).catch(err => { console.log('getShare', err) }) }, onPageScroll: function(res) { if (this.data.ShowStoreUpMask) { this.setData({ ShowStoreUpMask: false }) } let { scrollTime } = this.data if (scrollTime) { clearTimeout(scrollTime) } let _t = setTimeout(() => { this.setData({ redStyle: '', favStyle: res.scrollTop === 0 ? '' : 'slide-left' }) }, 1000) // 滑动则隐藏红包、收藏 this.setData({ redStyle: 'slide-right', favStyle: 'slide-left', scrollTop: res.scrollTop, scrollTime: _t }) }, PageInit: function() { this.setData({ userInfo: app.globalData.userInfo, auth: app.globalData.auth }) // 红包 console.log('game-onLoad-获取userInfo', app.globalData.userInfo) if (app.globalData.userInfo.get_novice_award === 1) { // 是否领取了新手奖励 1否 2是 if (app.globalData.auth.red === 2) { wx.hideTabBar({ animation: true }) api.getMpRedInfo().then(res => { this.setData({ redIcon: res.data.icon || '/image/logo.png', redName: res.data.name || app.globalData.mpName }) setTimeout(() => { this.setAnimation().then(() => { this.setData({ redAnimation: true }) }) }, 350) }) } else { this.setData({ authEnter: true }) wx.hideTabBar() } } }, routeAll: function(e) { let { title, type } = e.currentTarget.dataset; util.routeTo(`/pages/game/all?title=${title}&type=${type}`) }, // 获取用户信息 getUserInfoFun: function() { var _self = this; wx.getUserInfo({ success: function(res) { api.updateUserInfo({ raw_data: res.rawData, signature: res.signature, encrypted_data: res.encryptedData, iv: res.iv }).then(res => { if (res.code === 200) { if (res.data.is_old_member === 2) { wx.switchTab({ url: '/pages/user/index' }) _self.setData({ ShowRedPackMask: false }) wx.showTabBar({ animation: true }) } else { _self.setData({ // redPackAward: _self.data.mpType === 'gd' ? res.data.novice_award_integral : (res.data.novice_award * 1).toFixed(2), redPackAward: (res.data.novice_award * 1).toFixed(2), getSuccess: true }) // 更新余额 api.getUserInfo() setTimeout(() => { _self.setData({ userInfo: getApp().globalData.userInfo }) }, 500) } } else { wx.showToast({ title: res.msg, }) } }) _self.setData({ authEnter: false }) wx.showTabBar() }, fail: function(err) { console.log(err) } }) }, // 获取设备信息 getSystem: function(e) { const _self = this wx.getSystemInfo({ success: function(res) { _self.setData({ system: res }) } }) }, // 跳转赚钱技巧页 routeTips: function(e) { if (this.data.mpType === 'gd') { // util.routeTo('/pages/tips/tips') util.routeTo('/pages/lottery/lottery') } else { util.routeTo('/pages/user/purse') } }, // 显示隐藏收藏弹框 ChangeStoreUp: function(e) { this.setData({ ShowStoreUpMask: e.currentTarget.dataset.show ? true : false, favStyle: (e.currentTarget.dataset.show || this.data.scrollTop === 0) ? '' : 'slide-left' }) }, // 显示隐藏分享弹框 showShare: function(e) { let { show } = e.currentTarget.dataset if (!show) { wx.showTabBar({ animation: true }) } this.setData({ ShowShareMask: show ? true : false }) }, // 显示隐藏分享menu ChangeShare: function (e) { wx.hideTabBar({ animation: true }) if (!this.data.shareInfo.title) { api.getShareInfo({ data: { path: this.data.sharePath } }).then(res => { console.log('getShare', res) this.setData({ shareInfo: res.data, ShowShareMask: e.currentTarget.dataset.show ? true : false }) }).catch(err => { console.log('getShare', err) }) } else { this.setData({ ShowShareMask: e.currentTarget.dataset.show ? true : false }) } }, // 关闭红包弹框 CloseRedPack: function(e) { const _self = this _self.setData({ redAnimation: false }) setTimeout(() => { _self.setData({ ShowRedPackMask: false }) wx.showTabBar({ animation: true }) }, 300) }, // 设置红包动画 setAnimation: function(e) { const _self = this return new Promise((resolve, reject) => { _self.setData({ ShowRedPackMask: true }) resolve() }) }, // 关闭分享朋友圈弹框 CloseshareCircles: function() { wx.showTabBar({ aniamtion: true }) this.setData({ ShowshareCirclesMask: false }) }, // 分享朋友圈 shareCircles: function() { console.log(this.data.shareImgSrc) if (this.data.shareImgSrc) { this.setData({ ShowShareMask: false, ShowshareCirclesMask: true }) return false } wx.showLoading({ title: '正在生成', mask: true }) this.setData({ ShowShareMask: false, ShowshareCirclesMask: true }) const _self = this const rpx = this.data.system.windowWidth / 750 const ctx = wx.createCanvasContext('shareCanvas') const canvasW = 466 * rpx const canvasH = 828 * rpx const avatarW = 70 * rpx const avatarH = 70 * rpx const avatarX = 200 * rpx const avatarY = 19 * rpx const avatarBW = 66 * rpx const avatarBH = 66 * rpx const avatarBX = 200 * rpx const avatarBY = 20 * rpx const QRcodeW = 454 * rpx const QRcodeH = 454 * rpx const QRcodeX = 6 * rpx const QRcodeY = 374 * rpx const QRcodeBW = 147 * rpx const QRcodeBH = 147 * rpx const QRcodeBX = 160 * rpx const QRcodeBY = 580 * rpx util.getImageInfo(this.data.shareInfo.pyq_bg).then(res => { ctx.drawImage(res.path, 0, 0, canvasW, canvasH) ctx.save() ctx.beginPath() ctx.arc(avatarBW / 2 + avatarBX, avatarBH / 2 + avatarBY, avatarBW / 2, 0, Math.PI * 2, false) ctx.clip() ctx.setFillStyle('white') ctx.fillRect(avatarBX, avatarBY, avatarBW, avatarBH) ctx.restore() ctx.draw(true) ctx.save() ctx.beginPath() ctx.arc(QRcodeBW / 2 + QRcodeBX, QRcodeBH / 2 + QRcodeBY, QRcodeBW / 2, 0, Math.PI * 2, false) ctx.clip() ctx.setFillStyle('white') ctx.fillRect(QRcodeBX, QRcodeBY, QRcodeBW, QRcodeBH) ctx.restore() ctx.draw(true) }).then(res => { return util.getImageInfo(this.data.shareInfo.mp_qr).then(res => { ctx.save() ctx.beginPath() ctx.arc(QRcodeW / 2 + QRcodeX, QRcodeH / 2 + QRcodeY, QRcodeW / 2, 0, Math.PI * 2, false) ctx.clip() ctx.drawImage(res.path, canvasW / 2 - QRcodeW / 2, QRcodeY, QRcodeW, QRcodeH) ctx.restore() ctx.draw(true) }) }).then(res => { return util.getImageInfo(this.data.userInfo.avatar).then(res => { ctx.save() ctx.beginPath() ctx.arc(avatarW / 2 + avatarX, avatarH / 2 + avatarY, avatarW / 2, 0, Math.PI * 2, false) ctx.clip() ctx.drawImage(res.path, avatarX, avatarY, avatarW, avatarH) ctx.restore() ctx.draw(true) }) }).then(res => { setTimeout(() => { wx.canvasToTempFilePath({ x: 0, y: 0, width: canvasW, height: canvasH, destWidth: canvasW * 4, destHeight: canvasH * 4, canvasId: 'shareCanvas', success: function(res) { wx.hideLoading() _self.setData({ shareImgSrc: res.tempFilePath }) }, fail: function(res) { console.log(res) } }) }, 500) }).catch(err => { wx.hideLoading() console.log(err) }) }, // 保存分享朋友圈图片 saveMemQRcode: function(e) { const _self = this wx.showLoading({ title: '正在保存...' }) wx.saveImageToPhotosAlbum({ filePath: _self.data.shareImgSrc, success: function(res) { console.log('保存图片', res) wx.showToast({ title: '保存成功', icon: 'success', duration: 1000 }) }, fail: function(err) { console.log('保存图片', err) if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") { console.log("用户一开始拒绝了,我们想再次发起授权") console.log('打开设置窗口') wx.openSetting({ success(settingdata) { console.log(settingdata) if (settingdata.authSetting['scope.writePhotosAlbum']) { console.log('获取权限成功,给出再次点击图片保存到相册的提示。') } else { console.log('获取权限失败,给出不给权限就无法正常使用的提示') } } }) } }, complete: function() { wx.hideLoading() _self.setData({ ShowshareCirclesMask: false }) wx.showTabBar({ aniamtion: true }) } }) }, // 生命周期函数--监听页面初次渲染完成 onReady: function() {}, // 生命周期函数--监听页面显示 onShow: function(options) { wx.showShareMenu({ // 允许转发时携带 shareTicket。 withShareTicket: true }) let timer = setInterval(() => { if (app.globalData.PageCallback) { if (app.globalData.windowOpen && app.globalData.userInfo.get_favorite_award !== 2) { api.FavoriteRewardGet().then(res => { if (this.data.mpType === 'gd' && res.data.gain_integral && res.data.gain_integral > 0) { wx.showModal({ title: '收藏奖励', content: `恭喜获得${res.data.gain_integral}金币!`, showCancel: false }) } else if (this.data.mpType === 'rp' && res.data.gain_amount && res.data.gain_amount > 0) { wx.showModal({ title: '收藏奖励', content: `恭喜获得${res.data.gain_amount}元!`, showCancel: false }) } }, err => { }) } api.getPlayIndex({ HideLoading: this.data.hideLoadingStatus }).then(res => { this.setData({ playGame: res.data, hideLoadingStatus: true }) }, err => { console.log('getPlayIndex failed', err) }) if (!this.data.first) { api.getUserInfo().then(res => { this.setData({ userInfo: res.data, auth: res.data.auth }) }, err => { console.log(err) }) } else { this.setData({ first: false }) } clearInterval(timer) } }, 60) this.setData({ ShowStoreUpMask: false }) }, // 生命周期函数--监听页面隐藏 onHide: function() {}, // 生命周期函数--监听页面卸载 onUnload: function() {}, // 页面相关事件处理函数--监听用户下拉动作 onPullDownRefresh: function() { api.getPlayIndex({HideLoading: true}).then(res => { this.setData({ playGame: res.data }) }, err => { console.log('getPlayIndex failed', err) }) wx.stopPullDownRefresh() }, // 页面上拉触底事件的处理函数 onReachBottom: function() { }, openGame: function(e) { let id = e.currentTarget.dataset.id api.openGame({ game_id: id }).then(res => { console.log(res) }, err => { console.log(err) }) }, showPop: function (e) { wx.previewImage({ urls: [e.currentTarget.dataset.entrance] }) } })