game.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // pages/game/game.js
  2. const util = require('../../utils/util.js')
  3. const api = require('../../utils/api.js')
  4. const app = getApp()
  5. Page({
  6. // 页面的初始数据
  7. data: {
  8. first: true,
  9. scrollTop: 0,
  10. scrollTime: 0,
  11. redStyle: '',
  12. favStyle: '',
  13. auth: {},
  14. authEnter: false,
  15. playGame: {
  16. count: 0, // 分类游戏数量
  17. list: [], // 分类游戏列表
  18. topads: { // 轮播图
  19. count: 0,
  20. list: []
  21. }
  22. },
  23. system: '', // devtools:PC ios:苹果 android:安卓
  24. ShowStoreUpMask: false, // 收藏
  25. ShowRedPackMask: false, // 红包
  26. ShowShareMask: false, // 分享
  27. getSuccess: false, // 红包是否领取
  28. ShowshareCirclesMask: false, // 小程序码
  29. redAnimation: false, // 红包动画
  30. shareImgSrc: '',
  31. userInfo: {},
  32. shareInfo: {},
  33. sharePath: 'pages/share/share',
  34. redIcon: '',
  35. redName: '',
  36. hideLoadingStatus: false,
  37. mpType: app.globalData.mpType
  38. },
  39. // 生命周期函数--监听页面加载
  40. onLoad: function(options) {
  41. this.getSystem()
  42. let timer = setInterval(() => {
  43. if (app.globalData.PageCallback) {
  44. this.PageInit()
  45. this.getShareInfo()
  46. clearInterval(timer)
  47. }
  48. }, 200)
  49. },
  50. getShareInfo: function () {
  51. api.getShareInfo({
  52. data: {
  53. path: this.data.sharePath
  54. }
  55. }).then(res => {
  56. console.log('getShare', res)
  57. this.setData({
  58. shareInfo: res.data
  59. })
  60. }).catch(err => {
  61. console.log('getShare', err)
  62. })
  63. },
  64. onPageScroll: function(res) {
  65. if (this.data.ShowStoreUpMask) {
  66. this.setData({
  67. ShowStoreUpMask: false
  68. })
  69. }
  70. let { scrollTime } = this.data
  71. if (scrollTime) {
  72. clearTimeout(scrollTime)
  73. }
  74. let _t = setTimeout(() => {
  75. this.setData({
  76. redStyle: '',
  77. favStyle: res.scrollTop === 0 ? '' : 'slide-left'
  78. })
  79. }, 1000)
  80. // 滑动则隐藏红包、收藏
  81. this.setData({
  82. redStyle: 'slide-right',
  83. favStyle: 'slide-left',
  84. scrollTop: res.scrollTop,
  85. scrollTime: _t
  86. })
  87. },
  88. PageInit: function() {
  89. this.setData({
  90. userInfo: app.globalData.userInfo,
  91. auth: app.globalData.auth
  92. })
  93. // 红包
  94. console.log('game-onLoad-获取userInfo', app.globalData.userInfo)
  95. if (app.globalData.userInfo.get_novice_award === 1) {
  96. // 是否领取了新手奖励 1否 2是
  97. if (app.globalData.auth.red === 2) {
  98. wx.hideTabBar({
  99. animation: true
  100. })
  101. api.getMpRedInfo().then(res => {
  102. this.setData({
  103. redIcon: res.data.icon || '/image/logo.png',
  104. redName: res.data.name || app.globalData.mpName
  105. })
  106. setTimeout(() => {
  107. this.setAnimation().then(() => {
  108. this.setData({
  109. redAnimation: true
  110. })
  111. })
  112. }, 350)
  113. })
  114. } else {
  115. this.setData({
  116. authEnter: true
  117. })
  118. wx.hideTabBar()
  119. }
  120. }
  121. },
  122. routeAll: function(e) {
  123. let {
  124. title,
  125. type
  126. } = e.currentTarget.dataset;
  127. util.routeTo(`/pages/game/all?title=${title}&type=${type}`)
  128. },
  129. // 获取用户信息
  130. getUserInfoFun: function() {
  131. var _self = this;
  132. wx.getUserInfo({
  133. success: function(res) {
  134. api.updateUserInfo({
  135. raw_data: res.rawData,
  136. signature: res.signature,
  137. encrypted_data: res.encryptedData,
  138. iv: res.iv
  139. }).then(res => {
  140. if (res.code === 200) {
  141. _self.setData({
  142. redPackAward: _self.data.mpType === 'gd' ? res.data.novice_award_integral : (res.data.novice_award * 1).toFixed(2),
  143. getSuccess: true
  144. })
  145. // 更新余额
  146. api.getUserInfo()
  147. setTimeout(() => {
  148. _self.setData({
  149. userInfo: getApp().globalData.userInfo
  150. })
  151. }, 500)
  152. } else {
  153. wx.showToast({
  154. title: res.msg,
  155. })
  156. }
  157. })
  158. _self.setData({
  159. authEnter: false
  160. })
  161. wx.showTabBar()
  162. },
  163. fail: function(err) {
  164. console.log(err)
  165. }
  166. })
  167. },
  168. // 获取设备信息
  169. getSystem: function(e) {
  170. const _self = this
  171. wx.getSystemInfo({
  172. success: function(res) {
  173. _self.setData({
  174. system: res
  175. })
  176. }
  177. })
  178. },
  179. // 跳转赚钱技巧页
  180. routeTips: function(e) {
  181. if (this.data.mpType === 'gd') {
  182. // util.routeTo('/pages/tips/tips')
  183. util.routeTo('/pages/lottery/lottery')
  184. } else {
  185. util.routeTo('/pages/user/purse')
  186. }
  187. },
  188. // 显示隐藏收藏弹框
  189. ChangeStoreUp: function(e) {
  190. this.setData({
  191. ShowStoreUpMask: e.currentTarget.dataset.show ? true : false,
  192. favStyle: (e.currentTarget.dataset.show || this.data.scrollTop === 0) ? '' : 'slide-left'
  193. })
  194. },
  195. // 显示隐藏分享弹框
  196. showShare: function(e) {
  197. let { show } = e.currentTarget.dataset
  198. if (!show) {
  199. wx.showTabBar({
  200. animation: true
  201. })
  202. }
  203. this.setData({
  204. ShowShareMask: show ? true : false
  205. })
  206. },
  207. // 显示隐藏分享menu
  208. ChangeShare: function (e) {
  209. wx.hideTabBar({
  210. animation: true
  211. })
  212. if (!this.data.shareInfo.title) {
  213. api.getShareInfo({
  214. data: {
  215. path: this.data.sharePath
  216. }
  217. }).then(res => {
  218. console.log('getShare', res)
  219. this.setData({
  220. shareInfo: res.data,
  221. ShowShareMask: e.currentTarget.dataset.show ? true : false
  222. })
  223. }).catch(err => {
  224. console.log('getShare', err)
  225. })
  226. } else {
  227. this.setData({
  228. ShowShareMask: e.currentTarget.dataset.show ? true : false
  229. })
  230. }
  231. },
  232. // 关闭红包弹框
  233. CloseRedPack: function(e) {
  234. const _self = this
  235. _self.setData({
  236. redAnimation: false
  237. })
  238. setTimeout(() => {
  239. _self.setData({
  240. ShowRedPackMask: false
  241. })
  242. wx.showTabBar({
  243. animation: true
  244. })
  245. }, 300)
  246. },
  247. // 设置红包动画
  248. setAnimation: function(e) {
  249. const _self = this
  250. return new Promise((resolve, reject) => {
  251. _self.setData({
  252. ShowRedPackMask: true
  253. })
  254. resolve()
  255. })
  256. },
  257. // 关闭分享朋友圈弹框
  258. CloseshareCircles: function() {
  259. wx.showTabBar({
  260. aniamtion: true
  261. })
  262. this.setData({
  263. ShowshareCirclesMask: false
  264. })
  265. },
  266. // 分享朋友圈
  267. shareCircles: function() {
  268. console.log(this.data.shareImgSrc)
  269. if (this.data.shareImgSrc) {
  270. this.setData({
  271. ShowShareMask: false,
  272. ShowshareCirclesMask: true
  273. })
  274. return false
  275. }
  276. wx.showLoading({
  277. title: '正在生成',
  278. mask: true
  279. })
  280. this.setData({
  281. ShowShareMask: false,
  282. ShowshareCirclesMask: true
  283. })
  284. const _self = this
  285. const rpx = this.data.system.windowWidth / 750
  286. const ctx = wx.createCanvasContext('shareCanvas')
  287. const canvasW = 466 * rpx
  288. const canvasH = 828 * rpx
  289. const avatarW = 60 * rpx
  290. const avatarH = 60 * rpx
  291. const avatarX = 203 * rpx
  292. const avatarY = 23 * rpx
  293. const avatarBW = 66 * rpx
  294. const avatarBH = 66 * rpx
  295. const avatarBX = 200 * rpx
  296. const avatarBY = 20 * rpx
  297. const QRcodeW = 137 * rpx
  298. const QRcodeH = 137 * rpx
  299. const QRcodeX = 165 * rpx
  300. const QRcodeY = 585 * rpx
  301. const QRcodeBW = 147 * rpx
  302. const QRcodeBH = 147 * rpx
  303. const QRcodeBX = 160 * rpx
  304. const QRcodeBY = 580 * rpx
  305. util.getImageInfo(this.data.shareInfo.pyq_bg).then(res => {
  306. ctx.drawImage(res.path, 0, 0, canvasW, canvasH)
  307. ctx.save()
  308. ctx.beginPath()
  309. ctx.arc(avatarBW / 2 + avatarBX, avatarBH / 2 + avatarBY, avatarBW / 2, 0, Math.PI * 2, false)
  310. ctx.clip()
  311. ctx.setFillStyle('white')
  312. ctx.fillRect(avatarBX, avatarBY, avatarBW, avatarBH)
  313. ctx.restore()
  314. ctx.draw(true)
  315. ctx.save()
  316. ctx.beginPath()
  317. ctx.arc(QRcodeBW / 2 + QRcodeBX, QRcodeBH / 2 + QRcodeBY, QRcodeBW / 2, 0, Math.PI * 2, false)
  318. ctx.clip()
  319. ctx.setFillStyle('white')
  320. ctx.fillRect(QRcodeBX, QRcodeBY, QRcodeBW, QRcodeBH)
  321. ctx.restore()
  322. ctx.draw(true)
  323. }).then(res => {
  324. return util.getImageInfo(this.data.shareInfo.mp_qr).then(res => {
  325. ctx.save()
  326. ctx.beginPath()
  327. ctx.arc(QRcodeW / 2 + QRcodeX, QRcodeH / 2 + QRcodeY, QRcodeW / 2, 0, Math.PI * 2, false)
  328. ctx.clip()
  329. ctx.drawImage(res.path, canvasW / 2 - QRcodeW / 2, QRcodeY, QRcodeW, QRcodeH)
  330. ctx.restore()
  331. ctx.draw(true)
  332. })
  333. }).then(res => {
  334. return util.getImageInfo(this.data.userInfo.avatar).then(res => {
  335. ctx.save()
  336. ctx.beginPath()
  337. ctx.arc(avatarW / 2 + avatarX, avatarH / 2 + avatarY, avatarW / 2, 0, Math.PI * 2, false)
  338. ctx.clip()
  339. ctx.drawImage(res.path, avatarX, avatarY, avatarW, avatarH)
  340. ctx.restore()
  341. ctx.draw(true)
  342. })
  343. }).then(res => {
  344. setTimeout(() => {
  345. wx.canvasToTempFilePath({
  346. x: 0,
  347. y: 0,
  348. width: canvasW,
  349. height: canvasH,
  350. destWidth: canvasW * 4,
  351. destHeight: canvasH * 4,
  352. canvasId: 'shareCanvas',
  353. success: function(res) {
  354. wx.hideLoading()
  355. _self.setData({
  356. shareImgSrc: res.tempFilePath
  357. })
  358. },
  359. fail: function(res) {
  360. console.log(res)
  361. }
  362. })
  363. }, 500)
  364. }).catch(err => {
  365. wx.hideLoading()
  366. console.log(err)
  367. })
  368. },
  369. // 保存分享朋友圈图片
  370. saveMemQRcode: function(e) {
  371. const _self = this
  372. wx.showLoading({
  373. title: '正在保存...'
  374. })
  375. wx.saveImageToPhotosAlbum({
  376. filePath: _self.data.shareImgSrc,
  377. success: function(res) {
  378. console.log('保存图片', res)
  379. wx.showToast({
  380. title: '保存成功',
  381. icon: 'success',
  382. duration: 1000
  383. })
  384. },
  385. fail: function(err) {
  386. console.log('保存图片', err)
  387. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
  388. console.log("用户一开始拒绝了,我们想再次发起授权")
  389. console.log('打开设置窗口')
  390. wx.openSetting({
  391. success(settingdata) {
  392. console.log(settingdata)
  393. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  394. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  395. } else {
  396. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  397. }
  398. }
  399. })
  400. }
  401. },
  402. complete: function() {
  403. wx.hideLoading()
  404. _self.setData({
  405. ShowshareCirclesMask: false
  406. })
  407. wx.showTabBar({
  408. aniamtion: true
  409. })
  410. }
  411. })
  412. },
  413. // 生命周期函数--监听页面初次渲染完成
  414. onReady: function() {},
  415. // 生命周期函数--监听页面显示
  416. onShow: function(options) {
  417. wx.showShareMenu({ // 允许转发时携带 shareTicket。
  418. withShareTicket: true
  419. })
  420. let timer = setInterval(() => {
  421. if (app.globalData.PageCallback) {
  422. if (app.globalData.windowOpen && app.globalData.userInfo.get_favorite_award !== 2) {
  423. api.FavoriteRewardGet().then(res => {
  424. if (this.data.mpType === 'gd' && res.data.gain_integral && res.data.gain_integral > 0) {
  425. wx.showModal({
  426. title: '收藏奖励',
  427. content: `恭喜获得${res.data.gain_integral}金币!`,
  428. showCancel: false
  429. })
  430. } else if (this.data.mpType === 'rp' && res.data.gain_amount && res.data.gain_amount > 0) {
  431. wx.showModal({
  432. title: '收藏奖励',
  433. content: `恭喜获得${res.data.gain_amount}元!`,
  434. showCancel: false
  435. })
  436. }
  437. }, err => {
  438. })
  439. }
  440. api.getPlayIndex({
  441. HideLoading: this.data.hideLoadingStatus
  442. }).then(res => {
  443. this.setData({
  444. playGame: res.data,
  445. hideLoadingStatus: true
  446. })
  447. }, err => {
  448. console.log('getPlayIndex failed', err)
  449. })
  450. if (!this.data.first) {
  451. api.getUserInfo().then(res => {
  452. this.setData({
  453. userInfo: res.data,
  454. auth: res.data.auth
  455. })
  456. }, err => {
  457. console.log(err)
  458. })
  459. } else {
  460. this.setData({
  461. first: false
  462. })
  463. }
  464. clearInterval(timer)
  465. }
  466. }, 60)
  467. this.setData({
  468. ShowStoreUpMask: false
  469. })
  470. },
  471. // 生命周期函数--监听页面隐藏
  472. onHide: function() {},
  473. // 生命周期函数--监听页面卸载
  474. onUnload: function() {},
  475. // 页面相关事件处理函数--监听用户下拉动作
  476. onPullDownRefresh: function() {
  477. api.getPlayIndex({HideLoading: true}).then(res => {
  478. this.setData({
  479. playGame: res.data
  480. })
  481. }, err => {
  482. console.log('getPlayIndex failed', err)
  483. })
  484. wx.stopPullDownRefresh()
  485. },
  486. // 页面上拉触底事件的处理函数
  487. onReachBottom: function() {
  488. },
  489. openGame: function(e) {
  490. let id = e.currentTarget.dataset.id
  491. api.openGame({
  492. game_id: id
  493. }).then(res => {
  494. console.log(res)
  495. }, err => {
  496. console.log(err)
  497. })
  498. },
  499. showPop: function (e) {
  500. wx.previewImage({
  501. urls: [e.currentTarget.dataset.entrance]
  502. })
  503. }
  504. })