share.js 11 KB

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