find.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // pages/find/find.js
  2. import util from '../../utils/util.js'
  3. import api from '../../utils/api.js'
  4. const app = getApp()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. auth: {},
  11. userInfo: {},
  12. // scrollTop: 0,
  13. scrollTime: 0,
  14. redStyle: '',
  15. params: {
  16. page: 1,
  17. offset: 10
  18. },
  19. result: [],
  20. count: 0,
  21. system: {},
  22. sharePath: 'pages/share/share',
  23. shareInfo: {},
  24. ShowShareMask: false,
  25. ShowshareCirclesMask: false,
  26. day: '',
  27. mpType: app.globalData.mpType
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad: function (options) {
  33. let timer = setInterval(() => {
  34. if (app.globalData.PageCallback) {
  35. this.setData({
  36. userInfo: app.globalData.userInfo,
  37. auth: app.globalData.auth,
  38. })
  39. this.getShareInfo()
  40. this.getFindPageData()
  41. this.getSystem()
  42. clearInterval(timer)
  43. }
  44. }, 200)
  45. },
  46. onShow: function () {
  47. wx.showShareMenu({ // 允许转发时携带 shareTicket。
  48. withShareTicket: true
  49. })
  50. api.getUserInfo().then(res => {
  51. this.setData({
  52. userInfo: res.data,
  53. auth: res.data.auth
  54. })
  55. }, err => {
  56. console.log(err)
  57. })
  58. },
  59. onPageScroll: function (res) {
  60. let { scrollTime } = this.data
  61. if (scrollTime) {
  62. clearTimeout(scrollTime)
  63. }
  64. let _t = setTimeout(() => {
  65. this.setData({
  66. redStyle: ''
  67. })
  68. }, 1000)
  69. // 滑动则隐藏红包、收藏
  70. this.setData({
  71. redStyle: 'slide-right',
  72. // scrollTop: res.scrollTop,
  73. scrollTime: _t
  74. })
  75. },
  76. getDay: function () {
  77. var date = new Date();
  78. var seperator1 = "-";
  79. var year = date.getFullYear();
  80. var month = date.getMonth() + 1;
  81. var strDate = date.getDate();
  82. if (month >= 1 && month <= 9) {
  83. month = "0" + month;
  84. }
  85. if (strDate >= 0 && strDate <= 9) {
  86. strDate = "0" + strDate;
  87. }
  88. var currentdate = year + seperator1 + month + seperator1 + strDate;
  89. return currentdate;
  90. },
  91. getShareInfo: function () {
  92. api.getShareInfo({
  93. data: {
  94. path: this.data.sharePath
  95. }
  96. }).then(res => {
  97. this.setData({
  98. shareInfo: res.data
  99. })
  100. }).catch(err => {
  101. console.log('getShare', err)
  102. })
  103. },
  104. getFindPageData () {
  105. api.getFindGameList(this.data.params).then(res => {
  106. this.setData({
  107. result: this.data.params.page > 1 ? this.data.result.concat(res.data.list) : res.data.list,
  108. count: res.data.count
  109. })
  110. })
  111. this.setData({
  112. day: this.getDay()
  113. })
  114. },
  115. openGame: function (e) {
  116. let id = e.currentTarget.dataset.id
  117. api.openGame({
  118. game_id: id
  119. }).then(res => {
  120. console.log(res)
  121. }, err => {
  122. console.log(err)
  123. })
  124. },
  125. // 跳转赚钱技巧页
  126. routeTips: function (e) {
  127. if (this.data.mpType === 'gd') {
  128. util.routeTo('/pages/lottery/lottery')
  129. } else {
  130. util.routeTo('/pages/user/purse')
  131. }
  132. },
  133. /**
  134. * 页面上拉触底事件的处理函数
  135. */
  136. onReachBottom: function () {
  137. if (this.data.result.length >= this.data.count) {
  138. return false
  139. }
  140. this.data.params.page++
  141. this.setData({
  142. params: this.data.params
  143. })
  144. this.getFindPageData()
  145. },
  146. // 页面相关事件处理函数--监听用户下拉动作
  147. onPullDownRefresh: function () {
  148. this.setData({
  149. params: {
  150. page: 1,
  151. offset: 10
  152. }
  153. })
  154. api.getFindGameList({ ...this.data.params, HideLoading: true}).then(res => {
  155. this.setData({
  156. result: res.data.list,
  157. count: res.data.count
  158. })
  159. })
  160. this.setData({
  161. day: this.getDay()
  162. })
  163. wx.stopPullDownRefresh()
  164. },
  165. showPop: function (e) {
  166. wx.previewImage({
  167. urls: [e.currentTarget.dataset.entrance]
  168. })
  169. },
  170. // 显示隐藏分享menu
  171. ChangeShare: function (e) {
  172. wx.hideTabBar({
  173. animation: true
  174. })
  175. if (!this.data.shareInfo.title) {
  176. api.getShareInfo({
  177. data: {
  178. path: this.data.sharePath
  179. }
  180. }).then(res => {
  181. console.log('getShare', res)
  182. this.setData({
  183. shareInfo: res.data,
  184. ShowShareMask: e.currentTarget.dataset.show ? true : false
  185. })
  186. }).catch(err => {
  187. console.log('getShare', err)
  188. })
  189. } else {
  190. this.setData({
  191. ShowShareMask: e.currentTarget.dataset.show ? true : false
  192. })
  193. }
  194. this.getShareInfo()
  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. // 关闭分享朋友圈弹框
  209. CloseshareCircles: function () {
  210. wx.showTabBar({
  211. aniamtion: true
  212. })
  213. this.setData({
  214. ShowshareCirclesMask: false
  215. })
  216. },
  217. // 分享朋友圈
  218. shareCircles: function () {
  219. console.log(this.data.shareImgSrc)
  220. if (this.data.shareImgSrc) {
  221. this.setData({
  222. ShowShareMask: false,
  223. ShowshareCirclesMask: true
  224. })
  225. return false
  226. }
  227. wx.showLoading({
  228. title: '正在生成',
  229. mask: true
  230. })
  231. this.setData({
  232. ShowShareMask: false,
  233. ShowshareCirclesMask: true
  234. })
  235. const _self = this
  236. const rpx = this.data.system.windowWidth / 750
  237. const ctx = wx.createCanvasContext('shareCanvas')
  238. const canvasW = 466 * rpx
  239. const canvasH = 828 * rpx
  240. const avatarW = 70 * rpx
  241. const avatarH = 70 * rpx
  242. const avatarX = 200 * rpx
  243. const avatarY = 19 * rpx
  244. const avatarBW = 66 * rpx
  245. const avatarBH = 66 * rpx
  246. const avatarBX = 200 * rpx
  247. const avatarBY = 20 * rpx
  248. const QRcodeW = 454 * rpx
  249. const QRcodeH = 454 * rpx
  250. const QRcodeX = 6 * rpx
  251. const QRcodeY = 374 * rpx
  252. const QRcodeBW = 147 * rpx
  253. const QRcodeBH = 147 * rpx
  254. const QRcodeBX = 160 * rpx
  255. const QRcodeBY = 580 * rpx
  256. util.getImageInfo(this.data.shareInfo.pyq_bg).then(res => {
  257. ctx.drawImage(res.path, 0, 0, canvasW, canvasH)
  258. ctx.save()
  259. ctx.beginPath()
  260. ctx.arc(avatarBW / 2 + avatarBX, avatarBH / 2 + avatarBY, avatarBW / 2, 0, Math.PI * 2, false)
  261. ctx.clip()
  262. ctx.setFillStyle('white')
  263. ctx.fillRect(avatarBX, avatarBY, avatarBW, avatarBH)
  264. ctx.restore()
  265. ctx.draw(true)
  266. ctx.save()
  267. ctx.beginPath()
  268. ctx.arc(QRcodeBW / 2 + QRcodeBX, QRcodeBH / 2 + QRcodeBY, QRcodeBW / 2, 0, Math.PI * 2, false)
  269. ctx.clip()
  270. ctx.setFillStyle('white')
  271. ctx.fillRect(QRcodeBX, QRcodeBY, QRcodeBW, QRcodeBH)
  272. ctx.restore()
  273. ctx.draw(true)
  274. }).then(res => {
  275. return util.getImageInfo(this.data.shareInfo.mp_qr).then(res => {
  276. ctx.save()
  277. ctx.beginPath()
  278. ctx.arc(QRcodeW / 2 + QRcodeX, QRcodeH / 2 + QRcodeY, QRcodeW / 2, 0, Math.PI * 2, false)
  279. ctx.clip()
  280. ctx.drawImage(res.path, canvasW / 2 - QRcodeW / 2, QRcodeY, QRcodeW, QRcodeH)
  281. ctx.restore()
  282. ctx.draw(true)
  283. })
  284. }).then(res => {
  285. return util.getImageInfo(this.data.userInfo.avatar).then(res => {
  286. ctx.beginPath()
  287. ctx.arc(avatarW / 2 + avatarX, avatarH / 2 + avatarY, avatarW / 2, 0, Math.PI * 2, false)
  288. ctx.clip()
  289. ctx.drawImage(res.path, canvasW / 2 - avatarW / 2, avatarY, avatarW, avatarH)
  290. ctx.restore()
  291. ctx.draw(true)
  292. })
  293. }).then(res => {
  294. setTimeout(() => {
  295. wx.canvasToTempFilePath({
  296. x: 0,
  297. y: 0,
  298. width: canvasW,
  299. height: canvasH,
  300. destWidth: canvasW * 4,
  301. destHeight: canvasH * 4,
  302. canvasId: 'shareCanvas',
  303. success: function (res) {
  304. wx.hideLoading()
  305. _self.setData({
  306. shareImgSrc: res.tempFilePath
  307. })
  308. },
  309. fail: function (res) {
  310. console.log(res)
  311. }
  312. })
  313. }, 500)
  314. }).catch(err => {
  315. wx.hideLoading()
  316. console.log(err)
  317. })
  318. },
  319. // 保存分享朋友圈图片
  320. saveMemQRcode: function (e) {
  321. const _self = this
  322. wx.showLoading({
  323. title: '正在保存...'
  324. })
  325. wx.saveImageToPhotosAlbum({
  326. filePath: _self.data.shareImgSrc,
  327. success: function (res) {
  328. console.log('保存图片', res)
  329. wx.showToast({
  330. title: '保存成功',
  331. icon: 'success',
  332. duration: 1000
  333. })
  334. },
  335. fail: function (err) {
  336. console.log('保存图片', err)
  337. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
  338. console.log("用户一开始拒绝了,我们想再次发起授权")
  339. console.log('打开设置窗口')
  340. wx.openSetting({
  341. success(settingdata) {
  342. console.log(settingdata)
  343. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  344. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  345. } else {
  346. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  347. }
  348. }
  349. })
  350. }
  351. },
  352. complete: function () {
  353. wx.hideLoading()
  354. _self.setData({
  355. ShowshareCirclesMask: false
  356. })
  357. wx.showTabBar({
  358. aniamtion: true
  359. })
  360. }
  361. })
  362. },
  363. // 获取设备信息
  364. getSystem: function (e) {
  365. const _self = this
  366. wx.getSystemInfo({
  367. success: function (res) {
  368. _self.setData({
  369. system: res
  370. })
  371. }
  372. })
  373. }
  374. })