share.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. console.log(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(data)
  127. console.log('团购分享')
  128. this.setData({
  129. shareFlag: !this.data.shareFlag,
  130. avatar: data.avatar,
  131. author: data.author,
  132. iconImg: data.iconImg,
  133. title: data.title,
  134. // path: data.path,
  135. QRData: {
  136. page: data.path,
  137. scene: data.scene,
  138. qrCodeId: this.data.shareId
  139. },
  140. groupPurchaseInfo: data.groupPurchaseInfo
  141. })
  142. }
  143. if (this.data.shareType === 'class') {
  144. console.log('课程分享')
  145. this.setData({
  146. shareFlag: !this.data.shareFlag
  147. })
  148. }
  149. },
  150. //关闭弹框
  151. close: function (e) {
  152. console.log(e)
  153. this.setData({
  154. shareFlag: !this.data.shareFlag,
  155. imgUrls: ''
  156. })
  157. },
  158. nothing: function (e) {
  159. return
  160. },
  161. // 保存图片到本地
  162. saveImg: function () {
  163. console.log('yibuububu')
  164. let download = new Promise((resolve, reject) => {
  165. let QRCode = () => {
  166. // 生成二维码并下载
  167. httpRequestApi.createQRCode(this.data.QRData).success(res => {
  168. console.log(this.data.QRData);
  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. if (this.data.shareType === 'works') {
  228. ctx.drawImage('../../../static/image/works_poster.jpg', 0, 0, 1500, 2668,0,0,750,1334); //插入图片
  229. } else {
  230. ctx.drawImage('../../../static/image/group_poster.jpg', 0, 0, 1500, 2668,0,0,750,1334); //插入图片
  231. }
  232. ctx.save()
  233. ctx.beginPath()
  234. ctx.fill()
  235. resolve();
  236. });
  237. return background;
  238. },
  239. // 画头像和作者||发起者
  240. drawAuthor: function (ctx) {
  241. let author = new Promise((resolve, reject) => {
  242. if (this.data.shareType === 'works') {
  243. ctx.arc(187.5, 171, 36, 0, 2 * Math.PI, false)
  244. ctx.clip(); //剪切头像区域
  245. ctx.fill('red')
  246. ctx.drawImage(this.data.avatar, 157, 135, 72, 72); //插入图片
  247. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  248. ctx.setFontSize(18)
  249. ctx.setFillStyle('#444444')
  250. ctx.fillText(this.data.author, (375 - ctx.measureText(this.data.author).width) / 2, 232)
  251. ctx.setFontSize(16)
  252. ctx.setFillStyle('#ff9209')
  253. ctx.fillText('播音主持特长生', (375 - ctx.measureText('播音主持特长生').width) / 2, 253)
  254. } else {
  255. ctx.arc(52, 58, 36, 0, 2 * Math.PI, false)
  256. ctx.clip(); //剪切头像区域
  257. ctx.fill('red')
  258. ctx.drawImage(this.data.avatar, 15, 22, 72, 72); //插入图片
  259. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  260. ctx.setFontSize(20)
  261. ctx.setFillStyle('#fff')
  262. ctx.fillText(this.data.author, 97, 49)
  263. ctx.setFontSize(22)
  264. ctx.fillText('邀您一起来拼团', 97, 77)
  265. }
  266. resolve();
  267. })
  268. return author;
  269. },
  270. // 分享作品的主题内容
  271. drawWorksGut: function (ctx) {
  272. let works = new Promise((resolve, reject) => {
  273. // resolve(() => {
  274. console.log('开始画内容')
  275. ctx.setFillStyle('#fff')
  276. ctx.setFontSize(18)
  277. ctx.fillText('小学语文 最新全国统一教材', (375 - ctx.measureText('小学语文 最新全国统一教材').width) / 2, 55);
  278. ctx.setFontSize(30)
  279. ctx.fillText('课文朗读 从未如此有趣', (375 - ctx.measureText('课文朗读 从未如此有趣').width) / 2 ,103);
  280. ctx.setFontSize(16)
  281. ctx.setFillStyle('#444')
  282. ctx.fillText('使出洪荒之力,声情并茂地朗诵了', (375 - ctx.measureText('使出洪荒之力,声情并茂地朗诵了').width) / 2 ,297)
  283. // ctx.fillText(`配了一段惊世之作`, 90, 240)
  284. // ctx.setFontSize(16)
  285. // ctx.setFillStyle('red')
  286. ctx.fillText(`《${this.data.title}》`, (375 - ctx.measureText(`《${this.data.title}》`).width) / 2, 322)
  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. if(this.data.groupPurchaseInfo.productId === 1){
  312. ctx.drawImage('../../../static/share/poster_1.jpg', 12, 176, 351, 145);
  313. }
  314. if(this.data.groupPurchaseInfo.productId === 2){
  315. ctx.drawImage('../../../static/share/poster_2.jpg', 12, 176, 351, 145);
  316. }
  317. if(this.data.groupPurchaseInfo.productId === 3){
  318. ctx.drawImage('../../../static/share/poster_3.jpg', 12, 176, 351, 145);
  319. }
  320. ctx.setFontSize(17)
  321. ctx.setFillStyle('#A95A00')
  322. ctx.fillText(`${this.data.groupPurchaseInfo.headcount}人团`, 96, 412)
  323. ctx.setFontSize(19)
  324. ctx.fillText('每人仅需', 151, 412)
  325. // ctx.fillText('元', 120, 495)
  326. ctx.setFontSize(28)
  327. ctx.setStrokeStyle('red')
  328. ctx.setFillStyle('red')
  329. ctx.fillText(`${parseFloat(this.data.groupPurchaseInfo.price / 100).toFixed(2)}元`, 235, 415)
  330. ctx.setFontSize(12)
  331. ctx.setFillStyle('#666666')
  332. ctx.fillText('长按识别二维码', (375 - ctx.measureText('长按识别二维码').width) / 2, 574)
  333. //})
  334. resolve();
  335. })
  336. return group;
  337. },
  338. // 保存最终图片
  339. PreservationImg: function () {
  340. wx.saveImageToPhotosAlbum({
  341. filePath: this.data.imgUrls,
  342. success: (res) => {
  343. console.log(res)
  344. this.setData({
  345. imgUrls: false
  346. })
  347. }
  348. })
  349. },
  350. shareFriend: function () {
  351. this.triggerEvent('customevent', {})
  352. }
  353. },
  354. onShareAppMessage: (res) => {
  355. if (res.from === 'button') {
  356. // 来自页面内转发按钮
  357. console.log(res.target)
  358. }
  359. return {
  360. title: this.data.title,
  361. path: this.data.path
  362. }
  363. },
  364. })