box.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. const util = require('../../utils/util.js')
  2. const api = require('../../utils/api.js')
  3. const app = getApp()
  4. Page({
  5. data: {
  6. auth: {},
  7. userInfo: {},
  8. system: '', // devtools:PC ios:苹果 android:安卓
  9. boxGamesParams: {
  10. page: 1,
  11. offset: 999,
  12. listype: 2
  13. },
  14. ShowEditInfoMask: false,
  15. boxName: '我的游戏盒子',
  16. boxName_maxLength: 12,
  17. boxIntro: '这是我的游戏盒子,来玩吧额勤奋的我',
  18. boxIntro_maxLength: 20,
  19. boxInfo: {}, // 主页信息
  20. memData: [],
  21. memGame: {},
  22. floats: [],
  23. visiblePrompt: true,
  24. rankList: [],
  25. sharePath: 'pages/share/game',
  26. shareInfo: {},
  27. ShowShareMask: false,
  28. ShowshareCirclesMask: false,
  29. mpType: app.globalData.mpType,
  30. GamePlayList: {}
  31. },
  32. /**
  33. * 生命周期函数--监听页面加载
  34. */
  35. onLoad: function (options) {
  36. this.getSystem()
  37. let timer = setInterval(() => {
  38. if (app.globalData.PageCallback) {
  39. this.getBoxInfo()
  40. this.getShareInfo()
  41. clearInterval(timer)
  42. this.setData({
  43. auth: app.globalData.auth,
  44. userInfo: app.globalData.userInfo
  45. })
  46. }
  47. }, 200)
  48. },
  49. getShareInfo: function () {
  50. api.getShareInfo({
  51. data: {
  52. path: 'pages/share/share'
  53. }
  54. }).then(res => {
  55. this.setData({
  56. shareInfo: res.data
  57. })
  58. }, err => {
  59. console.log(err)
  60. })
  61. },
  62. // 关闭分享朋友圈弹框
  63. CloseshareCircles: function () {
  64. wx.showTabBar({
  65. aniamtion: true
  66. })
  67. this.setData({
  68. ShowshareCirclesMask: false
  69. })
  70. },
  71. onShow: function () {
  72. wx.showShareMenu({ // 允许转发时携带 shareTicket。
  73. withShareTicket: true
  74. })
  75. let timer = setInterval(() => {
  76. if (app.globalData.PageCallback) {
  77. this.getMemData()
  78. this.getBoxGameList()
  79. this.getShareIncomRank()
  80. this.getGamePlayList()
  81. clearInterval(timer)
  82. }
  83. }, 60)
  84. },
  85. routeDetail: function (e) {
  86. // util.routeTo(`/pages/detail/detail?id=${e.currentTarget.dataset.id}`)
  87. },
  88. // 获取活跃用户
  89. getMemData: function () {
  90. api.getMemberList({ page: 1, offset: 10, HideLoading: true }).then(res => {
  91. this.setData({
  92. memData: res.data
  93. })
  94. })
  95. },
  96. // 获取我的盒子游戏列表
  97. getBoxGameList: function () {
  98. api.getMemGameList({
  99. ...this.data.boxGamesParams,
  100. offset: 3,
  101. HideLoading: true
  102. }).then( res => {
  103. this.setData({
  104. memGame: res.data,
  105. floats: res.data.list.map((item) => {
  106. return this.data.mpType === 'gd' ? item.mem_agent_integral : (item.mem_agent_reward).toFixed(2)
  107. })
  108. })
  109. })
  110. },
  111. getShareIncomRank: function () {
  112. api.getShareIncomeRank({ HideLoading: true }).then(res => {
  113. if (res.code === 200) {
  114. this.setData({
  115. rankList: res.data.list
  116. })
  117. }
  118. })
  119. },
  120. // tips 显示
  121. ShowPrompt: function () {
  122. this.setData({
  123. visiblePrompt: !this.data.visiblePrompt
  124. })
  125. },
  126. // 获取主页信息
  127. getBoxInfo: function () {
  128. api.getHomeInfo().then(res => {
  129. this.setData({
  130. boxInfo: res.data,
  131. boxName: res.data.title,
  132. boxIntro: res.data.intro
  133. })
  134. })
  135. },
  136. // 限制输入字数
  137. ChangeForm: function (e) {
  138. let taget = e.detail
  139. if (e.target.dataset.type === 'Name') {
  140. this.setData({
  141. boxName: taget.cursor > this.data.boxName_maxLength ? taget.value.substr(0, this.data.boxName_maxLength) : taget.value
  142. })
  143. } else if (e.target.dataset.type === 'Intro') {
  144. this.setData({
  145. boxIntro: taget.cursor > this.data.boxIntro_maxLength ? taget.value.substr(0, this.data.boxIntro_maxLength) : taget.value
  146. })
  147. }
  148. },
  149. // 显示隐藏编辑信息弹框
  150. ChangeEditInfo: function (e) {
  151. this.setData({
  152. ShowEditInfoMask: e.currentTarget.dataset.show ? true : false
  153. })
  154. },
  155. // 保存信息
  156. saveEdit: function () {
  157. const _self = this
  158. let params = { title: this.data.boxName, intro: this.data.boxIntro }
  159. if (params.title && params.intro) {
  160. api.setHomeInfo(params).then(res => {
  161. if (res.code === 200) {
  162. wx.showToast({
  163. title: res.msg,
  164. icon: 'success',
  165. duration: 500,
  166. mask: true,
  167. success: function () {
  168. setTimeout(() => {
  169. _self.getBoxInfo()
  170. _self.setData({
  171. ShowEditInfoMask: false
  172. })
  173. }, 500)
  174. }
  175. })
  176. } else {
  177. wx.showToast({
  178. title: `${res.msg}`
  179. })
  180. }
  181. })
  182. } else {
  183. wx.showToast({
  184. title: '请确认您的输入完整后重试~',
  185. icon: 'none'
  186. })
  187. }
  188. },
  189. routeManage: function (e) {
  190. util.routeTo('/pages/manage/manage')
  191. },
  192. routeTo: function (e) {
  193. util.routeTo(e.currentTarget.dataset.to)
  194. },
  195. /**
  196. * 页面上拉触底事件的处理函数
  197. */
  198. onReachBottom: function () {
  199. },
  200. openGame: function (e) {
  201. let id = e.currentTarget.dataset.id
  202. api.openGame({
  203. game_id: id
  204. }).then(res => {
  205. console.log(res)
  206. }, err => {
  207. console.log(err)
  208. })
  209. },
  210. /**
  211. * 用户点击右上角分享
  212. */
  213. onShareAppMessage: function (opt) {
  214. this.getShareInfo()
  215. let shareUrl = ''
  216. let shareTitle = ''
  217. let shareImage = ''
  218. if (opt.from === 'menu') {
  219. let { title, image, state } = this.data.shareInfo
  220. shareUrl = `/pages/share/share?state=${state}&mem_id=${this.data.userInfo.mem_id}`
  221. shareTitle = this.data.boxName
  222. } else if (opt.target) {
  223. let { title, image, state } = this.data.shareInfo
  224. let { game_id, gamename, share_title, share_img } = opt.target.dataset
  225. if (opt.target.id === 'share-box') {
  226. shareUrl = `/pages/share/share?state=${state}&mem_id=${this.data.userInfo.mem_id}`
  227. shareTitle = title || this.data.userInfo.nickname
  228. shareImage = image
  229. } else if (opt.target.id === 'share-game') {
  230. shareUrl = `/${this.data.sharePath}?state=${state}&game_id=${game_id}&mem_id=${this.data.userInfo.mem_id}`
  231. shareTitle = share_title || gamename || title
  232. shareImage = share_img || image
  233. }
  234. }
  235. return {
  236. title: shareTitle,
  237. path: shareUrl,
  238. imageUrl: shareImage
  239. }
  240. },
  241. // 显示隐藏分享menu
  242. ChangeShare: function (e) {
  243. wx.hideTabBar({
  244. animation: true
  245. })
  246. if (!this.data.shareInfo.title) {
  247. api.getShareInfo({
  248. data: {
  249. path: this.data.sharePath
  250. }
  251. }).then(res => {
  252. console.log('getShare', res)
  253. this.setData({
  254. shareInfo: res.data,
  255. ShowShareMask: e.currentTarget.dataset.show ? true : false
  256. })
  257. }).catch(err => {
  258. console.log('getShare', err)
  259. })
  260. } else {
  261. this.setData({
  262. ShowShareMask: e.currentTarget.dataset.show ? true : false
  263. })
  264. }
  265. },
  266. // 显示隐藏分享弹框
  267. showShare: function (e) {
  268. let { show } = e.currentTarget.dataset
  269. if (!show) {
  270. wx.showTabBar({
  271. animation: true
  272. })
  273. }
  274. this.setData({
  275. ShowShareMask: show ? true : false
  276. })
  277. },
  278. // 分享朋友圈
  279. shareCircles: function () {
  280. console.log(this.data.shareImgSrc)
  281. if (this.data.shareImgSrc) {
  282. this.setData({
  283. ShowShareMask: false,
  284. ShowshareCirclesMask: true
  285. })
  286. return false
  287. }
  288. wx.showLoading({
  289. title: '正在生成',
  290. mask: true
  291. })
  292. this.setData({
  293. ShowShareMask: false,
  294. ShowshareCirclesMask: true
  295. })
  296. const _self = this
  297. const rpx = this.data.system.windowWidth / 750
  298. const ctx = wx.createCanvasContext('shareCanvas')
  299. const canvasW = 466 * rpx
  300. const canvasH = 828 * rpx
  301. const avatarW = 60 * rpx
  302. const avatarH = 60 * rpx
  303. const avatarX = 203 * rpx
  304. const avatarY = 23 * rpx
  305. const avatarBW = 66 * rpx
  306. const avatarBH = 66 * rpx
  307. const avatarBX = 200 * rpx
  308. const avatarBY = 20 * rpx
  309. const QRcodeW = 137 * rpx
  310. const QRcodeH = 137 * rpx
  311. const QRcodeX = 165 * rpx
  312. const QRcodeY = 585 * rpx
  313. const QRcodeBW = 147 * rpx
  314. const QRcodeBH = 147 * rpx
  315. const QRcodeBX = 160 * rpx
  316. const QRcodeBY = 580 * rpx
  317. util.getImageInfo(this.data.shareInfo.pyq_bg).then(res => {
  318. ctx.drawImage(res.path, 0, 0, canvasW, canvasH)
  319. ctx.save()
  320. ctx.beginPath()
  321. ctx.arc(avatarBW / 2 + avatarBX, avatarBH / 2 + avatarBY, avatarBW / 2, 0, Math.PI * 2, false)
  322. ctx.clip()
  323. ctx.setFillStyle('white')
  324. ctx.fillRect(avatarBX, avatarBY, avatarBW, avatarBH)
  325. ctx.restore()
  326. ctx.draw(true)
  327. ctx.save()
  328. ctx.beginPath()
  329. ctx.arc(QRcodeBW / 2 + QRcodeBX, QRcodeBH / 2 + QRcodeBY, QRcodeBW / 2, 0, Math.PI * 2, false)
  330. ctx.clip()
  331. ctx.setFillStyle('white')
  332. ctx.fillRect(QRcodeBX, QRcodeBY, QRcodeBW, QRcodeBH)
  333. ctx.restore()
  334. ctx.draw(true)
  335. }).then(res => {
  336. return util.getImageInfo(this.data.shareInfo.mp_qr).then(res => {
  337. ctx.save()
  338. ctx.beginPath()
  339. ctx.arc(QRcodeW / 2 + QRcodeX, QRcodeH / 2 + QRcodeY, QRcodeW / 2, 0, Math.PI * 2, false)
  340. ctx.clip()
  341. ctx.drawImage(res.path, canvasW / 2 - QRcodeW / 2, QRcodeY, QRcodeW, QRcodeH)
  342. ctx.restore()
  343. ctx.draw(true)
  344. })
  345. }).then(res => {
  346. return util.getImageInfo(this.data.userInfo.avatar).then(res => {
  347. ctx.beginPath()
  348. ctx.arc(avatarW / 2 + avatarX, avatarH / 2 + avatarY, avatarW / 2, 0, Math.PI * 2, false)
  349. ctx.clip()
  350. ctx.drawImage(res.path, canvasW / 2 - avatarW / 2, avatarY, avatarW, avatarH)
  351. ctx.restore()
  352. ctx.draw(true)
  353. })
  354. }).then(res => {
  355. setTimeout(() => {
  356. wx.canvasToTempFilePath({
  357. x: 0,
  358. y: 0,
  359. width: canvasW,
  360. height: canvasH,
  361. destWidth: canvasW * 4,
  362. destHeight: canvasH * 4,
  363. canvasId: 'shareCanvas',
  364. success: function (res) {
  365. wx.hideLoading()
  366. _self.setData({
  367. shareImgSrc: res.tempFilePath
  368. })
  369. },
  370. fail: function (res) {
  371. console.log(res)
  372. }
  373. })
  374. }, 500)
  375. }).catch(err => {
  376. wx.hideLoading()
  377. console.log(err)
  378. })
  379. },
  380. // 保存分享朋友圈图片
  381. saveMemQRcode: function (e) {
  382. const _self = this
  383. wx.showLoading({
  384. title: '正在保存...'
  385. })
  386. wx.saveImageToPhotosAlbum({
  387. filePath: _self.data.shareImgSrc,
  388. success: function (res) {
  389. console.log('保存图片', res)
  390. wx.showToast({
  391. title: '保存成功',
  392. icon: 'success',
  393. duration: 1000
  394. })
  395. },
  396. fail: function (err) {
  397. console.log('保存图片', err)
  398. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
  399. console.log("用户一开始拒绝了,我们想再次发起授权")
  400. console.log('打开设置窗口')
  401. wx.openSetting({
  402. success(settingdata) {
  403. console.log(settingdata)
  404. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  405. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  406. } else {
  407. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  408. }
  409. }
  410. })
  411. }
  412. },
  413. complete: function () {
  414. wx.hideLoading()
  415. _self.setData({
  416. ShowshareCirclesMask: false
  417. })
  418. wx.showTabBar({
  419. aniamtion: true
  420. })
  421. }
  422. })
  423. },
  424. // 获取设备信息
  425. getSystem: function (e) {
  426. const _self = this
  427. wx.getSystemInfo({
  428. success: function (res) {
  429. _self.setData({
  430. system: res
  431. })
  432. }
  433. })
  434. },
  435. // 我玩过的游戏
  436. getGamePlayList: function () {
  437. api.getGamePlayList({
  438. page: 1,
  439. offset: 6,
  440. HideLoading: true
  441. }).then(res => {
  442. if (!res.data.count) {
  443. api.getGameList({
  444. rank_type: 3,
  445. page: 1,
  446. offset: 6,
  447. HideLoading: true
  448. }).then(res => {
  449. this.setData({
  450. GamePlayList: res.data
  451. })
  452. })
  453. } else {
  454. this.setData({
  455. GamePlayList: res.data
  456. })
  457. }
  458. })
  459. },
  460. showPop: function (e) {
  461. wx.previewImage({
  462. urls: [e.currentTarget.dataset.entrance]
  463. })
  464. }
  465. })