share.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // component/shar-dialog/shar-dialog.js
  2. import httpRequestApi from '../../utils/APIClient';
  3. const app = getApp();
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. shareType: {
  10. type: 'String',
  11. value: ''
  12. },
  13. shareId: {
  14. type: 'String',
  15. value: ''
  16. }
  17. },
  18. /**
  19. * 组件的初始数据
  20. */
  21. data: {
  22. shareFlag: false,
  23. imgUrls: '',
  24. avatar: '',
  25. author: '',
  26. iconImg: '',
  27. title: '',
  28. path: '',
  29. QRCodeImagePath: '',
  30. QRCodeImageLocalPath: '',
  31. activityImg: '',
  32. groupPurchaseInfo: {},
  33. groupType: '',
  34. animationData: {},
  35. touchMove: true,
  36. randomSologan: [
  37. '开发右脑最简单的方式就是大声朗读',
  38. '大声朗读 有利于建立自信心',
  39. '大声朗读 能帮助孩子提高写作能力',
  40. '朗读是感知世界的一种重要方式',
  41. '增强记忆力的方法之一大声朗读',
  42. '大声朗读是语感形成的最佳途径',
  43. '大声朗读 还原课文的诗性美',
  44. '在朗读中 提升孩子的思辨能力',
  45. '重温经典课文 享受亲子美好时光',
  46. '大声朗读 能帮助孩子提高写作能力'
  47. ],
  48. shareStep: 'firend',
  49. activity: ''
  50. },
  51. /**
  52. * 组件的方法列表
  53. */
  54. methods: {
  55. touchMove: function () {
  56. return false
  57. },
  58. poster: function () {
  59. const that = this;
  60. wx.showLoading({
  61. title: '专属海报生成中',
  62. // mask: true
  63. })
  64. if (this.data.shareType === 'readActivity') {
  65. httpRequestApi.getqrCodeShare(this.data.QRData).success(res => {
  66. console.log('----------------->分享二维码222',res.data.data)
  67. // 下载二维码
  68. wx.downloadFile({
  69. url: res.data.data,
  70. success: res => {
  71. console.log('----------------->分享二维码',res.tempFilePath)
  72. this.setData({
  73. QRCodeImagePath: res.tempFilePath
  74. }, () => {
  75. this.saveAvatar();
  76. })
  77. }
  78. });
  79. })
  80. } else {
  81. httpRequestApi.createQRCode(this.data.QRData).success(res => {
  82. console.log('----------------->分享二维码222',res.data.data)
  83. // 下载二维码
  84. wx.downloadFile({
  85. url: res.data.data,
  86. success: res => {
  87. console.log('----------------->分享二维码',res.tempFilePath)
  88. this.setData({
  89. QRCodeImagePath: res.tempFilePath
  90. }, () => {
  91. this.saveAvatar();
  92. })
  93. }
  94. });
  95. })
  96. }
  97. },
  98. makeImage: function () {
  99. const ctx = wx.createCanvasContext('myCanvas');
  100. // 画出背景和相同内容
  101. if(this.data.activity || this.data.shareType === 'readActivity') {
  102. ctx.drawImage(this.data.activityImg, 0, 0, 1200, 2100, 0, 0, 600, 1050); //插入图片
  103. } else {
  104. if (this.data.shareType === 'works') {
  105. ctx.drawImage('../../static/image/works_poster.jpg', 0, 0, 1200, 2100, 0, 0, 600, 1050); //插入图片
  106. } else {
  107. ctx.drawImage('../../static/image/group_poster.jpg', 0, 0, 1200, 2100, 0, 0, 600, 1050); //插入图片
  108. }
  109. }
  110. if (this.data.activity) {
  111. ctx.save()
  112. ctx.arc(49, 50, 29, 0, 2 * Math.PI, true)
  113. ctx.clip(); //剪切头像区域
  114. ctx.drawImage(this.data.avatar, 20, 20, 61, 61);
  115. ctx.clip(); //剪切头像区域
  116. ctx.restore();
  117. ctx.font = 'normal bold 14px'
  118. // ctx.setFontSize(14)
  119. // ctx.setFontWeight(500)
  120. ctx.setFillStyle('#198165')
  121. ctx.fillText(this.data.author, 82, 49)
  122. ctx.restore();
  123. ctx.setFontSize(16)
  124. ctx.setFillStyle('#434343')
  125. ctx.fillText(`《${this.data.title}》`, (300 - ctx.measureText(`《${this.data.title}》`).width) / 2, 285)
  126. // 二维码
  127. ctx.drawImage(this.data.QRCodeImagePath, 104, 299, 92, 92);
  128. ctx.draw();
  129. }else if(this.data.shareType === 'readActivity') {
  130. ctx.save()
  131. ctx.arc(49, 50, 29, 0, 2 * Math.PI, true)
  132. ctx.clip(); //剪切头像区域
  133. ctx.drawImage(this.data.avatar, 20, 20, 61, 61);
  134. ctx.clip(); //剪切头像区域
  135. ctx.restore();
  136. ctx.font = 'normal bold 14px'
  137. ctx.setFontSize(14)
  138. ctx.setFillStyle('#fff')
  139. ctx.fillText(this.data.name, 85, 59)
  140. ctx.restore();
  141. // 二维码
  142. ctx.drawImage(this.data.QRCodeImagePath, 11, 409, 92, 92);
  143. ctx.draw();
  144. } else {
  145. ctx.save()
  146. ctx.beginPath()
  147. ctx.fill()
  148. ctx.setFontSize(25)
  149. ctx.setFillStyle('#fff')
  150. ctx.fillText('课文朗读 每天5分钟', (300 - ctx.measureText('课文朗读 每天5分钟').width) / 2, 54.5)
  151. ctx.setFontSize(16)
  152. let randomSologan = this.data.randomSologan[Math.floor(Math.random() * 10)];
  153. ctx.fillText(randomSologan, (300 - ctx.measureText(randomSologan).width) / 2, 75.5)
  154. ctx.setFontSize(15)
  155. ctx.setFillStyle('#838383')
  156. ctx.fillText('长按识别二维码', 130.5, 391)
  157. ctx.setFontSize(12)
  158. ctx.setFillStyle('#838383')
  159. ctx.fillText('接收邀请,获得你的课程吧', 130.5, 409.5) /* */
  160. // 头像
  161. console.log('画头像')
  162. if (this.data.shareType === 'works') {
  163. ctx.arc(150, 160, 36, 0, 2 * Math.PI, true)
  164. ctx.clip(); //剪切头像区域
  165. ctx.drawImage(this.data.avatar, 116, 126, 68, 68);
  166. ctx.arc(150, 160, 34, 0, 2 * Math.PI, false)
  167. ctx.setFillStyle('#ff6f42')
  168. ctx.fill();
  169. ctx.clip(); //剪切头像区域
  170. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  171. ctx.setFontSize(15)
  172. ctx.setFillStyle('#ff6f42')
  173. ctx.fillText(this.data.author, (300 - ctx.measureText(this.data.author).width) / 2, 220)
  174. ctx.setFillStyle('#434343')
  175. ctx.fillText('播音主持特长生', (300 - ctx.measureText('播音主持特长生').width) / 2, 243)
  176. ctx.setFontSize(15)
  177. ctx.setFillStyle('#434343')
  178. ctx.fillText('使出洪荒之力,声情并茂地朗诵了', (300 - ctx.measureText('使出洪荒之力,声情并茂地朗诵了').width) / 2, 262)
  179. ctx.fillText(`《${this.data.title}》`, (300 - ctx.measureText(`《${this.data.title}》`).width) / 2, 284)
  180. } else {
  181. ctx.arc(64, 146, 29, 0, 2 * Math.PI, true)
  182. ctx.clip(); //剪切头像区域
  183. ctx.drawImage(this.data.avatar, 37, 119, 54, 54);
  184. ctx.arc(64, 146, 27, 0, 2 * Math.PI, false)
  185. ctx.setFillStyle('#ff6f42')
  186. ctx.fill();
  187. ctx.clip(); //剪切头像区域
  188. //插入图片
  189. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  190. ctx.setFontSize(18)
  191. ctx.setFillStyle('#ff6f42')
  192. ctx.fillText(this.data.author, 100, 143.5)
  193. ctx.setFontSize(16)
  194. ctx.setFillStyle('#434343')
  195. ctx.fillText('邀您和我一起大声朗读', 100, 167.5)
  196. ctx.drawImage(this.data.iconImg, 27, 194, 247, 103.5);
  197. ctx.setFontSize(15)
  198. ctx.setFillStyle('#434343')
  199. ctx.fillText('朗朗读书声,是世界上最美妙的声音', (300 - ctx.measureText('朗朗读书声,是世界上最美妙的声音').width) / 2, 327.5)
  200. }
  201. console.log('花二维码')
  202. console.log(this.data)
  203. console.log(this.data.avatar)
  204. ctx.drawImage(this.data.QRCodeImagePath, 35, 351, 90, 90);
  205. ctx.draw();
  206. }
  207. setTimeout(() => {
  208. wx.canvasToTempFilePath({
  209. x: 0,
  210. y: 0,
  211. width: 300,
  212. height: 525,
  213. destWidth: 1200,
  214. destHeight: 2100,
  215. canvasId: 'myCanvas',
  216. success: (res) => {
  217. this.setData({
  218. imgUrls: res.tempFilePath
  219. })
  220. wx.hideLoading()
  221. },
  222. fail: (res) => {
  223. console.log('史称失败', res)
  224. wx.hideLoading();
  225. }
  226. })
  227. }, 400)
  228. },
  229. saveIcon: function () {
  230. wx.downloadFile({
  231. url: this.data.iconImg,
  232. success: res => {
  233. this.setData({
  234. iconImg: res.tempFilePath
  235. }, () => {
  236. console.log(this.data.iconImg)
  237. this.makeImage();
  238. })
  239. }
  240. });
  241. },
  242. /** 需要优化 */
  243. saveAvatar: function () {
  244. wx.downloadFile({
  245. url: this.data.avatar,
  246. success: res => {
  247. this.setData({
  248. avatar: res.tempFilePath
  249. }, () => {
  250. const imgName = this.data.shareType === 'readActivity' ? 'pay/share.png' : 'activity/activity_bg.jpg'
  251. wx.downloadFile({
  252. url: `https://reader-wx.ai160.com/images/reader/${imgName}`,
  253. success: res => {
  254. this.setData({
  255. activityImg: res.tempFilePath
  256. }, () => {
  257. if (this.data.shareType === 'group') {
  258. this.saveIcon();
  259. } else {
  260. this.makeImage();
  261. }
  262. })
  263. }
  264. })
  265. })
  266. }
  267. });
  268. },
  269. share: function (data) {
  270. console.log('分享内容', data)
  271. if (this.data.shareType === 'works') {
  272. this.setData({
  273. shareFlag: !this.data.shareFlag,
  274. avatar: data.avatar,
  275. author: data.author,
  276. iconImg: data.iconImg,
  277. title: data.title,
  278. productId: data.productId,
  279. id: data.scene,
  280. type: data.type,
  281. grade: data.grade,
  282. shareImg: data.shareImg,
  283. shareStep: 'firend',
  284. shareIndex: data.index,
  285. activity: data.activity,
  286. QRData: {
  287. page: data.path,
  288. scene: data.scene
  289. }
  290. })
  291. }
  292. if (this.data.shareType === 'group') {
  293. console.log(data)
  294. console.log('团购分享')
  295. this.setData({
  296. shareFlag: !this.data.shareFlag,
  297. avatar: data.avatar,
  298. author: data.author,
  299. iconImg: data.iconImg,
  300. title: data.title,
  301. // path: data.path,
  302. QRData: {
  303. page: data.path,
  304. scene: data.scene
  305. },
  306. groupPurchaseInfo: data.groupPurchaseInfo,
  307. groupType: data.groupType
  308. })
  309. }
  310. /**添加团购分享海报为了进度代码越写越乱项目可重构 */
  311. if (this.data.shareType === 'readActivity') {
  312. console.log(data)
  313. this.setData({
  314. shareFlag: !this.data.shareFlag,
  315. avatar: data.avatar,
  316. name: data.name,
  317. QRData: {
  318. page: data.path,
  319. type: `${data.type}`
  320. }
  321. })
  322. this.poster();
  323. }
  324. if (this.data.shareType === 'class') {
  325. console.log('课程分享')
  326. this.setData({
  327. shareFlag: !this.data.shareFlag
  328. })
  329. }
  330. // 动画测试
  331. this.animation = wx.createAnimation({
  332. duration: 700,
  333. timingFunction: 'ease',
  334. })
  335. this.animation.bottom(0).step();
  336. this.setData({
  337. animationData: this.animation.export()
  338. })
  339. // 动画测试
  340. // this.poster();
  341. },
  342. //关闭弹框
  343. close: function (e) {
  344. this.animation = wx.createAnimation({
  345. duration: 400,
  346. timingFunction: 'ease',
  347. })
  348. this.animation.bottom(-300).step();
  349. this.setData({
  350. animationData: this.animation.export()
  351. }, () => {
  352. setTimeout(() => {
  353. this.setData({
  354. shareFlag: !this.data.shareFlag,
  355. imgUrls: ''
  356. })
  357. this.triggerEvent('ShareDialogClose', {})
  358. }, 200)
  359. })
  360. wx.hideLoading()
  361. },
  362. nothing: function (e) {
  363. return
  364. },
  365. // 保存最终图片
  366. PreservationImg: function () {
  367. // this.getImgAuthorize()
  368. wx.saveImageToPhotosAlbum({
  369. filePath: this.data.imgUrls,
  370. success: (res) => {
  371. wx.showModal({
  372. title: '海报已保存至相册',
  373. content: '快去分享给小伙伴吧',
  374. confirmText: '我知道了',
  375. showCancel: false,
  376. success: (res) => {
  377. console.log('用户点击确定')
  378. this.close();
  379. httpRequestApi.shareImgLog({
  380. readId: this.data.id,
  381. }).success((oRes) => {
  382. this.triggerEvent('addShareAmount', {
  383. index:this.data.shareIndex
  384. })
  385. setTimeout(() => {
  386. if (oRes.data.count > 0) {
  387. this.setData({
  388. flowerNum: oRes.data.count
  389. })
  390. this.flowerAnimationHandler();
  391. }
  392. }, 500)
  393. })
  394. }
  395. })
  396. this.setData({
  397. imgUrls: false
  398. })
  399. },
  400. fail: res => {
  401. wx.getSetting({
  402. success(res) {
  403. if (!res.authSetting['scope.writePhotosAlbum']) {
  404. // wx.showToast({
  405. // title: '无权限操作',
  406. // icon: 'fail',
  407. // duration: 800
  408. // })
  409. wx.showModal({
  410. title: '无法保存分享图片到相册',
  411. content: '点击右上角浮点按钮->设置,进行授权',
  412. confirmText: '我知道了',
  413. showCancel: false,
  414. success(res) {
  415. console.log('用户点击确定')
  416. }
  417. })
  418. }
  419. }
  420. })
  421. }
  422. })
  423. },
  424. shareFriend: function () {
  425. this.setData({
  426. shareStep: 'circle'
  427. })
  428. if (this.data.shareType !== 'readActivity') {
  429. this.poster();
  430. }
  431. httpRequestApi.shareLog({
  432. readId: this.data.id.replace('&activity=true',''),
  433. type: this.data.type,
  434. grade: this.data.grade
  435. }).success((res) => {
  436. console.log('分享记录', res)
  437. if (res.data.count > 0) {
  438. this.setData({
  439. flowerNum: res.data.count,
  440. }, () => {
  441. // this.flowerAnimationHandler();
  442. })
  443. }
  444. this.triggerEvent('addShareAmount', {
  445. index:this.data.shareIndex
  446. })
  447. })
  448. },
  449. flowerAnimationHandler: function () {
  450. this.flowerBox = this.selectComponent("#flower-toast");
  451. console.log('this.flower', this.flowerBox)
  452. this.flowerBox.comeOut();
  453. },
  454. },
  455. onShareAppMessage: (e) => {
  456. console.log('点击分享按钮', e)
  457. if (res.from === 'button') {
  458. // 来自页面内转发按钮
  459. console.log(res.target)
  460. } else {
  461. return {
  462. title: '课文朗读,从未如此有趣。',
  463. path: '/pages/index/index'
  464. }
  465. }
  466. }
  467. })