share.js 10 KB

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