game.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. if (res.data.is_old_member === 2) {
  142. wx.switchTab({
  143. url: '/pages/user/index'
  144. })
  145. _self.setData({
  146. ShowRedPackMask: false
  147. })
  148. wx.showTabBar({
  149. animation: true
  150. })
  151. } else {
  152. _self.setData({
  153. // redPackAward: _self.data.mpType === 'gd' ? res.data.novice_award_integral : (res.data.novice_award * 1).toFixed(2),
  154. redPackAward: (res.data.novice_award * 1).toFixed(2),
  155. getSuccess: true
  156. })
  157. // 更新余额
  158. api.getUserInfo()
  159. setTimeout(() => {
  160. _self.setData({
  161. userInfo: getApp().globalData.userInfo
  162. })
  163. }, 500)
  164. }
  165. } else {
  166. wx.showToast({
  167. title: res.msg,
  168. })
  169. }
  170. })
  171. _self.setData({
  172. authEnter: false
  173. })
  174. wx.showTabBar()
  175. },
  176. fail: function(err) {
  177. console.log(err)
  178. }
  179. })
  180. },
  181. // 获取设备信息
  182. getSystem: function(e) {
  183. const _self = this
  184. wx.getSystemInfo({
  185. success: function(res) {
  186. _self.setData({
  187. system: res
  188. })
  189. }
  190. })
  191. },
  192. // 跳转赚钱技巧页
  193. routeTips: function(e) {
  194. if (this.data.mpType === 'gd') {
  195. // util.routeTo('/pages/tips/tips')
  196. util.routeTo('/pages/lottery/lottery')
  197. } else {
  198. util.routeTo('/pages/user/purse')
  199. }
  200. },
  201. // 显示隐藏收藏弹框
  202. ChangeStoreUp: function(e) {
  203. this.setData({
  204. ShowStoreUpMask: e.currentTarget.dataset.show ? true : false,
  205. favStyle: (e.currentTarget.dataset.show || this.data.scrollTop === 0) ? '' : 'slide-left'
  206. })
  207. },
  208. // 显示隐藏分享弹框
  209. showShare: function(e) {
  210. let { show } = e.currentTarget.dataset
  211. if (!show) {
  212. wx.showTabBar({
  213. animation: true
  214. })
  215. }
  216. this.setData({
  217. ShowShareMask: show ? true : false
  218. })
  219. },
  220. // 显示隐藏分享menu
  221. ChangeShare: function (e) {
  222. wx.hideTabBar({
  223. animation: true
  224. })
  225. if (!this.data.shareInfo.title) {
  226. api.getShareInfo({
  227. data: {
  228. path: this.data.sharePath
  229. }
  230. }).then(res => {
  231. console.log('getShare', res)
  232. this.setData({
  233. shareInfo: res.data,
  234. ShowShareMask: e.currentTarget.dataset.show ? true : false
  235. })
  236. }).catch(err => {
  237. console.log('getShare', err)
  238. })
  239. } else {
  240. this.setData({
  241. ShowShareMask: e.currentTarget.dataset.show ? true : false
  242. })
  243. }
  244. },
  245. // 关闭红包弹框
  246. CloseRedPack: function(e) {
  247. const _self = this
  248. _self.setData({
  249. redAnimation: false
  250. })
  251. setTimeout(() => {
  252. _self.setData({
  253. ShowRedPackMask: false
  254. })
  255. wx.showTabBar({
  256. animation: true
  257. })
  258. }, 300)
  259. },
  260. // 设置红包动画
  261. setAnimation: function(e) {
  262. const _self = this
  263. return new Promise((resolve, reject) => {
  264. _self.setData({
  265. ShowRedPackMask: true
  266. })
  267. resolve()
  268. })
  269. },
  270. // 关闭分享朋友圈弹框
  271. CloseshareCircles: function() {
  272. wx.showTabBar({
  273. aniamtion: true
  274. })
  275. this.setData({
  276. ShowshareCirclesMask: false
  277. })
  278. },
  279. // 分享朋友圈
  280. shareCircles: function() {
  281. console.log(this.data.shareImgSrc)
  282. if (this.data.shareImgSrc) {
  283. this.setData({
  284. ShowShareMask: false,
  285. ShowshareCirclesMask: true
  286. })
  287. return false
  288. }
  289. wx.showLoading({
  290. title: '正在生成',
  291. mask: true
  292. })
  293. this.setData({
  294. ShowShareMask: false,
  295. ShowshareCirclesMask: true
  296. })
  297. const _self = this
  298. const rpx = this.data.system.windowWidth / 750
  299. const ctx = wx.createCanvasContext('shareCanvas')
  300. const canvasW = 466 * rpx
  301. const canvasH = 828 * rpx
  302. const avatarW = 70 * rpx
  303. const avatarH = 70 * rpx
  304. const avatarX = 200 * rpx
  305. const avatarY = 19 * rpx
  306. const avatarBW = 66 * rpx
  307. const avatarBH = 66 * rpx
  308. const avatarBX = 200 * rpx
  309. const avatarBY = 20 * rpx
  310. const QRcodeW = 454 * rpx
  311. const QRcodeH = 454 * rpx
  312. const QRcodeX = 6 * rpx
  313. const QRcodeY = 374 * rpx
  314. const QRcodeBW = 147 * rpx
  315. const QRcodeBH = 147 * rpx
  316. const QRcodeBX = 160 * rpx
  317. const QRcodeBY = 580 * rpx
  318. util.getImageInfo(this.data.shareInfo.pyq_bg).then(res => {
  319. ctx.drawImage(res.path, 0, 0, canvasW, canvasH)
  320. ctx.save()
  321. ctx.beginPath()
  322. ctx.arc(avatarBW / 2 + avatarBX, avatarBH / 2 + avatarBY, avatarBW / 2, 0, Math.PI * 2, false)
  323. ctx.clip()
  324. ctx.setFillStyle('white')
  325. ctx.fillRect(avatarBX, avatarBY, avatarBW, avatarBH)
  326. ctx.restore()
  327. ctx.draw(true)
  328. ctx.save()
  329. ctx.beginPath()
  330. ctx.arc(QRcodeBW / 2 + QRcodeBX, QRcodeBH / 2 + QRcodeBY, QRcodeBW / 2, 0, Math.PI * 2, false)
  331. ctx.clip()
  332. ctx.setFillStyle('white')
  333. ctx.fillRect(QRcodeBX, QRcodeBY, QRcodeBW, QRcodeBH)
  334. ctx.restore()
  335. ctx.draw(true)
  336. }).then(res => {
  337. return util.getImageInfo(this.data.shareInfo.mp_qr).then(res => {
  338. ctx.save()
  339. ctx.beginPath()
  340. ctx.arc(QRcodeW / 2 + QRcodeX, QRcodeH / 2 + QRcodeY, QRcodeW / 2, 0, Math.PI * 2, false)
  341. ctx.clip()
  342. ctx.drawImage(res.path, canvasW / 2 - QRcodeW / 2, QRcodeY, QRcodeW, QRcodeH)
  343. ctx.restore()
  344. ctx.draw(true)
  345. })
  346. }).then(res => {
  347. return util.getImageInfo(this.data.userInfo.avatar).then(res => {
  348. ctx.save()
  349. ctx.beginPath()
  350. ctx.arc(avatarW / 2 + avatarX, avatarH / 2 + avatarY, avatarW / 2, 0, Math.PI * 2, false)
  351. ctx.clip()
  352. ctx.drawImage(res.path, avatarX, avatarY, avatarW, avatarH)
  353. ctx.restore()
  354. ctx.draw(true)
  355. })
  356. }).then(res => {
  357. setTimeout(() => {
  358. wx.canvasToTempFilePath({
  359. x: 0,
  360. y: 0,
  361. width: canvasW,
  362. height: canvasH,
  363. destWidth: canvasW * 4,
  364. destHeight: canvasH * 4,
  365. canvasId: 'shareCanvas',
  366. success: function(res) {
  367. wx.hideLoading()
  368. _self.setData({
  369. shareImgSrc: res.tempFilePath
  370. })
  371. },
  372. fail: function(res) {
  373. console.log(res)
  374. }
  375. })
  376. }, 500)
  377. }).catch(err => {
  378. wx.hideLoading()
  379. console.log(err)
  380. })
  381. },
  382. // 保存分享朋友圈图片
  383. saveMemQRcode: function(e) {
  384. const _self = this
  385. wx.showLoading({
  386. title: '正在保存...'
  387. })
  388. wx.saveImageToPhotosAlbum({
  389. filePath: _self.data.shareImgSrc,
  390. success: function(res) {
  391. console.log('保存图片', res)
  392. wx.showToast({
  393. title: '保存成功',
  394. icon: 'success',
  395. duration: 1000
  396. })
  397. },
  398. fail: function(err) {
  399. console.log('保存图片', err)
  400. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
  401. console.log("用户一开始拒绝了,我们想再次发起授权")
  402. console.log('打开设置窗口')
  403. wx.openSetting({
  404. success(settingdata) {
  405. console.log(settingdata)
  406. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  407. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  408. } else {
  409. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  410. }
  411. }
  412. })
  413. }
  414. },
  415. complete: function() {
  416. wx.hideLoading()
  417. _self.setData({
  418. ShowshareCirclesMask: false
  419. })
  420. wx.showTabBar({
  421. aniamtion: true
  422. })
  423. }
  424. })
  425. },
  426. // 生命周期函数--监听页面初次渲染完成
  427. onReady: function() {},
  428. // 生命周期函数--监听页面显示
  429. onShow: function(options) {
  430. wx.showShareMenu({ // 允许转发时携带 shareTicket。
  431. withShareTicket: true
  432. })
  433. let timer = setInterval(() => {
  434. if (app.globalData.PageCallback) {
  435. if (app.globalData.windowOpen && app.globalData.userInfo.get_favorite_award !== 2) {
  436. api.FavoriteRewardGet().then(res => {
  437. if (this.data.mpType === 'gd' && res.data.gain_integral && res.data.gain_integral > 0) {
  438. wx.showModal({
  439. title: '收藏奖励',
  440. content: `恭喜获得${res.data.gain_integral}金币!`,
  441. showCancel: false
  442. })
  443. } else if (this.data.mpType === 'rp' && res.data.gain_amount && res.data.gain_amount > 0) {
  444. wx.showModal({
  445. title: '收藏奖励',
  446. content: `恭喜获得${res.data.gain_amount}元!`,
  447. showCancel: false
  448. })
  449. }
  450. }, err => {
  451. })
  452. }
  453. api.getPlayIndex({
  454. HideLoading: this.data.hideLoadingStatus
  455. }).then(res => {
  456. this.setData({
  457. playGame: res.data,
  458. hideLoadingStatus: true
  459. })
  460. }, err => {
  461. console.log('getPlayIndex failed', err)
  462. })
  463. if (!this.data.first) {
  464. api.getUserInfo().then(res => {
  465. this.setData({
  466. userInfo: res.data,
  467. auth: res.data.auth
  468. })
  469. }, err => {
  470. console.log(err)
  471. })
  472. } else {
  473. this.setData({
  474. first: false
  475. })
  476. }
  477. clearInterval(timer)
  478. }
  479. }, 60)
  480. this.setData({
  481. ShowStoreUpMask: false
  482. })
  483. },
  484. // 生命周期函数--监听页面隐藏
  485. onHide: function() {},
  486. // 生命周期函数--监听页面卸载
  487. onUnload: function() {},
  488. // 页面相关事件处理函数--监听用户下拉动作
  489. onPullDownRefresh: function() {
  490. api.getPlayIndex({HideLoading: true}).then(res => {
  491. this.setData({
  492. playGame: res.data
  493. })
  494. }, err => {
  495. console.log('getPlayIndex failed', err)
  496. })
  497. wx.stopPullDownRefresh()
  498. },
  499. // 页面上拉触底事件的处理函数
  500. onReachBottom: function() {
  501. },
  502. openGame: function(e) {
  503. let id = e.currentTarget.dataset.id
  504. api.openGame({
  505. game_id: id
  506. }).then(res => {
  507. console.log(res)
  508. }, err => {
  509. console.log(err)
  510. })
  511. },
  512. showPop: function (e) {
  513. wx.previewImage({
  514. urls: [e.currentTarget.dataset.entrance]
  515. })
  516. }
  517. })