share.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. },
  13. /**
  14. * 组件的初始数据
  15. */
  16. data: {
  17. shareFlag: false,
  18. imgUrls: '',
  19. avatar: '',
  20. author: '',
  21. iconImg: '',
  22. title: '',
  23. path: '',
  24. QRCode:''
  25. },
  26. /**
  27. * 组件的方法列表
  28. */
  29. methods: {
  30. poster: function () {
  31. const that = this;
  32. const ctx = wx.createCanvasContext('myCanvas');
  33. // 先画背景
  34. this.drawBg(ctx)
  35. .then(() => {
  36. // 下载保存图片
  37. console.log('背景画完');
  38. return this.saveImg(ctx);
  39. })
  40. .then(() => {
  41. // 画头像和作者||发起者
  42. console.log('头像画完');
  43. return this.drawAuthor(ctx);
  44. })
  45. .then(() => {
  46. console.log(this.data.shareType)
  47. if (this.data.shareType === 'works') {
  48. // 分享作品的主题内容
  49. console.log('主题画完');
  50. return this.drawWorksGut(ctx)
  51. } else {
  52. return this.drawGroupGut(ctx)
  53. }
  54. })
  55. .then(() => {
  56. ctx.drawImage(this.data.QRCode, 217, 374, 135, 135);
  57. console.log(ctx)
  58. ctx.draw(true, function (res) {
  59. wx.canvasToTempFilePath({
  60. x: 0,
  61. y: 0,
  62. width: 370,
  63. height: 507,
  64. destWidth: 370,
  65. destHeight: 507,
  66. canvasId: 'myCanvas',
  67. success: (res) => {
  68. console.log(res.tempFilePath)
  69. wx.saveImageToPhotosAlbum({
  70. filePath: res.path,
  71. success(res) {
  72. console.log(res);
  73. }
  74. })
  75. that.setData({
  76. imgUrls: res.tempFilePath
  77. }, () => {
  78. wx.hideLoading();
  79. })
  80. },
  81. fail: (res) => {
  82. console.log(res)
  83. }
  84. })
  85. })
  86. })
  87. },
  88. share: function (data) {
  89. if (this.data.shareType === 'works') {
  90. this.setData({
  91. shareFlag: !this.data.shareFlag,
  92. avatar: data.avatar,
  93. author: data.author,
  94. iconImg: data.iconImg,
  95. title: data.title,
  96. // path: data.path,
  97. QRData:{
  98. page: data.path,
  99. scene: data.scene
  100. }
  101. })
  102. }
  103. if (this.data.shareType === 'group') {
  104. console.log('团购分享')
  105. this.setData({
  106. shareFlag: !this.data.shareFlag
  107. })
  108. }
  109. if (this.data.shareType === 'class') {
  110. console.log('课程分享')
  111. this.setData({
  112. shareFlag: !this.data.shareFlag
  113. })
  114. }
  115. },
  116. // 保存图片到本地
  117. saveImg: function () {
  118. console.log('yibuububu')
  119. let download = new Promise((resolve, reject) => {
  120. let QRCode = () => {
  121. // 生成二维码并下载
  122. httpRequestApi.createQRCode(this.data.QRData).success(res => {
  123. // let base64 = wx.arrayBufferToBase64(res.data);
  124. // console.log(a)
  125. this.setData({
  126. QRCode: res.data
  127. })
  128. // wx.downloadFile({
  129. // url: res,
  130. // success: (res) => {
  131. // console.log(res)
  132. // console.log('下载二维码成功')
  133. // // this.setData({
  134. // // avatar: res.tempFilePath
  135. // // }, () => {
  136. // // })
  137. // }
  138. // });
  139. console.log(this.data.QRCode)
  140. console.log(res)
  141. resolve();
  142. })
  143. }
  144. let downAvatar = () => {
  145. wx.downloadFile({
  146. url: this.data.avatar,
  147. success: (res) => {
  148. console.log('下载头像成功')
  149. console.log(this.data.avatar)
  150. this.setData({
  151. avatar: res.tempFilePath
  152. }, () => {
  153. QRCode();
  154. })
  155. }
  156. });
  157. }
  158. // resolve(()=>{
  159. // 下载icon
  160. wx.downloadFile({
  161. url: this.data.iconImg,
  162. success: (res) => {
  163. console.log('下载icon成功')
  164. console.log(this.data.iconImg)
  165. this.setData({
  166. iconImg: res.tempFilePath
  167. }, () => {
  168. // 下载头像
  169. downAvatar();
  170. })
  171. }
  172. });
  173. })
  174. return download;
  175. },
  176. // 先画出背景
  177. drawBg: function (ctx) {
  178. let background = new Promise((resolve, reject) => {
  179. wx.showLoading({
  180. title: '海报生成中',
  181. mask: true
  182. })
  183. console.log('开始画背景')
  184. ctx.rect(0, 0, 370, 507)
  185. ctx.setFillStyle('#fff')
  186. ctx.fill()
  187. ctx.save()
  188. ctx.beginPath()
  189. ctx.setFillStyle('#fff')
  190. ctx.fill()
  191. ctx.arc(185, 48, 37, 0, 2 * Math.PI, false)
  192. ctx.clip(); //画
  193. resolve();
  194. });
  195. return background;
  196. },
  197. // 画头像和作者||发起者
  198. drawAuthor: function (ctx) {
  199. let author = new Promise((resolve, reject) => {
  200. // resolve(() => {
  201. console.log('开始画头像')
  202. ctx.drawImage(this.data.avatar, 148, 8, 100, 100); //插入图片
  203. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  204. ctx.setFontSize(20)
  205. ctx.setFillStyle('#535353')
  206. ctx.fillText(this.data.author, (370 - ctx.measureText(this.data.author).width) / 2, 115)
  207. ctx.setFillStyle('#FF4400')
  208. // })
  209. resolve();
  210. })
  211. return author;
  212. },
  213. // 分享作品的主题内容
  214. drawWorksGut: function (ctx) {
  215. let works = new Promise((resolve, reject) => {
  216. // resolve(() => {
  217. console.log('开始画内容')
  218. ctx.fillText('已使出洪荒之力,声情并茂的为', (370 - ctx.measureText('已使出洪荒之力,声情并茂的为').width) / 2, 138)
  219. ctx.fillText(`《${this.data.title}》`, (370 - ctx.measureText(`《${this.data.title}》`).width) / 2, 160)
  220. ctx.fillText(`配了一段惊世之作`, (370 - ctx.measureText(`配了一段惊世之作`).width) / 2, 182)
  221. ctx.drawImage(this.data.iconImg, 9, 205, 352, 145);
  222. ctx.setFontSize(18)
  223. ctx.setFillStyle('#000')
  224. ctx.drawImage('../../../static/groupImg/share_bottom.png', 3, 419, 370, 192);
  225. ctx.setFontSize(20)
  226. ctx.setFillStyle('red')
  227. ctx.fillText('为TA疯狂打CALL', 9, 467)
  228. ctx.setFontSize(18)
  229. ctx.setFillStyle('#000')
  230. ctx.fillText('长按识别二维码,快去听听', 9, 493)
  231. // })
  232. resolve();
  233. })
  234. return works;
  235. },
  236. // 分享团购的主体内容
  237. drawGroupGut: function (ctx) {
  238. let group = new Promise((resolve, reject) => {
  239. resolve(() => {
  240. ctx.fillText('郎朗读书声是世上最美的声音', 50, 138)
  241. ctx.drawImage('../../../static/groupImg/Combined Shape.png', 11, 348, 196, 65);
  242. ctx.drawImage('../../../static/groupImg/Rectangle 41.png', 134, 358, 59, 20);
  243. ctx.setFontSize(14)
  244. ctx.setFillStyle('#fff')
  245. ctx.fillText('五人团', 149, 373)
  246. ctx.setFontSize(16)
  247. ctx.setFillStyle('#A95A00')
  248. ctx.fillText('原价', 24, 373)
  249. ctx.setStrokeStyle('red')
  250. ctx.moveTo(64, 368)
  251. ctx.lineTo(114, 368)
  252. ctx.stroke()
  253. ctx.fillText('¥99元', 64, 373)
  254. ctx.setFontSize(18)
  255. ctx.fillText('团购价仅需', 20, 403)
  256. ctx.fillText('元', 144, 403)
  257. ctx.setFontSize(20)
  258. ctx.setFillStyle('red')
  259. ctx.fillText('¥1', 109, 403)
  260. ctx.drawImage('../../../static/groupImg/share_bottom.png', 3, 419, 370, 192);
  261. ctx.setFontSize(20)
  262. ctx.setFillStyle('red')
  263. ctx.fillText('名额有限,售完截止', 9, 467)
  264. ctx.setFontSize(18)
  265. ctx.setFillStyle('#000')
  266. ctx.fillText('长按识别二维码参加团购', 9, 493)
  267. })
  268. })
  269. return group();
  270. },
  271. // 保存最终图片
  272. PreservationImg: function () {
  273. wx.saveImageToPhotosAlbum({
  274. filePath: this.data.imgUrls,
  275. success(res) {
  276. console.log('保存成功')
  277. }
  278. })
  279. },
  280. shareFriend: function () {
  281. this.triggerEvent('customevent', {})
  282. }
  283. },
  284. onShareAppMessage: (res) => {
  285. if (res.from === 'button') {
  286. // 来自页面内转发按钮
  287. console.log(res.target)
  288. }
  289. return {
  290. title: this.data.title,
  291. path: this.data.path
  292. }
  293. },
  294. })