share.js 9.9 KB

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