box.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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.getBlackList()
  80. this.getShareIncomRank()
  81. this.getGamePlayList()
  82. clearInterval(timer)
  83. }
  84. }, 60)
  85. },
  86. routeDetail: function (e) {
  87. // util.routeTo(`/pages/detail/detail?id=${e.currentTarget.dataset.id}`)
  88. },
  89. // 获取活跃用户
  90. getMemData: function () {
  91. api.getMemberList({ page: 1, offset: 10, HideLoading: true }).then(res => {
  92. this.setData({
  93. memData: res.data
  94. })
  95. })
  96. },
  97. // 获取我的盒子游戏列表
  98. getBoxGameList: function () {
  99. api.getMemGameList({
  100. ...this.data.boxGamesParams,
  101. offset: 3,
  102. HideLoading: true
  103. }).then( res => {
  104. this.setData({
  105. memGame: res.data,
  106. floats: res.data.list.map((item) => {
  107. return this.data.mpType === 'gd' ? item.mem_agent_integral : (item.mem_agent_reward).toFixed(2)
  108. })
  109. })
  110. })
  111. },
  112. getShareIncomRank: function () {
  113. api.getShareIncomeRank({ HideLoading: true }).then(res => {
  114. if (res.code === 200) {
  115. this.setData({
  116. rankList: res.data.list
  117. })
  118. }
  119. })
  120. },
  121. // tips 显示
  122. ShowPrompt: function () {
  123. this.setData({
  124. visiblePrompt: !this.data.visiblePrompt
  125. })
  126. },
  127. // 获取主页信息
  128. getBoxInfo: function () {
  129. api.getHomeInfo().then(res => {
  130. this.setData({
  131. boxInfo: res.data,
  132. boxName: res.data.title,
  133. boxIntro: res.data.intro
  134. })
  135. })
  136. },
  137. // 限制输入字数
  138. ChangeForm: function (e) {
  139. let taget = e.detail
  140. if (e.target.dataset.type === 'Name') {
  141. this.setData({
  142. boxName: taget.cursor > this.data.boxName_maxLength ? taget.value.substr(0, this.data.boxName_maxLength) : taget.value
  143. })
  144. } else if (e.target.dataset.type === 'Intro') {
  145. this.setData({
  146. boxIntro: taget.cursor > this.data.boxIntro_maxLength ? taget.value.substr(0, this.data.boxIntro_maxLength) : taget.value
  147. })
  148. }
  149. },
  150. // 显示隐藏编辑信息弹框
  151. ChangeEditInfo: function (e) {
  152. this.setData({
  153. ShowEditInfoMask: e.currentTarget.dataset.show ? true : false
  154. })
  155. },
  156. // 保存信息
  157. saveEdit: function () {
  158. const _self = this
  159. let params = { title: this.data.boxName, intro: this.data.boxIntro }
  160. if (params.title && params.intro) {
  161. api.setHomeInfo(params).then(res => {
  162. if (res.code === 200) {
  163. wx.showToast({
  164. title: res.msg,
  165. icon: 'success',
  166. duration: 500,
  167. mask: true,
  168. success: function () {
  169. setTimeout(() => {
  170. _self.getBoxInfo()
  171. _self.setData({
  172. ShowEditInfoMask: false
  173. })
  174. }, 500)
  175. }
  176. })
  177. } else {
  178. wx.showToast({
  179. title: `${res.msg}`
  180. })
  181. }
  182. })
  183. } else {
  184. wx.showToast({
  185. title: '请确认您的输入完整后重试~',
  186. icon: 'none'
  187. })
  188. }
  189. },
  190. routeManage: function (e) {
  191. util.routeTo('/pages/manage/manage')
  192. },
  193. routeTo: function (e) {
  194. util.routeTo(e.currentTarget.dataset.to)
  195. },
  196. /**
  197. * 页面上拉触底事件的处理函数
  198. */
  199. onReachBottom: function () {
  200. },
  201. openGame: function (e) {
  202. let id = e.currentTarget.dataset.id
  203. api.openGame({
  204. game_id: id
  205. }).then(res => {
  206. console.log(res)
  207. }, err => {
  208. console.log(err)
  209. })
  210. },
  211. /**
  212. * 用户点击右上角分享
  213. */
  214. onShareAppMessage: function (opt) {
  215. this.getShareInfo()
  216. let shareUrl = ''
  217. let shareTitle = ''
  218. let shareImage = ''
  219. if (opt.from === 'menu') {
  220. let { title, image, state } = this.data.shareInfo
  221. shareUrl = `/pages/share/share?state=${state}&mem_id=${this.data.userInfo.mem_id}`
  222. shareTitle = this.data.boxName
  223. } else if (opt.target) {
  224. let { title, image, state } = this.data.shareInfo
  225. let { game_id, gamename, share_title, share_img } = opt.target.dataset
  226. if (opt.target.id === 'share-box') {
  227. shareUrl = `/pages/share/share?state=${state}&mem_id=${this.data.userInfo.mem_id}`
  228. shareTitle = title || this.data.userInfo.nickname
  229. shareImage = image
  230. } else if (opt.target.id === 'share-game') {
  231. shareUrl = `/${this.data.sharePath}?state=${state}&game_id=${game_id}&mem_id=${this.data.userInfo.mem_id}`
  232. shareTitle = share_title || gamename || title
  233. shareImage = share_img || image
  234. }
  235. }
  236. return {
  237. title: shareTitle,
  238. path: shareUrl,
  239. imageUrl: shareImage
  240. }
  241. },
  242. // 显示隐藏分享menu
  243. ChangeShare: function (e) {
  244. wx.hideTabBar({
  245. animation: true
  246. })
  247. if (!this.data.shareInfo.title) {
  248. api.getShareInfo({
  249. data: {
  250. path: this.data.sharePath
  251. }
  252. }).then(res => {
  253. console.log('getShare', res)
  254. this.setData({
  255. shareInfo: res.data,
  256. ShowShareMask: e.currentTarget.dataset.show ? true : false
  257. })
  258. }).catch(err => {
  259. console.log('getShare', err)
  260. })
  261. } else {
  262. this.setData({
  263. ShowShareMask: e.currentTarget.dataset.show ? true : false
  264. })
  265. }
  266. },
  267. // 显示隐藏分享弹框
  268. showShare: function (e) {
  269. let { show } = e.currentTarget.dataset
  270. if (!show) {
  271. wx.showTabBar({
  272. animation: true
  273. })
  274. }
  275. this.setData({
  276. ShowShareMask: show ? true : 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.beginPath()
  349. ctx.arc(avatarW / 2 + avatarX, avatarH / 2 + avatarY, avatarW / 2, 0, Math.PI * 2, false)
  350. ctx.clip()
  351. ctx.drawImage(res.path, canvasW / 2 - avatarW / 2, avatarY, avatarW, avatarH)
  352. ctx.restore()
  353. ctx.draw(true)
  354. })
  355. }).then(res => {
  356. setTimeout(() => {
  357. wx.canvasToTempFilePath({
  358. x: 0,
  359. y: 0,
  360. width: canvasW,
  361. height: canvasH,
  362. destWidth: canvasW * 4,
  363. destHeight: canvasH * 4,
  364. canvasId: 'shareCanvas',
  365. success: function (res) {
  366. wx.hideLoading()
  367. _self.setData({
  368. shareImgSrc: res.tempFilePath
  369. })
  370. },
  371. fail: function (res) {
  372. console.log(res)
  373. }
  374. })
  375. }, 500)
  376. }).catch(err => {
  377. wx.hideLoading()
  378. console.log(err)
  379. })
  380. },
  381. // 保存分享朋友圈图片
  382. saveMemQRcode: function (e) {
  383. const _self = this
  384. wx.showLoading({
  385. title: '正在保存...'
  386. })
  387. wx.saveImageToPhotosAlbum({
  388. filePath: _self.data.shareImgSrc,
  389. success: function (res) {
  390. console.log('保存图片', res)
  391. wx.showToast({
  392. title: '保存成功',
  393. icon: 'success',
  394. duration: 1000
  395. })
  396. },
  397. fail: function (err) {
  398. console.log('保存图片', err)
  399. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
  400. console.log("用户一开始拒绝了,我们想再次发起授权")
  401. console.log('打开设置窗口')
  402. wx.openSetting({
  403. success(settingdata) {
  404. console.log(settingdata)
  405. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  406. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  407. } else {
  408. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  409. }
  410. }
  411. })
  412. }
  413. },
  414. complete: function () {
  415. wx.hideLoading()
  416. _self.setData({
  417. ShowshareCirclesMask: false
  418. })
  419. wx.showTabBar({
  420. aniamtion: true
  421. })
  422. }
  423. })
  424. },
  425. // 获取设备信息
  426. getSystem: function (e) {
  427. const _self = this
  428. wx.getSystemInfo({
  429. success: function (res) {
  430. _self.setData({
  431. system: res
  432. })
  433. }
  434. })
  435. },
  436. // 我玩过的游戏
  437. getGamePlayList: function () {
  438. api.getGamePlayList({
  439. page: 1,
  440. offset: 6,
  441. HideLoading: true
  442. }).then(res => {
  443. if (!res.data.count) {
  444. api.getGameList({
  445. rank_type: 3,
  446. page: 1,
  447. offset: 6,
  448. HideLoading: true
  449. }).then(res => {
  450. this.setData({
  451. GamePlayList: res.data
  452. })
  453. })
  454. } else {
  455. this.setData({
  456. GamePlayList: res.data
  457. })
  458. }
  459. })
  460. },
  461. getBlackList: function (e) {
  462. api.getBlackList({
  463. page: 1,
  464. offset: 3,
  465. HideLoading: true
  466. }).then(res => {
  467. this.setData({
  468. memGame: res.data,
  469. floats: res.data.list.map((item) => {
  470. return this.data.mpType === 'gd' ? item.mem_agent_integral : (item.mem_agent_reward).toFixed(2)
  471. })
  472. })
  473. })
  474. },
  475. showPop: function (e) {
  476. wx.previewImage({
  477. urls: [e.currentTarget.dataset.entrance]
  478. })
  479. }
  480. })