share.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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,127, 423, 121, 121);
  69. console.log('下载content成功2')
  70. ctx.draw(true, function (res) {
  71. wx.canvasToTempFilePath({
  72. x: 0,
  73. y: 0,
  74. width: 375,
  75. height: 667,
  76. // destWidth: 370,
  77. destWidth: 1500,
  78. // destHeight: 507,
  79. destHeight: 2668,
  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. if (this.data.shareType === 'works') {
  227. ctx.drawImage('../../../static/image/works_poster.png', 0, 0, 1500, 2668,0,0,750,1334); //插入图片
  228. } else {
  229. ctx.drawImage('../../../static/image/group_poster.jpg', 0, 0, 1500, 2668,0,0,750,1334); //插入图片
  230. }
  231. ctx.save()
  232. ctx.beginPath()
  233. ctx.fill()
  234. resolve();
  235. });
  236. return background;
  237. },
  238. // 画头像和作者||发起者
  239. drawAuthor: function (ctx) {
  240. let author = new Promise((resolve, reject) => {
  241. if (this.data.shareType === 'works') {
  242. ctx.arc(187.5, 171, 36, 0, 2 * Math.PI, false)
  243. ctx.clip(); //剪切头像区域
  244. ctx.fill('red')
  245. ctx.drawImage(this.data.avatar, 157, 135, 72, 72); //插入图片
  246. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  247. ctx.setFontSize(18)
  248. ctx.setFillStyle('#444444')
  249. ctx.fillText(this.data.author, (375 - ctx.measureText(this.data.author).width) / 2, 232)
  250. ctx.setFontSize(16)
  251. ctx.setFillStyle('#ff9209')
  252. ctx.fillText('播音主持特长生', (375 - ctx.measureText('播音主持特长生').width) / 2, 253)
  253. } else {
  254. ctx.arc(52, 58, 36, 0, 2 * Math.PI, false)
  255. ctx.clip(); //剪切头像区域
  256. ctx.fill('red')
  257. ctx.drawImage(this.data.avatar, 15, 22, 72, 72); //插入图片
  258. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  259. ctx.setFontSize(20)
  260. ctx.setFillStyle('#fff')
  261. ctx.fillText(this.data.author, 97, 49)
  262. ctx.setFontSize(22)
  263. ctx.fillText('邀您一起来拼团', 97, 77)
  264. }
  265. resolve();
  266. })
  267. return author;
  268. },
  269. // 分享作品的主题内容
  270. drawWorksGut: function (ctx) {
  271. let works = new Promise((resolve, reject) => {
  272. // resolve(() => {
  273. console.log('开始画内容')
  274. ctx.setFillStyle('#fff')
  275. ctx.setFontSize(18)
  276. ctx.fillText('小学语文 最新全国统一教材', (375 - ctx.measureText('小学语文 最新全国统一教材').width) / 2, 55);
  277. ctx.setFontSize(30)
  278. ctx.fillText('课文朗读 从未如此有趣', (375 - ctx.measureText('课文朗读 从未如此有趣').width) / 2 ,103);
  279. ctx.setFontSize(16)
  280. ctx.setFillStyle('#444')
  281. ctx.fillText('使出洪荒之力,声情并茂地朗诵了', (375 - ctx.measureText('使出洪荒之力,声情并茂地朗诵了').width) / 2 ,297)
  282. // ctx.fillText(`配了一段惊世之作`, 90, 240)
  283. // ctx.setFontSize(16)
  284. // ctx.setFillStyle('red')
  285. ctx.fillText(`《${this.data.title}》`, (375 - ctx.measureText(`《${this.data.title}》`).width) / 2, 322)
  286. // ctx.drawImage('../../../static/image/reading_poster.jpg', 11, 276, 351, 145);
  287. ctx.setFontSize(16)
  288. ctx.setFillStyle('#000')
  289. // ctx.setFillStyle('#000')
  290. ctx.setFontSize(16)
  291. // ctx.setFillStyle('#c90000')
  292. ctx.setFillStyle('#ff9209')
  293. ctx.fillText('为TA疯狂打CALL 快去听听', (375 - ctx.measureText('为TA疯狂打CALL 快去听听').width) / 2 , 401)
  294. ctx.setFontSize(12)
  295. ctx.setFillStyle('#666666')
  296. ctx.fillText('长按识别二维码', (375 - ctx.measureText('长按识别二维码').width) / 2, 574)
  297. // })
  298. resolve();
  299. })
  300. return works;
  301. },
  302. // 分享团购的主体内容
  303. drawGroupGut: function (ctx) {
  304. let group = new Promise((resolve, reject) => {
  305. ctx.setFontSize(28)
  306. ctx.setFillStyle('#ff670d')
  307. ctx.fillText('小学语文 朗读配音', (375 - ctx.measureText('小学语文 朗读配音').width) / 2 , 152)
  308. ctx.setFontSize(16)
  309. ctx.setFillStyle('#444')
  310. ctx.fillText('郎朗读书声,是世上最美的声音', (375 - ctx.measureText('郎朗读书声,是世上最美的声音').width) / 2 ,351)
  311. ctx.drawImage(this.data.iconImg, 12, 176, 351, 145);
  312. // ctx.drawImage('../../../static/groupImg/Rectangle 41.png', 134, 358, 59, 20);
  313. ctx.setFontSize(17)
  314. ctx.setFillStyle('#A95A00')
  315. ctx.fillText(`${this.data.groupPurchaseInfo.headcount}人团`, 96, 412)
  316. ctx.setFontSize(19)
  317. ctx.fillText('每人仅需', 151, 412)
  318. // ctx.fillText('元', 120, 495)
  319. ctx.setFontSize(28)
  320. ctx.setStrokeStyle('red')
  321. ctx.setFillStyle('red')
  322. ctx.fillText(`${parseFloat(this.data.groupPurchaseInfo.price / 100).toFixed(2)}元`, 235, 415)
  323. ctx.setFontSize(12)
  324. ctx.setFillStyle('#666666')
  325. ctx.fillText('长按识别二维码', (375 - ctx.measureText('长按识别二维码').width) / 2, 574)
  326. //})
  327. resolve();
  328. })
  329. return group;
  330. },
  331. // 保存最终图片
  332. PreservationImg: function () {
  333. wx.saveImageToPhotosAlbum({
  334. filePath: this.data.imgUrls,
  335. success: (res) => {
  336. console.log(res)
  337. this.setData({
  338. imgUrls: false
  339. })
  340. }
  341. })
  342. },
  343. shareFriend: function () {
  344. this.triggerEvent('customevent', {})
  345. }
  346. },
  347. onShareAppMessage: (res) => {
  348. if (res.from === 'button') {
  349. // 来自页面内转发按钮
  350. console.log(res.target)
  351. }
  352. return {
  353. title: this.data.title,
  354. path: this.data.path
  355. }
  356. },
  357. })