share.js 14 KB

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