share.js 9.8 KB

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