share.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. },
  35. /**
  36. * 组件的方法列表
  37. */
  38. methods: {
  39. touchMove: function () {
  40. return false
  41. },
  42. poster: function () {
  43. const ctx = wx.createCanvasContext('myCanvas');
  44. const that = this;
  45. wx.showLoading({
  46. title: '海报生成中',
  47. // mask: true
  48. })
  49. httpRequestApi.createQRCode(this.data.QRData).success(res => {
  50. // 下载二维码
  51. wx.downloadFile({
  52. url: res.data.data,
  53. success: res => {
  54. this.setData({
  55. QRCodeImagePath: res.tempFilePath
  56. })
  57. }
  58. });
  59. wx.downloadFile({
  60. url: that.data.avatar,
  61. success: res => {
  62. this.setData({
  63. avatar: res.tempFilePath
  64. })
  65. }
  66. });
  67. // 下载icon
  68. if (this.data.shareType === 'works') {
  69. wx.downloadFile({
  70. url: that.data.iconImg,
  71. success: res => {
  72. this.setData({
  73. iconImg: res.tempFilePath
  74. })
  75. }
  76. });
  77. }
  78. this.makeImage(ctx);
  79. })
  80. },
  81. makeImage: function (ctx) {
  82. // 画出背景和相同内容
  83. if (this.data.shareType === 'works') {
  84. ctx.drawImage('../../../static/image/works_poster.jpg', 0, 0, 1200, 2100, 0, 0, 600, 1050); //插入图片
  85. } else {
  86. ctx.drawImage('../../../static/image/group_poster.jpg', 0, 0, 1200, 2100, 0, 0, 600, 1050); //插入图片
  87. }
  88. ctx.save()
  89. ctx.beginPath()
  90. ctx.fill()
  91. ctx.setFontSize(25)
  92. ctx.setFillStyle('#fff')
  93. ctx.fillText('课文朗读 每天5分钟', (300 - ctx.measureText('课文朗读 每天5分钟').width) / 2, 54.5)
  94. ctx.setFontSize(16)
  95. ctx.fillText('大声朗读是语感形成的必走之路', (300 - ctx.measureText('大声朗读是语感形成的必走之路').width) / 2, 75.5)
  96. ctx.setFontSize(15)
  97. ctx.setFillStyle('#838383')
  98. ctx.fillText('长按识别二维码', 130.5, 391)
  99. ctx.setFontSize(12)
  100. ctx.setFillStyle('#838383')
  101. ctx.fillText('接收邀请,获得你的课程吧', 130.5, 409.5) /* */
  102. // 头像
  103. console.log('画头像')
  104. if (this.data.shareType === 'works') {
  105. ctx.arc(150, 160, 36, 0, 2 * Math.PI, true)
  106. ctx.clip(); //剪切头像区域
  107. ctx.drawImage(this.data.avatar, 116, 126, 68, 68);
  108. ctx.arc(150, 160, 34, 0, 2 * Math.PI, false)
  109. ctx.setFillStyle('#ff6f42')
  110. ctx.fill();
  111. ctx.clip(); //剪切头像区域
  112. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  113. ctx.setFontSize(15)
  114. ctx.setFillStyle('#ff6f42')
  115. ctx.fillText(this.data.author, (300 - ctx.measureText(this.data.author).width) / 2, 220)
  116. ctx.setFillStyle('#434343')
  117. ctx.fillText('播音主持特长生', (300 - ctx.measureText('播音主持特长生').width) / 2, 243)
  118. ctx.setFontSize(15)
  119. ctx.setFillStyle('#434343')
  120. ctx.fillText('使出洪荒之力,声情并茂地朗诵了', (300 - ctx.measureText('使出洪荒之力,声情并茂地朗诵了').width) / 2, 262)
  121. ctx.fillText(`《${this.data.title}》`, (300 - ctx.measureText(`《${this.data.title}》`).width) / 2, 284)
  122. } else {
  123. ctx.arc(64, 146, 29, 0, 2 * Math.PI, true)
  124. ctx.clip(); //剪切头像区域
  125. ctx.drawImage(this.data.avatar, 37, 119, 54, 54);
  126. ctx.arc(64, 146, 27, 0, 2 * Math.PI, false)
  127. ctx.setFillStyle('#ff6f42')
  128. ctx.fill();
  129. ctx.clip(); //剪切头像区域
  130. //插入图片
  131. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  132. ctx.setFontSize(18)
  133. ctx.setFillStyle('#ff6f42')
  134. ctx.fillText(this.data.author, 100, 143.5)
  135. ctx.setFontSize(16)
  136. ctx.setFillStyle('#434343')
  137. ctx.fillText('邀您和我一起大声朗读', 100, 167.5)
  138. ctx.drawImage(this.data.iconImg, 27, 194, 247, 103.5);
  139. ctx.setFontSize(15)
  140. ctx.setFillStyle('#434343')
  141. ctx.fillText('朗朗读书声,是世界上最美妙的声音', (300 - ctx.measureText('朗朗读书声,是世界上最美妙的声音').width) / 2, 327.5)
  142. }
  143. console.log('花二维码')
  144. console.log(this.data.QRCodeImagePath)
  145. ctx.drawImage(this.data.QRCodeImagePath, 35, 353, 90, 90);
  146. ctx.draw();
  147. setTimeout(() => {
  148. wx.canvasToTempFilePath({
  149. x: 0,
  150. y: 0,
  151. width: 300,
  152. height: 525,
  153. destWidth: 1200,
  154. destHeight: 2100,
  155. canvasId: 'myCanvas',
  156. success: (res) => {
  157. this.setData({
  158. imgUrls: res.tempFilePath
  159. })
  160. wx.hideLoading()
  161. },
  162. fail: (res) => {
  163. console.log(res)
  164. wx.hideLoading();
  165. }
  166. })
  167. }, 200)
  168. },
  169. share: function (data) {
  170. console.log(data)
  171. if (this.data.shareType === 'works') {
  172. this.setData({
  173. shareFlag: !this.data.shareFlag,
  174. avatar: data.avatar,
  175. author: data.author,
  176. iconImg: data.iconImg,
  177. title: data.title,
  178. productId: data.productId,
  179. // path: data.path,
  180. QRData: {
  181. page: data.path,
  182. scene: data.scene,
  183. qrCodeId: this.data.shareId
  184. }
  185. })
  186. }
  187. if (this.data.shareType === 'group') {
  188. console.log(data)
  189. console.log('团购分享')
  190. this.setData({
  191. shareFlag: !this.data.shareFlag,
  192. avatar: data.avatar,
  193. author: data.author,
  194. iconImg: data.iconImg,
  195. title: data.title,
  196. // path: data.path,
  197. QRData: {
  198. page: data.path,
  199. scene: data.scene,
  200. qrCodeId: this.data.shareId
  201. },
  202. groupPurchaseInfo: data.groupPurchaseInfo,
  203. groupType: data.groupType
  204. })
  205. }
  206. this.poster();
  207. if (this.data.shareType === 'class') {
  208. console.log('课程分享')
  209. this.setData({
  210. shareFlag: !this.data.shareFlag
  211. })
  212. }
  213. // 动画测试
  214. this.animation = wx.createAnimation({
  215. duration: 700,
  216. timingFunction: 'ease',
  217. })
  218. this.animation.bottom(0).step();
  219. this.setData({
  220. animationData: this.animation.export()
  221. })
  222. // 动画测试
  223. },
  224. //关闭弹框
  225. close: function (e) {
  226. this.animation = wx.createAnimation({
  227. duration: 400,
  228. timingFunction: 'ease',
  229. })
  230. this.animation.bottom(-300).step();
  231. this.setData({
  232. animationData: this.animation.export()
  233. }, () => {
  234. setTimeout(() => {
  235. this.setData({
  236. shareFlag: !this.data.shareFlag,
  237. imgUrls: ''
  238. })
  239. this.triggerEvent('ShareDialogClose', {})
  240. }, 200)
  241. })
  242. },
  243. nothing: function (e) {
  244. return
  245. },
  246. // 保存最终图片
  247. PreservationImg: function () {
  248. // this.getImgAuthorize()
  249. wx.saveImageToPhotosAlbum({
  250. filePath: this.data.imgUrls,
  251. success: (res) => {
  252. console.log(res)
  253. const data = {};
  254. console.log('productId', this.data.productId)
  255. // 团购分享记录
  256. if (this.data.shareType === 'group') {
  257. data.shareTypeEnum = 'POSTER';
  258. data.shareContentEnum = 'GROUP';
  259. data.productId = this.data.groupPurchaseInfo.productId;
  260. }
  261. // 朗读分享记录
  262. if (this.data.shareType === 'works') {
  263. data.shareTypeEnum = 'POSTER';
  264. data.shareContentEnum = 'READ';
  265. data.productId = this.data.productId;
  266. }
  267. httpRequestApi.shareRecord(data).success((res) => {
  268. console.log(res)
  269. })
  270. this.setData({
  271. imgUrls: false
  272. })
  273. },
  274. fail: res => {
  275. wx.getSetting({
  276. success(res) {
  277. if (!res.authSetting['scope.writePhotosAlbum']) {
  278. // wx.showToast({
  279. // title: '无权限操作',
  280. // icon: 'fail',
  281. // duration: 800
  282. // })
  283. wx.showModal({
  284. title: '无法保存分享图片到相册',
  285. content: '点击右上角浮点按钮->进入关于->点击右上角按钮->设置,进行授权',
  286. confirmText: '我知道了',
  287. showCancel: false,
  288. success(res) {
  289. console.log('用户点击确定')
  290. }
  291. })
  292. }
  293. }
  294. })
  295. }
  296. })
  297. },
  298. shareFriend: function () {
  299. this.triggerEvent('customevent', {})
  300. }
  301. },
  302. onShareAppMessage: (res) => {
  303. if (res.from === 'button') {
  304. // 来自页面内转发按钮
  305. console.log(res.target)
  306. }
  307. return {
  308. title: this.data.title,
  309. path: this.data.path
  310. }
  311. },
  312. })