share.js 10 KB

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