share.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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.setFontSize(16)
  99. ctx.setFillStyle('#434343')
  100. ctx.fillText(`《${this.data.title}》`, (300 - ctx.measureText(`《${this.data.title}》`).width) / 2, 285)
  101. // 二维码
  102. ctx.drawImage(this.data.QRCodeImagePath, 104, 299, 92, 92);
  103. ctx.draw();
  104. } else {
  105. ctx.save()
  106. ctx.beginPath()
  107. ctx.fill()
  108. ctx.setFontSize(25)
  109. ctx.setFillStyle('#fff')
  110. ctx.fillText('课文朗读 每天5分钟', (300 - ctx.measureText('课文朗读 每天5分钟').width) / 2, 54.5)
  111. ctx.setFontSize(16)
  112. let randomSologan = this.data.randomSologan[Math.floor(Math.random() * 10)];
  113. ctx.fillText(randomSologan, (300 - ctx.measureText(randomSologan).width) / 2, 75.5)
  114. ctx.setFontSize(15)
  115. ctx.setFillStyle('#838383')
  116. ctx.fillText('长按识别二维码', 130.5, 391)
  117. ctx.setFontSize(12)
  118. ctx.setFillStyle('#838383')
  119. ctx.fillText('接收邀请,获得你的课程吧', 130.5, 409.5) /* */
  120. // 头像
  121. console.log('画头像')
  122. if (this.data.shareType === 'works') {
  123. ctx.arc(150, 160, 36, 0, 2 * Math.PI, true)
  124. ctx.clip(); //剪切头像区域
  125. ctx.drawImage(this.data.avatar, 116, 126, 68, 68);
  126. ctx.arc(150, 160, 34, 0, 2 * Math.PI, false)
  127. ctx.setFillStyle('#ff6f42')
  128. ctx.fill();
  129. ctx.clip(); //剪切头像区域
  130. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  131. ctx.setFontSize(15)
  132. ctx.setFillStyle('#ff6f42')
  133. ctx.fillText(this.data.author, (300 - ctx.measureText(this.data.author).width) / 2, 220)
  134. ctx.setFillStyle('#434343')
  135. ctx.fillText('播音主持特长生', (300 - ctx.measureText('播音主持特长生').width) / 2, 243)
  136. ctx.setFontSize(15)
  137. ctx.setFillStyle('#434343')
  138. ctx.fillText('使出洪荒之力,声情并茂地朗诵了', (300 - ctx.measureText('使出洪荒之力,声情并茂地朗诵了').width) / 2, 262)
  139. ctx.fillText(`《${this.data.title}》`, (300 - ctx.measureText(`《${this.data.title}》`).width) / 2, 284)
  140. } else {
  141. ctx.arc(64, 146, 29, 0, 2 * Math.PI, true)
  142. ctx.clip(); //剪切头像区域
  143. ctx.drawImage(this.data.avatar, 37, 119, 54, 54);
  144. ctx.arc(64, 146, 27, 0, 2 * Math.PI, false)
  145. ctx.setFillStyle('#ff6f42')
  146. ctx.fill();
  147. ctx.clip(); //剪切头像区域
  148. //插入图片
  149. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  150. ctx.setFontSize(18)
  151. ctx.setFillStyle('#ff6f42')
  152. ctx.fillText(this.data.author, 100, 143.5)
  153. ctx.setFontSize(16)
  154. ctx.setFillStyle('#434343')
  155. ctx.fillText('邀您和我一起大声朗读', 100, 167.5)
  156. ctx.drawImage(this.data.iconImg, 27, 194, 247, 103.5);
  157. ctx.setFontSize(15)
  158. ctx.setFillStyle('#434343')
  159. ctx.fillText('朗朗读书声,是世界上最美妙的声音', (300 - ctx.measureText('朗朗读书声,是世界上最美妙的声音').width) / 2, 327.5)
  160. }
  161. console.log('花二维码')
  162. console.log(this.data)
  163. console.log(this.data.avatar)
  164. ctx.drawImage(this.data.QRCodeImagePath, 35, 351, 90, 90);
  165. ctx.draw();
  166. }
  167. setTimeout(() => {
  168. wx.canvasToTempFilePath({
  169. x: 0,
  170. y: 0,
  171. width: 300,
  172. height: 525,
  173. destWidth: 1200,
  174. destHeight: 2100,
  175. canvasId: 'myCanvas',
  176. success: (res) => {
  177. this.setData({
  178. imgUrls: res.tempFilePath
  179. })
  180. wx.hideLoading()
  181. },
  182. fail: (res) => {
  183. console.log('史称失败', res)
  184. wx.hideLoading();
  185. }
  186. })
  187. }, 400)
  188. },
  189. saveIcon: function () {
  190. wx.downloadFile({
  191. url: this.data.iconImg,
  192. success: res => {
  193. this.setData({
  194. iconImg: res.tempFilePath
  195. }, () => {
  196. console.log(this.data.iconImg)
  197. this.makeImage();
  198. })
  199. }
  200. });
  201. },
  202. /** 需要优化 */
  203. saveAvatar: function () {
  204. wx.downloadFile({
  205. url: this.data.avatar,
  206. success: res => {
  207. this.setData({
  208. avatar: res.tempFilePath
  209. }, () => {
  210. wx.downloadFile({
  211. url: 'https://reader-wx.ai160.com/images/reader/activity/activity_bg.jpg',
  212. success: res => {
  213. this.setData({
  214. activityImg: res.tempFilePath
  215. }, () => {
  216. if (this.data.shareType === 'group') {
  217. this.saveIcon();
  218. } else {
  219. this.makeImage();
  220. }
  221. })
  222. }
  223. })
  224. })
  225. }
  226. });
  227. },
  228. share: function (data) {
  229. console.log('分享内容', data)
  230. if (this.data.shareType === 'works') {
  231. this.setData({
  232. shareFlag: !this.data.shareFlag,
  233. avatar: data.avatar,
  234. author: data.author,
  235. iconImg: data.iconImg,
  236. title: data.title,
  237. productId: data.productId,
  238. id: data.scene,
  239. type: data.type,
  240. grade: data.grade,
  241. shareImg: data.shareImg,
  242. shareStep: 'firend',
  243. shareIndex: data.index,
  244. activity: data.activity,
  245. QRData: {
  246. page: data.path,
  247. scene: data.scene
  248. }
  249. })
  250. }
  251. if (this.data.shareType === 'group') {
  252. console.log(data)
  253. console.log('团购分享')
  254. this.setData({
  255. shareFlag: !this.data.shareFlag,
  256. avatar: data.avatar,
  257. author: data.author,
  258. iconImg: data.iconImg,
  259. title: data.title,
  260. // path: data.path,
  261. QRData: {
  262. page: data.path,
  263. scene: data.scene
  264. },
  265. groupPurchaseInfo: data.groupPurchaseInfo,
  266. groupType: data.groupType
  267. })
  268. }
  269. if (this.data.shareType === 'class') {
  270. console.log('课程分享')
  271. this.setData({
  272. shareFlag: !this.data.shareFlag
  273. })
  274. }
  275. // 动画测试
  276. this.animation = wx.createAnimation({
  277. duration: 700,
  278. timingFunction: 'ease',
  279. })
  280. this.animation.bottom(0).step();
  281. this.setData({
  282. animationData: this.animation.export()
  283. })
  284. // 动画测试
  285. // this.poster();
  286. },
  287. //关闭弹框
  288. close: function (e) {
  289. this.animation = wx.createAnimation({
  290. duration: 400,
  291. timingFunction: 'ease',
  292. })
  293. this.animation.bottom(-300).step();
  294. this.setData({
  295. animationData: this.animation.export()
  296. }, () => {
  297. setTimeout(() => {
  298. this.setData({
  299. shareFlag: !this.data.shareFlag,
  300. imgUrls: ''
  301. })
  302. this.triggerEvent('ShareDialogClose', {})
  303. }, 200)
  304. })
  305. wx.hideLoading()
  306. },
  307. nothing: function (e) {
  308. return
  309. },
  310. // 保存最终图片
  311. PreservationImg: function () {
  312. // this.getImgAuthorize()
  313. wx.saveImageToPhotosAlbum({
  314. filePath: this.data.imgUrls,
  315. success: (res) => {
  316. wx.showModal({
  317. title: '海报已保存至相册',
  318. content: '快去分享给小伙伴吧',
  319. confirmText: '我知道了',
  320. showCancel: false,
  321. success: (res) => {
  322. console.log('用户点击确定')
  323. this.close();
  324. httpRequestApi.shareImgLog({
  325. readId: this.data.id,
  326. }).success((oRes) => {
  327. this.triggerEvent('addShareAmount', {
  328. index:this.data.shareIndex
  329. })
  330. setTimeout(() => {
  331. if (oRes.data.count > 0) {
  332. this.setData({
  333. flowerNum: oRes.data.count
  334. })
  335. this.flowerAnimationHandler();
  336. }
  337. }, 500)
  338. })
  339. }
  340. })
  341. this.setData({
  342. imgUrls: false
  343. })
  344. },
  345. fail: res => {
  346. wx.getSetting({
  347. success(res) {
  348. if (!res.authSetting['scope.writePhotosAlbum']) {
  349. // wx.showToast({
  350. // title: '无权限操作',
  351. // icon: 'fail',
  352. // duration: 800
  353. // })
  354. wx.showModal({
  355. title: '无法保存分享图片到相册',
  356. content: '点击右上角浮点按钮->设置,进行授权',
  357. confirmText: '我知道了',
  358. showCancel: false,
  359. success(res) {
  360. console.log('用户点击确定')
  361. }
  362. })
  363. }
  364. }
  365. })
  366. }
  367. })
  368. },
  369. shareFriend: function () {
  370. this.setData({
  371. shareStep: 'circle'
  372. })
  373. this.poster();
  374. httpRequestApi.shareLog({
  375. readId: this.data.id,
  376. type: this.data.type,
  377. grade: this.data.grade
  378. }).success((res) => {
  379. console.log('分享记录', res)
  380. if (res.data.count > 0) {
  381. this.setData({
  382. flowerNum: res.data.count,
  383. }, () => {
  384. // this.flowerAnimationHandler();
  385. })
  386. }
  387. this.triggerEvent('addShareAmount', {
  388. index:this.data.shareIndex
  389. })
  390. })
  391. },
  392. flowerAnimationHandler: function () {
  393. this.flowerBox = this.selectComponent("#flower-toast");
  394. console.log('this.flower', this.flowerBox)
  395. this.flowerBox.comeOut();
  396. },
  397. },
  398. onShareAppMessage: (e) => {
  399. console.log('点击分享按钮', e)
  400. if (res.from === 'button') {
  401. // 来自页面内转发按钮
  402. console.log(res.target)
  403. } else {
  404. return {
  405. title: '课文朗读,从未如此有趣。',
  406. path: '/pages/index/index'
  407. }
  408. }
  409. }
  410. })