shareTemp.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. //res.path是网络图片的本地地址
  72. ctx.drawImage(res.path, 35, 353, 90, 90);
  73. ctx.draw(true, function (res) {
  74. setTimeout(() => {
  75. wx.canvasToTempFilePath({
  76. x: 0,
  77. y: 0,
  78. width: 300,
  79. height: 525,
  80. // destWidth: 370,
  81. destWidth: 1200,
  82. // destHeight: 507,
  83. destHeight: 2100,
  84. canvasId: 'myCanvas',
  85. success: (res) => {
  86. console.log(res.tempFilePath)
  87. that.setData({
  88. imgUrls: res.tempFilePath
  89. }, () => {
  90. wx.hideLoading();
  91. })
  92. },
  93. fail: (res) => {
  94. console.log(res)
  95. }
  96. })
  97. }, 200)
  98. })
  99. },
  100. fail: function (res) {
  101. //失败回调
  102. console.log(res)
  103. }
  104. });
  105. })
  106. },
  107. share: function (data) {
  108. console.log(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. productId: data.productId,
  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. groupType: data.groupType
  142. })
  143. }
  144. this.poster();
  145. if (this.data.shareType === 'class') {
  146. console.log('课程分享')
  147. this.setData({
  148. shareFlag: !this.data.shareFlag
  149. })
  150. }
  151. // 动画测试
  152. this.animation = wx.createAnimation({
  153. duration: 700,
  154. timingFunction: 'ease',
  155. })
  156. this.animation.bottom(0).step();
  157. this.setData({
  158. animationData: this.animation.export()
  159. })
  160. // 动画测试
  161. },
  162. //关闭弹框
  163. close: function (e) {
  164. this.animation = wx.createAnimation({
  165. duration: 400,
  166. timingFunction: 'ease',
  167. })
  168. this.animation.bottom(-300).step();
  169. this.setData({
  170. animationData: this.animation.export()
  171. }, () => {
  172. setTimeout(() => {
  173. this.setData({
  174. shareFlag: !this.data.shareFlag,
  175. imgUrls: ''
  176. })
  177. this.triggerEvent('ShareDialogClose', {})
  178. }, 200)
  179. })
  180. },
  181. nothing: function (e) {
  182. return
  183. },
  184. // 保存图片到本地
  185. saveImg: function () {
  186. console.log('yibuububu')
  187. let download = new Promise((resolve, reject) => {
  188. let QRCode = () => {
  189. // 生成二维码并下载
  190. httpRequestApi.createQRCode(this.data.QRData).success(res => {
  191. console.log(this.data.QRData);
  192. this.setData({
  193. QRCodeImagePath: res.data.data
  194. })
  195. // wx.downloadFile({
  196. // url: res,
  197. // success: (res) => {
  198. // console.log(res)
  199. // console.log('下载二维码成功')
  200. // // this.setData({
  201. // // avatar: res.tempFilePath
  202. // // }, () => {
  203. // // })
  204. // }
  205. // });
  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, 1200, 2100, 0, 0, 600, 1050); //插入图片
  250. } else {
  251. ctx.drawImage('../../../static/image/group_poster.jpg', 0, 0, 1200, 2100, 0, 0, 600, 1050); //插入图片
  252. }
  253. ctx.save()
  254. ctx.beginPath()
  255. ctx.fill()
  256. ctx.setFontSize(25)
  257. ctx.setFillStyle('#fff')
  258. ctx.fillText('课文朗读 每天5分钟', (300 - ctx.measureText('课文朗读 每天5分钟').width) / 2, 54.5)
  259. ctx.setFontSize(16)
  260. ctx.fillText('大声朗读是语感形成的必走之路', (300 - ctx.measureText('大声朗读是语感形成的必走之路').width) / 2, 75.5)
  261. ctx.setFontSize(15)
  262. ctx.setFillStyle('#838383')
  263. ctx.fillText('长按识别二维码', 130.5, 391)
  264. ctx.setFontSize(12)
  265. ctx.setFillStyle('#838383')
  266. ctx.fillText('接收邀请,获得你的课程吧', 130.5, 409.5)
  267. resolve();
  268. });
  269. return background;
  270. },
  271. // 画头像和作者||发起者
  272. drawAuthor: function (ctx) {
  273. let author = new Promise((resolve, reject) => {
  274. if (this.data.shareType === 'works') {
  275. ctx.arc(150, 160, 36, 0, 2 * Math.PI, true)
  276. ctx.clip(); //剪切头像区域
  277. ctx.drawImage(this.data.avatar, 116, 126, 68, 68);
  278. ctx.arc(150, 160, 34, 0, 2 * Math.PI, false)
  279. ctx.setFillStyle('#ff6f42')
  280. ctx.fill();
  281. ctx.clip(); //剪切头像区域
  282. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  283. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  284. ctx.setFontSize(15)
  285. ctx.setFillStyle('#ff6f42')
  286. ctx.fillText(this.data.author, (300 - ctx.measureText(this.data.author).width) / 2, 220)
  287. ctx.setFillStyle('#434343')
  288. ctx.fillText('播音主持特长生', (300 - ctx.measureText('播音主持特长生').width) / 2, 243)
  289. } else {
  290. ctx.arc(64, 146, 29, 0, 2 * Math.PI, true)
  291. ctx.clip(); //剪切头像区域
  292. ctx.drawImage(this.data.avatar, 37, 119, 54, 54);
  293. ctx.arc(64, 146, 27, 0, 2 * Math.PI, false)
  294. ctx.setFillStyle('#ff6f42')
  295. ctx.fill();
  296. ctx.clip(); //剪切头像区域
  297. //插入图片
  298. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  299. ctx.setFontSize(18)
  300. ctx.setFillStyle('#ff6f42')
  301. ctx.fillText(this.data.author, 100, 143.5)
  302. ctx.setFontSize(16)
  303. ctx.setFillStyle('#434343')
  304. ctx.fillText('邀您和我一起大声朗读', 100, 167.5)
  305. }
  306. resolve();
  307. })
  308. return author;
  309. },
  310. // 分享作品的主题内容
  311. drawWorksGut: function (ctx) {
  312. let works = new Promise((resolve, reject) => {
  313. // resolve(() => {
  314. console.log('开始画内容')
  315. ctx.setFontSize(15)
  316. ctx.setFillStyle('#434343')
  317. ctx.fillText('使出洪荒之力,声情并茂地朗诵了', (300 - ctx.measureText('使出洪荒之力,声情并茂地朗诵了').width) / 2, 262)
  318. ctx.fillText(`《${this.data.title}》`, (300 - ctx.measureText(`《${this.data.title}》`).width) / 2, 284)
  319. resolve();
  320. })
  321. return works;
  322. },
  323. // 分享团购的主体内容
  324. drawGroupGut: function (ctx) {
  325. let group = new Promise((resolve, reject) => {
  326. ctx.drawImage(this.data.iconImg, 27, 194, 247, 103.5);
  327. ctx.setFontSize(15)
  328. ctx.setFillStyle('#434343')
  329. ctx.fillText('朗朗读书声,是世界上最美妙的声音', (300 - ctx.measureText('朗朗读书声,是世界上最美妙的声音').width) / 2, 327.5)
  330. resolve();
  331. })
  332. return group;
  333. },
  334. // 保存最终图片
  335. PreservationImg: function () {
  336. // this.getImgAuthorize()
  337. wx.saveImageToPhotosAlbum({
  338. filePath: this.data.imgUrls,
  339. success: (res) => {
  340. console.log(res)
  341. const data = {};
  342. console.log('productId', this.data.productId)
  343. // 团购分享记录
  344. if (this.data.shareType === 'group') {
  345. data.shareTypeEnum = 'POSTER';
  346. data.shareContentEnum = 'GROUP';
  347. data.productId = this.data.groupPurchaseInfo.productId;
  348. }
  349. // 朗读分享记录
  350. if (this.data.shareType === 'works') {
  351. data.shareTypeEnum = 'POSTER';
  352. data.shareContentEnum = 'READ';
  353. data.productId = this.data.productId;
  354. }
  355. httpRequestApi.shareRecord(data).success((res) => {
  356. console.log(res)
  357. })
  358. this.setData({
  359. imgUrls: false
  360. })
  361. },
  362. fail: res => {
  363. wx.getSetting({
  364. success(res) {
  365. if (!res.authSetting['scope.writePhotosAlbum']) {
  366. // wx.showToast({
  367. // title: '无权限操作',
  368. // icon: 'fail',
  369. // duration: 800
  370. // })
  371. wx.showModal({
  372. title: '无法保存分享图片到相册',
  373. content: '点击右上角浮点按钮->进入关于->点击右上角按钮->设置,进行授权',
  374. confirmText: '我知道了',
  375. showCancel: false,
  376. success(res) {
  377. console.log('用户点击确定')
  378. }
  379. })
  380. }
  381. }
  382. })
  383. }
  384. })
  385. },
  386. shareFriend: function () {
  387. this.triggerEvent('customevent', {})
  388. }
  389. },
  390. onShareAppMessage: (res) => {
  391. if (res.from === 'button') {
  392. // 来自页面内转发按钮
  393. console.log(res.target)
  394. }
  395. return {
  396. title: this.data.title,
  397. path: this.data.path
  398. }
  399. },
  400. })