share.js 13 KB

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