index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // pages/user/user.js
  2. import util from '../../utils/util'
  3. import api from '../../utils/api'
  4. const app = getApp()
  5. Page({
  6. // 页面的初始数据
  7. data: {
  8. auth: {},
  9. userInfo: {},
  10. howToWithdraw: false,
  11. qrCode: '',
  12. GamePlayList: [],
  13. CenterData: {},
  14. showText: false,
  15. system: {},
  16. sharePath: 'pages/share/share',
  17. shareInfo: {},
  18. ShowShareMask: false,
  19. ShowshareCirclesMask: false,
  20. mpType: app.globalData.mpType
  21. },
  22. onPageScroll: function (res) {
  23. if (this.data.howToWithdraw) {
  24. this.setData({
  25. howToWithdraw: false
  26. })
  27. }
  28. },
  29. // 生命周期函数--监听页面加载
  30. onLoad: function (options) {
  31. this.getShareInfo()
  32. this.getSystem()
  33. },
  34. onShow: function (e) {
  35. if (app.globalData.openGZH) {
  36. this.OpenWithdraw()
  37. app.globalData.openGZH = false
  38. } else {
  39. this.setData({
  40. howToWithdraw: false
  41. })
  42. }
  43. let timer = setInterval(() => {
  44. console.log(app.globalData.PageCallback)
  45. if (app.globalData.PageCallback) {
  46. this.getGamePlayList()
  47. api.getUserInfo().then(res => {
  48. this.setData({
  49. userInfo: res.data,
  50. auth: res.data.auth
  51. })
  52. })
  53. if (this.data.auth.memcenter === 1) {
  54. this.getCenterInfo()
  55. }
  56. clearInterval(timer)
  57. }
  58. }, 200)
  59. },
  60. getShareInfo: function () {
  61. api.getShareInfo({
  62. data: {
  63. path: this.data.sharePath
  64. }
  65. }).then(res => {
  66. this.setData({
  67. shareInfo: res.data
  68. })
  69. }).catch(err => {
  70. console.log('getShare', err)
  71. })
  72. },
  73. contact: function (e) {
  74. console.log('客服消息回调', e)
  75. },
  76. routeTo: function (e) {
  77. util.routeTo(e.currentTarget.dataset.url)
  78. },
  79. OpenWithdraw: function (e) {
  80. console.log(this.data.qrCode)
  81. if (!this.data.qrCode) {
  82. api.getWithdrawTips().then(res => {
  83. if (res.code === 200) {
  84. this.setData({
  85. howToWithdraw: true,
  86. qrCode: res.data.image
  87. })
  88. }
  89. })
  90. } else {
  91. this.setData({
  92. howToWithdraw: true
  93. })
  94. }
  95. },
  96. CloseWithdraw: function (e) {
  97. app.globalData.oepnGZH = false
  98. this.setData({
  99. howToWithdraw: false
  100. })
  101. },
  102. // 评论/客服
  103. getCenterInfo: function () {
  104. api.getCommentIndex().then(res => {
  105. if (res.code === 200) {
  106. this.setData({
  107. CenterData: res.data
  108. })
  109. }
  110. })
  111. },
  112. // 我玩过的游戏
  113. getGamePlayList: function () {
  114. api.getGamePlayList({
  115. page: 1,
  116. offset: 4,
  117. HideLoading: true
  118. }).then(res => {
  119. console.log(res, '----------------')
  120. if (!res.data.list.length) {
  121. api.getGameList({
  122. rank_type: 3,
  123. page: 1,
  124. offset: 4,
  125. HideLoading: true
  126. }).then(res => {
  127. this.setData({
  128. GamePlayList: res.data.list,
  129. showText: true
  130. })
  131. })
  132. } else {
  133. this.setData({
  134. GamePlayList: res.data.list,
  135. showText: false
  136. })
  137. }
  138. })
  139. },
  140. // 领取任务奖励
  141. getTaskReward: function (e) {
  142. new Promise((resolve, reject) => {
  143. switch (e.currentTarget.dataset.tasktype) {
  144. case 'sign':
  145. api.getSignIncome().then(res => {
  146. if (res.code === 200) {
  147. resolve({ title: '签到奖励', content: this.data.mpType === 'gd' ? `恭喜获得${res.data.gain_integral}金币!` : `恭喜获得${res.data.gain_amount}元!` })
  148. }
  149. })
  150. break;
  151. case 'share':
  152. api.getShareIncome().then(res => {
  153. if (res.code === 200) {
  154. resolve({ title: '邀请奖励', content: this.data.mpType === 'gd' ? `恭喜获得${res.data.gain_integral}金币!` : `恭喜获得${res.data.gain_amount}元!` })
  155. }
  156. })
  157. break;
  158. case 'play':
  159. api.getPlayIncome().then(res => {
  160. if (res.code === 200) {
  161. resolve({ title: '试玩奖励', content: this.data.mpType === 'gd' ? `恭喜获得${res.data.gain_integral}金币!` : `恭喜获得${res.data.gain_amount}元!` })
  162. }
  163. })
  164. break;
  165. }
  166. }).then(info => {
  167. const _self = this
  168. wx.showModal({
  169. title: info.title,
  170. content: info.content,
  171. showCancel: false,
  172. success: (e) => {
  173. api.getUserInfo().then(res => {
  174. _self.updateUserInfo(res.data)
  175. })
  176. }
  177. })
  178. })
  179. },
  180. // 更新用户信息
  181. updateUserInfo: function (data) {
  182. this.setData({
  183. userInfo: data,
  184. auth: data.auth
  185. })
  186. },
  187. openGame: function (e) {
  188. let id = e.currentTarget.dataset.id
  189. api.openGame({
  190. game_id: id
  191. }).then(res => {
  192. console.log(res)
  193. }, err => {
  194. console.log(err)
  195. })
  196. },
  197. hideWithdraw: function (e) {
  198. this.setData({
  199. howToWithdraw: false
  200. })
  201. },
  202. showPop: function (e) {
  203. wx.previewImage({
  204. urls: [e.currentTarget.dataset.entrance]
  205. })
  206. },
  207. // 跳转赚钱技巧页
  208. routeTips: function (e) {
  209. if (this.data.mpType === 'gd') {
  210. util.routeTo('/pages/lottery/lottery')
  211. } else {
  212. util.routeTo('/pages/user/purse')
  213. }
  214. },
  215. // 显示隐藏分享menu
  216. ChangeShare: function (e) {
  217. wx.hideTabBar({
  218. animation: true
  219. })
  220. if (!this.data.shareInfo.title) {
  221. api.getShareInfo({
  222. data: {
  223. path: this.data.sharePath
  224. }
  225. }).then(res => {
  226. console.log('getShare', res)
  227. this.setData({
  228. shareInfo: res.data,
  229. ShowShareMask: e.currentTarget.dataset.show ? true : false
  230. })
  231. }).catch(err => {
  232. console.log('getShare', err)
  233. })
  234. } else {
  235. this.setData({
  236. ShowShareMask: e.currentTarget.dataset.show ? true : false
  237. })
  238. }
  239. this.getShareInfo()
  240. },
  241. // 显示隐藏分享弹框
  242. showShare: function (e) {
  243. let { show } = e.currentTarget.dataset
  244. if (!show) {
  245. wx.showTabBar({
  246. animation: true
  247. })
  248. }
  249. this.setData({
  250. ShowShareMask: show ? true : false
  251. })
  252. },
  253. // 分享朋友圈
  254. shareCircles: function () {
  255. console.log(this.data.shareImgSrc)
  256. if (this.data.shareImgSrc) {
  257. this.setData({
  258. ShowShareMask: false,
  259. ShowshareCirclesMask: true
  260. })
  261. return false
  262. }
  263. wx.showLoading({
  264. title: '正在生成',
  265. mask: true
  266. })
  267. this.setData({
  268. ShowShareMask: false,
  269. ShowshareCirclesMask: true
  270. })
  271. const _self = this
  272. const rpx = this.data.system.windowWidth / 750
  273. const ctx = wx.createCanvasContext('shareCanvas')
  274. const canvasW = 466 * rpx
  275. const canvasH = 828 * rpx
  276. const avatarW = 60 * rpx
  277. const avatarH = 60 * rpx
  278. const avatarX = 203 * rpx
  279. const avatarY = 23 * rpx
  280. const avatarBW = 66 * rpx
  281. const avatarBH = 66 * rpx
  282. const avatarBX = 200 * rpx
  283. const avatarBY = 20 * rpx
  284. const QRcodeW = 137 * rpx
  285. const QRcodeH = 137 * rpx
  286. const QRcodeX = 165 * rpx
  287. const QRcodeY = 585 * rpx
  288. const QRcodeBW = 147 * rpx
  289. const QRcodeBH = 147 * rpx
  290. const QRcodeBX = 160 * rpx
  291. const QRcodeBY = 580 * rpx
  292. util.getImageInfo(this.data.shareInfo.pyq_bg).then(res => {
  293. ctx.drawImage(res.path, 0, 0, canvasW, canvasH)
  294. ctx.save()
  295. ctx.beginPath()
  296. ctx.arc(avatarBW / 2 + avatarBX, avatarBH / 2 + avatarBY, avatarBW / 2, 0, Math.PI * 2, false)
  297. ctx.clip()
  298. ctx.setFillStyle('white')
  299. ctx.fillRect(avatarBX, avatarBY, avatarBW, avatarBH)
  300. ctx.restore()
  301. ctx.draw(true)
  302. ctx.save()
  303. ctx.beginPath()
  304. ctx.arc(QRcodeBW / 2 + QRcodeBX, QRcodeBH / 2 + QRcodeBY, QRcodeBW / 2, 0, Math.PI * 2, false)
  305. ctx.clip()
  306. ctx.setFillStyle('white')
  307. ctx.fillRect(QRcodeBX, QRcodeBY, QRcodeBW, QRcodeBH)
  308. ctx.restore()
  309. ctx.draw(true)
  310. }).then(res => {
  311. return util.getImageInfo(this.data.shareInfo.mp_qr).then(res => {
  312. ctx.save()
  313. ctx.beginPath()
  314. ctx.arc(QRcodeW / 2 + QRcodeX, QRcodeH / 2 + QRcodeY, QRcodeW / 2, 0, Math.PI * 2, false)
  315. ctx.clip()
  316. ctx.drawImage(res.path, canvasW / 2 - QRcodeW / 2, QRcodeY, QRcodeW, QRcodeH)
  317. ctx.restore()
  318. ctx.draw(true)
  319. })
  320. }).then(res => {
  321. return util.getImageInfo(this.data.userInfo.avatar).then(res => {
  322. ctx.beginPath()
  323. ctx.arc(avatarW / 2 + avatarX, avatarH / 2 + avatarY, avatarW / 2, 0, Math.PI * 2, false)
  324. ctx.clip()
  325. ctx.drawImage(res.path, canvasW / 2 - avatarW / 2, avatarY, avatarW, avatarH)
  326. ctx.restore()
  327. ctx.draw(true)
  328. })
  329. }).then(res => {
  330. setTimeout(() => {
  331. wx.canvasToTempFilePath({
  332. x: 0,
  333. y: 0,
  334. width: canvasW,
  335. height: canvasH,
  336. destWidth: canvasW * 4,
  337. destHeight: canvasH * 4,
  338. canvasId: 'shareCanvas',
  339. success: function (res) {
  340. wx.hideLoading()
  341. _self.setData({
  342. shareImgSrc: res.tempFilePath
  343. })
  344. },
  345. fail: function (res) {
  346. console.log(res)
  347. }
  348. })
  349. }, 500)
  350. }).catch(err => {
  351. wx.hideLoading()
  352. console.log(err)
  353. })
  354. },
  355. // 保存分享朋友圈图片
  356. saveMemQRcode: function (e) {
  357. const _self = this
  358. wx.showLoading({
  359. title: '正在保存...'
  360. })
  361. wx.saveImageToPhotosAlbum({
  362. filePath: _self.data.shareImgSrc,
  363. success: function (res) {
  364. console.log('保存图片', res)
  365. wx.showToast({
  366. title: '保存成功',
  367. icon: 'success',
  368. duration: 1000
  369. })
  370. },
  371. fail: function (err) {
  372. console.log('保存图片', err)
  373. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
  374. console.log("用户一开始拒绝了,我们想再次发起授权")
  375. console.log('打开设置窗口')
  376. wx.openSetting({
  377. success(settingdata) {
  378. console.log(settingdata)
  379. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  380. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  381. } else {
  382. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  383. }
  384. }
  385. })
  386. }
  387. },
  388. complete: function () {
  389. wx.hideLoading()
  390. _self.setData({
  391. ShowshareCirclesMask: false
  392. })
  393. wx.showTabBar({
  394. aniamtion: true
  395. })
  396. }
  397. })
  398. },
  399. // 获取设备信息
  400. getSystem: function (e) {
  401. const _self = this
  402. wx.getSystemInfo({
  403. success: function (res) {
  404. _self.setData({
  405. system: res
  406. })
  407. }
  408. })
  409. }
  410. })