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. 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. saveImg: function () {
  150. console.log('yibuububu')
  151. let download = new Promise((resolve, reject) => {
  152. let QRCode = () => {
  153. // 生成二维码并下载
  154. httpRequestApi.createQRCode(this.data.QRData).success(res => {
  155. // let base64 = wx.arrayBufferToBase64(res.data);
  156. // console.log(a)
  157. this.setData({
  158. QRCodeImagePath: res.data.data
  159. })
  160. // wx.downloadFile({
  161. // url: res,
  162. // success: (res) => {
  163. // console.log(res)
  164. // console.log('下载二维码成功')
  165. // // this.setData({
  166. // // avatar: res.tempFilePath
  167. // // }, () => {
  168. // // })
  169. // }
  170. // });
  171. console.log(this.data.QRCodeImagePath)
  172. console.log(res)
  173. resolve();
  174. })
  175. }
  176. let downAvatar = () => {
  177. wx.downloadFile({
  178. url: this.data.avatar,
  179. success: (res) => {
  180. console.log('下载头像成功')
  181. console.log(this.data.avatar)
  182. this.setData({
  183. avatar: res.tempFilePath
  184. }, () => {
  185. QRCode();
  186. })
  187. }
  188. });
  189. }
  190. // resolve(()=>{
  191. // 下载icon
  192. wx.downloadFile({
  193. url: this.data.iconImg,
  194. success: (res) => {
  195. console.log('下载icon成功')
  196. console.log(this.data.iconImg)
  197. this.setData({
  198. iconImg: res.tempFilePath
  199. }, () => {
  200. // 下载头像
  201. downAvatar();
  202. })
  203. }
  204. });
  205. })
  206. return download;
  207. },
  208. // 先画出背景
  209. drawBg: function (ctx) {
  210. let background = new Promise((resolve, reject) => {
  211. wx.showLoading({
  212. title: '海报生成中',
  213. mask: true
  214. })
  215. console.log('开始画背景')
  216. ctx.rect(0, 0, 370, 507)
  217. ctx.setFillStyle('#fff')
  218. ctx.fill()
  219. ctx.save()
  220. ctx.beginPath()
  221. ctx.setFillStyle('#fff')
  222. ctx.fill()
  223. ctx.arc(185, 48, 37, 0, 2 * Math.PI, false)
  224. ctx.clip(); //画
  225. resolve();
  226. });
  227. return background;
  228. },
  229. // 画头像和作者||发起者
  230. drawAuthor: function (ctx) {
  231. let author = new Promise((resolve, reject) => {
  232. // resolve(() => {
  233. console.log('开始画头像')
  234. ctx.drawImage(this.data.avatar, 148, 8, 100, 100); //插入图片
  235. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  236. ctx.setFontSize(20)
  237. ctx.setFillStyle('#535353')
  238. ctx.fillText(this.data.author, (370 - ctx.measureText(this.data.author).width) / 2, 115)
  239. ctx.setFillStyle('#FF4400')
  240. // })
  241. resolve();
  242. })
  243. return author;
  244. },
  245. // 分享作品的主题内容
  246. drawWorksGut: function (ctx) {
  247. let works = new Promise((resolve, reject) => {
  248. // resolve(() => {
  249. console.log('开始画内容')
  250. ctx.fillText('已使出洪荒之力,声情并茂的为', (370 - ctx.measureText('已使出洪荒之力,声情并茂的为').width) / 2, 138)
  251. ctx.fillText(`《${this.data.title}》`, (370 - ctx.measureText(`《${this.data.title}》`).width) / 2, 160)
  252. ctx.fillText(`配了一段惊世之作`, (370 - ctx.measureText(`配了一段惊世之作`).width) / 2, 182)
  253. ctx.drawImage(this.data.iconImg, 9, 205, 704, 145);
  254. ctx.setFontSize(18)
  255. ctx.setFillStyle('#000')
  256. ctx.drawImage('../../../static/groupImg/share_bottom.png', 3, 419, 370, 192);
  257. ctx.setFontSize(20)
  258. ctx.setFillStyle('red')
  259. ctx.fillText('为TA疯狂打CALL', 9, 467)
  260. ctx.setFontSize(18)
  261. ctx.setFillStyle('#000')
  262. ctx.fillText('长按识别二维码,快去听听', 9, 493)
  263. // })
  264. resolve();
  265. })
  266. return works;
  267. },
  268. // 分享团购的主体内容
  269. drawGroupGut: function (ctx) {
  270. let group = new Promise((resolve, reject) => {
  271. //resolve(() => {
  272. ctx.fillText('郎朗读书声是世上最美的声音', 50, 138)
  273. ctx.drawImage(this.data.iconImg, 9, 205, 352, 145);
  274. ctx.drawImage('../../../static/groupImg/Rectangle 41.png', 134, 358, 59, 20);
  275. ctx.setFontSize(14)
  276. ctx.setFillStyle('#fff')
  277. ctx.fillText(`${this.data.groupPurchaseInfo.headcount}人团`, 149, 373)
  278. ctx.setFontSize(16)
  279. ctx.setFillStyle('#A95A00')
  280. ctx.fillText('原价', 20, 373)
  281. ctx.setStrokeStyle('red')
  282. ctx.moveTo(64, 368)
  283. ctx.lineTo(114, 368)
  284. ctx.stroke()
  285. ctx.fillText(`¥${parseFloat(this.data.groupPurchaseInfo.originPrice / 100).toFixed(2)}元`, 54, 373)
  286. ctx.setFontSize(18)
  287. ctx.fillText('团购价仅需', 20, 403)
  288. ctx.fillText('元', 185, 403)
  289. ctx.setFontSize(20)
  290. ctx.setFillStyle('red')
  291. ctx.fillText(`¥${parseFloat(this.data.groupPurchaseInfo.price / 100).toFixed(2)}`, 115, 403)
  292. ctx.drawImage('../../../static/groupImg/share_bottom.png', 3, 419, 370, 192);
  293. ctx.setFontSize(20)
  294. ctx.setFillStyle('red')
  295. ctx.fillText('名额有限,售完截止', 9, 467)
  296. ctx.setFontSize(18)
  297. ctx.setFillStyle('#000')
  298. ctx.fillText('长按识别二维码参加团购', 9, 493)
  299. //})
  300. resolve();
  301. })
  302. return group;
  303. },
  304. // 保存最终图片
  305. PreservationImg: function () {
  306. wx.saveImageToPhotosAlbum({
  307. filePath: this.data.imgUrls,
  308. success(res) {
  309. this.setData({
  310. imgUrls: false
  311. })
  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. })