share.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. animationData: {},
  32. touchMove: true
  33. },
  34. /**
  35. * 组件的方法列表
  36. */
  37. methods: {
  38. touchMove: function () {
  39. return false
  40. },
  41. poster: function () {
  42. const that = this;
  43. const ctx = wx.createCanvasContext('myCanvas');
  44. // 先画背景
  45. this.drawBg(ctx)
  46. .then(() => {
  47. // 下载保存图片
  48. console.log('背景画完');
  49. return this.saveImg(ctx);
  50. })
  51. .then(() => {
  52. // 画头像和作者||发起者
  53. console.log('头像画完');
  54. return this.drawAuthor(ctx);
  55. })
  56. .then(() => {
  57. console.log(this.data.shareType)
  58. if (this.data.shareType === 'works') {
  59. // 分享作品的主题内容
  60. console.log('主题画完');
  61. return this.drawWorksGut(ctx)
  62. } else {
  63. return this.drawGroupGut(ctx)
  64. }
  65. })
  66. .then(() => {
  67. wx.getImageInfo({
  68. src: this.data.QRCodeImagePath, //服务器返回的图片地址
  69. success: function (res) {
  70. console.log('下载content成功1')
  71. console.log(res.path)
  72. //res.path是网络图片的本地地址
  73. ctx.drawImage(res.path, 127, 423, 121, 121);
  74. console.log('下载content成功2')
  75. ctx.draw(true, function (res) {
  76. setTimeout(() => {
  77. wx.canvasToTempFilePath({
  78. x: 0,
  79. y: 0,
  80. width: 375,
  81. height: 667,
  82. // destWidth: 370,
  83. destWidth: 1500,
  84. // destHeight: 507,
  85. destHeight: 2668,
  86. canvasId: 'myCanvas',
  87. success: (res) => {
  88. console.log(res.tempFilePath)
  89. that.setData({
  90. imgUrls: res.tempFilePath
  91. }, () => {
  92. wx.hideLoading();
  93. })
  94. },
  95. fail: (res) => {
  96. console.log(res)
  97. }
  98. })
  99. }, 200)
  100. })
  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. this.animation = wx.createAnimation({
  151. duration: 700,
  152. timingFunction: 'ease',
  153. })
  154. this.animation.bottom(0).step();
  155. this.setData({
  156. animationData: this.animation.export()
  157. })
  158. // 动画测试
  159. },
  160. //关闭弹框
  161. close: function (e) {
  162. this.animation = wx.createAnimation({
  163. duration: 400,
  164. timingFunction: 'ease',
  165. })
  166. this.animation.bottom(-300).step();
  167. this.setData({
  168. animationData: this.animation.export()
  169. }, () => {
  170. setTimeout(() => {
  171. this.setData({
  172. shareFlag: !this.data.shareFlag,
  173. imgUrls: ''
  174. })
  175. this.triggerEvent('ShareDialogClose', {})
  176. }, 200)
  177. })
  178. },
  179. nothing: function (e) {
  180. return
  181. },
  182. // 保存图片到本地
  183. saveImg: function () {
  184. console.log('yibuububu')
  185. let download = new Promise((resolve, reject) => {
  186. let QRCode = () => {
  187. // 生成二维码并下载
  188. httpRequestApi.createQRCode(this.data.QRData).success(res => {
  189. console.log(this.data.QRData);
  190. this.setData({
  191. QRCodeImagePath: res.data.data
  192. })
  193. // wx.downloadFile({
  194. // url: res,
  195. // success: (res) => {
  196. // console.log(res)
  197. // console.log('下载二维码成功')
  198. // // this.setData({
  199. // // avatar: res.tempFilePath
  200. // // }, () => {
  201. // // })
  202. // }
  203. // });
  204. console.log(this.data.QRCodeImagePath)
  205. console.log(res)
  206. resolve();
  207. })
  208. }
  209. let downAvatar = () => {
  210. wx.downloadFile({
  211. url: this.data.avatar,
  212. success: (res) => {
  213. console.log('下载头像成功')
  214. console.log(this.data.avatar)
  215. this.setData({
  216. avatar: res.tempFilePath
  217. }, () => {
  218. QRCode();
  219. })
  220. }
  221. });
  222. }
  223. // resolve(()=>{
  224. // 下载icon
  225. wx.downloadFile({
  226. url: this.data.iconImg,
  227. success: (res) => {
  228. console.log('下载icon成功')
  229. console.log(this.data.iconImg)
  230. this.setData({
  231. iconImg: res.tempFilePath
  232. }, () => {
  233. // 下载头像
  234. downAvatar();
  235. })
  236. }
  237. });
  238. })
  239. return download;
  240. },
  241. // 先画出背景
  242. drawBg: function (ctx) {
  243. let background = new Promise((resolve, reject) => {
  244. wx.showLoading({
  245. title: '海报生成中',
  246. mask: true
  247. })
  248. if (this.data.shareType === 'works') {
  249. ctx.drawImage('../../../static/image/works_poster.jpg', 0, 0, 1500, 2668, 0, 0, 750, 1334); //插入图片
  250. } else {
  251. ctx.drawImage('../../../static/image/group_poster.jpg', 0, 0, 1500, 2668, 0, 0, 750, 1334); //插入图片
  252. }
  253. ctx.save()
  254. ctx.beginPath()
  255. ctx.fill()
  256. resolve();
  257. });
  258. return background;
  259. },
  260. // 画头像和作者||发起者
  261. drawAuthor: function (ctx) {
  262. let author = new Promise((resolve, reject) => {
  263. if (this.data.shareType === 'works') {
  264. ctx.arc(187.5, 171, 36, 0, 2 * Math.PI, false)
  265. ctx.clip(); //剪切头像区域
  266. ctx.fill('red')
  267. ctx.drawImage(this.data.avatar, 152, 135, 72, 72); //插入图片
  268. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  269. ctx.setFontSize(18)
  270. ctx.setFillStyle('#444444')
  271. ctx.fillText(this.data.author, (375 - ctx.measureText(this.data.author).width) / 2, 232)
  272. ctx.setFontSize(16)
  273. ctx.setFillStyle('#ff9209')
  274. ctx.fillText('播音主持特长生', (375 - ctx.measureText('播音主持特长生').width) / 2, 253)
  275. } else {
  276. ctx.arc(52, 58, 36, 0, 2 * Math.PI, false)
  277. ctx.clip(); //剪切头像区域
  278. ctx.fill('red')
  279. ctx.drawImage(this.data.avatar, 16, 22, 72, 72); //插入图片
  280. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  281. ctx.setFontSize(20)
  282. ctx.setFillStyle('#fff')
  283. ctx.fillText(this.data.author, 97, 49)
  284. ctx.setFontSize(22)
  285. ctx.fillText('邀您一起来拼团', 97, 77)
  286. }
  287. resolve();
  288. })
  289. return author;
  290. },
  291. // 分享作品的主题内容
  292. drawWorksGut: function (ctx) {
  293. let works = new Promise((resolve, reject) => {
  294. // resolve(() => {
  295. console.log('开始画内容')
  296. ctx.setFillStyle('#fff')
  297. ctx.setFontSize(18)
  298. ctx.fillText('小学语文 最新全国统一教材', (375 - ctx.measureText('小学语文 最新全国统一教材').width) / 2, 55);
  299. ctx.setFontSize(30)
  300. ctx.fillText('课文朗读 从未如此有趣', (375 - ctx.measureText('课文朗读 从未如此有趣').width) / 2, 103);
  301. ctx.setFontSize(16)
  302. ctx.setFillStyle('#444')
  303. ctx.fillText('使出洪荒之力,声情并茂地朗诵了', (375 - ctx.measureText('使出洪荒之力,声情并茂地朗诵了').width) / 2, 297)
  304. // ctx.fillText(`配了一段惊世之作`, 90, 240)
  305. // ctx.setFontSize(16)
  306. // ctx.setFillStyle('red')
  307. ctx.fillText(`《${this.data.title}》`, (375 - ctx.measureText(`《${this.data.title}》`).width) / 2, 322)
  308. ctx.setFontSize(16)
  309. ctx.setFillStyle('#000')
  310. // ctx.setFillStyle('#000')
  311. ctx.setFontSize(16)
  312. // ctx.setFillStyle('#c90000')
  313. ctx.setFillStyle('#ff9209')
  314. ctx.fillText('为TA疯狂打CALL 快去听听', (375 - ctx.measureText('为TA疯狂打CALL 快去听听').width) / 2, 401)
  315. ctx.setFontSize(12)
  316. ctx.setFillStyle('#666666')
  317. ctx.fillText('长按识别二维码', (375 - ctx.measureText('长按识别二维码').width) / 2, 574)
  318. // })
  319. resolve();
  320. })
  321. return works;
  322. },
  323. // 分享团购的主体内容
  324. drawGroupGut: function (ctx) {
  325. let group = new Promise((resolve, reject) => {
  326. ctx.setFontSize(28)
  327. ctx.setFillStyle('#ff670d')
  328. ctx.fillText('小学语文 朗读配音', (375 - ctx.measureText('小学语文 朗读配音').width) / 2, 152)
  329. ctx.setFontSize(16)
  330. ctx.setFillStyle('#444')
  331. ctx.fillText('朗朗读书声,是世上最美的声音', (375 - ctx.measureText('朗朗读书声,是世上最美的声音').width) / 2, 351)
  332. // if (this.data.groupPurchaseInfo.productId === 1) {
  333. ctx.drawImage(this.data.iconImg, 12, 176, 351, 145);
  334. // }
  335. // if (this.data.groupPurchaseInfo.productId === 2) {
  336. // ctx.drawImage('../../../static/share/poster_2.jpg', 12, 176, 351, 145);
  337. // }
  338. // if (this.data.groupPurchaseInfo.productId === 3) {
  339. // ctx.drawImage('../../../static/share/poster_3.jpg', 12, 176, 351, 145);
  340. // }
  341. ctx.setFontSize(17)
  342. ctx.setFillStyle('#A95A00')
  343. ctx.fillText(`${this.data.groupPurchaseInfo.headcount}人团`, 96, 412)
  344. ctx.setFontSize(19)
  345. ctx.fillText('每人仅需', 151, 412)
  346. // ctx.fillText('元', 120, 495)
  347. ctx.setFontSize(28)
  348. ctx.setStrokeStyle('red')
  349. ctx.setFillStyle('red')
  350. ctx.fillText(`${parseFloat(this.data.groupPurchaseInfo.price / 100).toFixed(2)}元`, 235, 415)
  351. ctx.setFontSize(12)
  352. ctx.setFillStyle('#666666')
  353. ctx.fillText('长按识别二维码', (375 - ctx.measureText('长按识别二维码').width) / 2, 574)
  354. //})
  355. resolve();
  356. })
  357. return group;
  358. },
  359. // 保存最终图片
  360. PreservationImg: function () {
  361. // this.getImgAuthorize()
  362. wx.saveImageToPhotosAlbum({
  363. filePath: this.data.imgUrls,
  364. success: (res) => {
  365. console.log(res)
  366. this.setData({
  367. imgUrls: false
  368. })
  369. },
  370. fail: res => {
  371. wx.getSetting({
  372. success(res) {
  373. if (!res.authSetting['scope.writePhotosAlbum']) {
  374. // wx.showToast({
  375. // title: '无权限操作',
  376. // icon: 'fail',
  377. // duration: 800
  378. // })
  379. wx.showModal({
  380. title: '无法保存分享图片到相册',
  381. content: '点击右上角浮点按钮->进入关于->点击右上角按钮->设置,进行授权',
  382. confirmText: '我知道了',
  383. showCancel: false,
  384. success(res) {
  385. console.log('用户点击确定')
  386. }
  387. })
  388. }
  389. }
  390. })
  391. }
  392. })
  393. },
  394. shareFriend: function () {
  395. this.triggerEvent('customevent', {})
  396. }
  397. },
  398. onShareAppMessage: (res) => {
  399. if (res.from === 'button') {
  400. // 来自页面内转发按钮
  401. console.log(res.target)
  402. }
  403. return {
  404. title: this.data.title,
  405. path: this.data.path
  406. }
  407. },
  408. })