find.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. shareCircles: function () {
  210. console.log(this.data.shareImgSrc)
  211. if (this.data.shareImgSrc) {
  212. this.setData({
  213. ShowShareMask: false,
  214. ShowshareCirclesMask: true
  215. })
  216. return false
  217. }
  218. wx.showLoading({
  219. title: '正在生成',
  220. mask: true
  221. })
  222. this.setData({
  223. ShowShareMask: false,
  224. ShowshareCirclesMask: true
  225. })
  226. const _self = this
  227. const rpx = this.data.system.windowWidth / 750
  228. const ctx = wx.createCanvasContext('shareCanvas')
  229. const canvasW = 466 * rpx
  230. const canvasH = 828 * rpx
  231. const avatarW = 60 * rpx
  232. const avatarH = 60 * rpx
  233. const avatarX = 203 * rpx
  234. const avatarY = 23 * rpx
  235. const avatarBW = 66 * rpx
  236. const avatarBH = 66 * rpx
  237. const avatarBX = 200 * rpx
  238. const avatarBY = 20 * rpx
  239. const QRcodeW = 137 * rpx
  240. const QRcodeH = 137 * rpx
  241. const QRcodeX = 165 * rpx
  242. const QRcodeY = 585 * rpx
  243. const QRcodeBW = 147 * rpx
  244. const QRcodeBH = 147 * rpx
  245. const QRcodeBX = 160 * rpx
  246. const QRcodeBY = 580 * rpx
  247. util.getImageInfo(this.data.shareInfo.pyq_bg).then(res => {
  248. ctx.drawImage(res.path, 0, 0, canvasW, canvasH)
  249. ctx.save()
  250. ctx.beginPath()
  251. ctx.arc(avatarBW / 2 + avatarBX, avatarBH / 2 + avatarBY, avatarBW / 2, 0, Math.PI * 2, false)
  252. ctx.clip()
  253. ctx.setFillStyle('white')
  254. ctx.fillRect(avatarBX, avatarBY, avatarBW, avatarBH)
  255. ctx.restore()
  256. ctx.draw(true)
  257. ctx.save()
  258. ctx.beginPath()
  259. ctx.arc(QRcodeBW / 2 + QRcodeBX, QRcodeBH / 2 + QRcodeBY, QRcodeBW / 2, 0, Math.PI * 2, false)
  260. ctx.clip()
  261. ctx.setFillStyle('white')
  262. ctx.fillRect(QRcodeBX, QRcodeBY, QRcodeBW, QRcodeBH)
  263. ctx.restore()
  264. ctx.draw(true)
  265. }).then(res => {
  266. return util.getImageInfo(this.data.shareInfo.mp_qr).then(res => {
  267. ctx.save()
  268. ctx.beginPath()
  269. ctx.arc(QRcodeW / 2 + QRcodeX, QRcodeH / 2 + QRcodeY, QRcodeW / 2, 0, Math.PI * 2, false)
  270. ctx.clip()
  271. ctx.drawImage(res.path, canvasW / 2 - QRcodeW / 2, QRcodeY, QRcodeW, QRcodeH)
  272. ctx.restore()
  273. ctx.draw(true)
  274. })
  275. }).then(res => {
  276. return util.getImageInfo(this.data.userInfo.avatar).then(res => {
  277. ctx.beginPath()
  278. ctx.arc(avatarW / 2 + avatarX, avatarH / 2 + avatarY, avatarW / 2, 0, Math.PI * 2, false)
  279. ctx.clip()
  280. ctx.drawImage(res.path, canvasW / 2 - avatarW / 2, avatarY, avatarW, avatarH)
  281. ctx.restore()
  282. ctx.draw(true)
  283. })
  284. }).then(res => {
  285. setTimeout(() => {
  286. wx.canvasToTempFilePath({
  287. x: 0,
  288. y: 0,
  289. width: canvasW,
  290. height: canvasH,
  291. destWidth: canvasW * 4,
  292. destHeight: canvasH * 4,
  293. canvasId: 'shareCanvas',
  294. success: function (res) {
  295. wx.hideLoading()
  296. _self.setData({
  297. shareImgSrc: res.tempFilePath
  298. })
  299. },
  300. fail: function (res) {
  301. console.log(res)
  302. }
  303. })
  304. }, 500)
  305. }).catch(err => {
  306. wx.hideLoading()
  307. console.log(err)
  308. })
  309. },
  310. // 保存分享朋友圈图片
  311. saveMemQRcode: function (e) {
  312. const _self = this
  313. wx.showLoading({
  314. title: '正在保存...'
  315. })
  316. wx.saveImageToPhotosAlbum({
  317. filePath: _self.data.shareImgSrc,
  318. success: function (res) {
  319. console.log('保存图片', res)
  320. wx.showToast({
  321. title: '保存成功',
  322. icon: 'success',
  323. duration: 1000
  324. })
  325. },
  326. fail: function (err) {
  327. console.log('保存图片', err)
  328. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
  329. console.log("用户一开始拒绝了,我们想再次发起授权")
  330. console.log('打开设置窗口')
  331. wx.openSetting({
  332. success(settingdata) {
  333. console.log(settingdata)
  334. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  335. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  336. } else {
  337. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  338. }
  339. }
  340. })
  341. }
  342. },
  343. complete: function () {
  344. wx.hideLoading()
  345. _self.setData({
  346. ShowshareCirclesMask: false
  347. })
  348. wx.showTabBar({
  349. aniamtion: true
  350. })
  351. }
  352. })
  353. },
  354. // 获取设备信息
  355. getSystem: function (e) {
  356. const _self = this
  357. wx.getSystemInfo({
  358. success: function (res) {
  359. _self.setData({
  360. system: res
  361. })
  362. }
  363. })
  364. }
  365. })