share.js 10 KB

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