game.js 13 KB

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