share.js 11 KB

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