share.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. randomSologan: [
  35. '开发右脑最简单的方式就是大声朗读',
  36. '大声朗读 有利于建立自信心',
  37. '大声朗读 能帮助孩子提高写作能力',
  38. '朗读是感知世界的一种重要方式',
  39. '增强记忆力的方法之一大声朗读',
  40. '大声朗读是语感形成的最佳途径',
  41. '大声朗读 还原课文的诗性美',
  42. '在朗读中 提升孩子的思辨能力',
  43. '重温经典课文 享受亲子美好时光',
  44. '大声朗读 能帮助孩子提高写作能力'
  45. ]
  46. },
  47. /**
  48. * 组件的方法列表
  49. */
  50. methods: {
  51. touchMove: function () {
  52. return false
  53. },
  54. poster: function () {
  55. const that = this;
  56. wx.showLoading({
  57. title: '专属海报生成中',
  58. // mask: true
  59. })
  60. httpRequestApi.createQRCode(this.data.QRData).success(res => {
  61. // 下载二维码
  62. wx.downloadFile({
  63. url: res.data.data,
  64. success: res => {
  65. this.setData({
  66. QRCodeImagePath: res.tempFilePath
  67. }, () => {
  68. this.saveAvatar();
  69. })
  70. }
  71. });
  72. })
  73. },
  74. makeImage: function () {
  75. const ctx = wx.createCanvasContext('myCanvas');
  76. // 画出背景和相同内容
  77. if (this.data.shareType === 'works') {
  78. ctx.drawImage('../../static/image/works_poster.jpg', 0, 0, 1200, 2100, 0, 0, 600, 1050); //插入图片
  79. } else {
  80. ctx.drawImage('../../static/image/group_poster.jpg', 0, 0, 1200, 2100, 0, 0, 600, 1050); //插入图片
  81. }
  82. ctx.save()
  83. ctx.beginPath()
  84. ctx.fill()
  85. ctx.setFontSize(25)
  86. ctx.setFillStyle('#fff')
  87. ctx.fillText('课文朗读 每天5分钟', (300 - ctx.measureText('课文朗读 每天5分钟').width) / 2, 54.5)
  88. ctx.setFontSize(16)
  89. let randomSologan = this.data.randomSologan[Math.floor(Math.random() * 10)];
  90. ctx.fillText(randomSologan, (300 - ctx.measureText(randomSologan).width) / 2, 75.5)
  91. ctx.setFontSize(15)
  92. ctx.setFillStyle('#838383')
  93. ctx.fillText('长按识别二维码', 130.5, 391)
  94. ctx.setFontSize(12)
  95. ctx.setFillStyle('#838383')
  96. ctx.fillText('接收邀请,获得你的课程吧', 130.5, 409.5) /* */
  97. // 头像
  98. console.log('画头像')
  99. if (this.data.shareType === 'works') {
  100. ctx.arc(150, 160, 36, 0, 2 * Math.PI, true)
  101. ctx.clip(); //剪切头像区域
  102. ctx.drawImage(this.data.avatar, 116, 126, 68, 68);
  103. ctx.arc(150, 160, 34, 0, 2 * Math.PI, false)
  104. ctx.setFillStyle('#ff6f42')
  105. ctx.fill();
  106. ctx.clip(); //剪切头像区域
  107. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  108. ctx.setFontSize(15)
  109. ctx.setFillStyle('#ff6f42')
  110. ctx.fillText(this.data.author, (300 - ctx.measureText(this.data.author).width) / 2, 220)
  111. ctx.setFillStyle('#434343')
  112. ctx.fillText('播音主持特长生', (300 - ctx.measureText('播音主持特长生').width) / 2, 243)
  113. ctx.setFontSize(15)
  114. ctx.setFillStyle('#434343')
  115. ctx.fillText('使出洪荒之力,声情并茂地朗诵了', (300 - ctx.measureText('使出洪荒之力,声情并茂地朗诵了').width) / 2, 262)
  116. ctx.fillText(`《${this.data.title}》`, (300 - ctx.measureText(`《${this.data.title}》`).width) / 2, 284)
  117. } else {
  118. ctx.arc(64, 146, 29, 0, 2 * Math.PI, true)
  119. ctx.clip(); //剪切头像区域
  120. ctx.drawImage(this.data.avatar, 37, 119, 54, 54);
  121. ctx.arc(64, 146, 27, 0, 2 * Math.PI, false)
  122. ctx.setFillStyle('#ff6f42')
  123. ctx.fill();
  124. ctx.clip(); //剪切头像区域
  125. //插入图片
  126. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  127. ctx.setFontSize(18)
  128. ctx.setFillStyle('#ff6f42')
  129. ctx.fillText(this.data.author, 100, 143.5)
  130. ctx.setFontSize(16)
  131. ctx.setFillStyle('#434343')
  132. ctx.fillText('邀您和我一起大声朗读', 100, 167.5)
  133. ctx.drawImage(this.data.iconImg, 27, 194, 247, 103.5);
  134. ctx.setFontSize(15)
  135. ctx.setFillStyle('#434343')
  136. ctx.fillText('朗朗读书声,是世界上最美妙的声音', (300 - ctx.measureText('朗朗读书声,是世界上最美妙的声音').width) / 2, 327.5)
  137. }
  138. console.log('花二维码')
  139. console.log(this.data)
  140. console.log(this.data.avatar)
  141. ctx.drawImage(this.data.QRCodeImagePath, 35, 351, 90, 90);
  142. ctx.draw();
  143. setTimeout(() => {
  144. wx.canvasToTempFilePath({
  145. x: 0,
  146. y: 0,
  147. width: 300,
  148. height: 525,
  149. destWidth: 1200,
  150. destHeight: 2100,
  151. canvasId: 'myCanvas',
  152. success: (res) => {
  153. this.setData({
  154. imgUrls: res.tempFilePath
  155. })
  156. wx.hideLoading()
  157. },
  158. fail: (res) => {
  159. console.log('史称失败',res)
  160. wx.hideLoading();
  161. }
  162. })
  163. }, 400)
  164. },
  165. saveIcon: function () {
  166. wx.downloadFile({
  167. url: this.data.iconImg,
  168. success: res => {
  169. this.setData({
  170. iconImg: res.tempFilePath
  171. }, () => {
  172. console.log(this.data.iconImg)
  173. this.makeImage();
  174. })
  175. }
  176. });
  177. },
  178. saveAvatar: function () {
  179. wx.downloadFile({
  180. url: this.data.avatar,
  181. success: res => {
  182. this.setData({
  183. avatar: res.tempFilePath
  184. }, () => {
  185. if (this.data.shareType === 'group') {
  186. this.saveIcon();
  187. } else {
  188. this.makeImage();
  189. }
  190. })
  191. }
  192. });
  193. },
  194. share: function (data) {
  195. console.log('分享内容',data)
  196. if (this.data.shareType === 'works') {
  197. this.setData({
  198. shareFlag: !this.data.shareFlag,
  199. avatar: data.avatar,
  200. author: data.author,
  201. iconImg: data.iconImg,
  202. title: data.title,
  203. productId: data.productId,
  204. id:data.scene,
  205. type:data.type,
  206. grade:data.grade,
  207. shareImg: data.shareImg,
  208. // path: data.path,
  209. QRData: {
  210. page: data.path,
  211. scene: data.scene
  212. }
  213. })
  214. }
  215. if (this.data.shareType === 'group') {
  216. console.log(data)
  217. console.log('团购分享')
  218. this.setData({
  219. shareFlag: !this.data.shareFlag,
  220. avatar: data.avatar,
  221. author: data.author,
  222. iconImg: data.iconImg,
  223. title: data.title,
  224. // path: data.path,
  225. QRData: {
  226. page: data.path,
  227. scene: data.scene
  228. },
  229. groupPurchaseInfo: data.groupPurchaseInfo,
  230. groupType: data.groupType
  231. })
  232. }
  233. this.poster();
  234. if (this.data.shareType === 'class') {
  235. console.log('课程分享')
  236. this.setData({
  237. shareFlag: !this.data.shareFlag
  238. })
  239. }
  240. // 动画测试
  241. this.animation = wx.createAnimation({
  242. duration: 700,
  243. timingFunction: 'ease',
  244. })
  245. this.animation.bottom(0).step();
  246. this.setData({
  247. animationData: this.animation.export()
  248. })
  249. // 动画测试
  250. },
  251. //关闭弹框
  252. close: function (e) {
  253. this.animation = wx.createAnimation({
  254. duration: 400,
  255. timingFunction: 'ease',
  256. })
  257. this.animation.bottom(-300).step();
  258. this.setData({
  259. animationData: this.animation.export()
  260. }, () => {
  261. setTimeout(() => {
  262. this.setData({
  263. shareFlag: !this.data.shareFlag,
  264. imgUrls: ''
  265. })
  266. this.triggerEvent('ShareDialogClose', {})
  267. }, 200)
  268. })
  269. wx.hideLoading()
  270. },
  271. nothing: function (e) {
  272. return
  273. },
  274. // 保存最终图片
  275. PreservationImg: function () {
  276. // this.getImgAuthorize()
  277. wx.saveImageToPhotosAlbum({
  278. filePath: this.data.imgUrls,
  279. success: (res) => {
  280. wx.showModal({
  281. title: '海报已保存至相册',
  282. content: '快去分享给小伙伴吧',
  283. confirmText: '我知道了',
  284. showCancel: false,
  285. success(res) {
  286. console.log('用户点击确定')
  287. }
  288. })
  289. /* const data = {};
  290. // 团购分享记录
  291. if (this.data.shareType === 'group') {
  292. data.shareTypeEnum = 'POSTER';
  293. data.shareContentEnum = 'GROUP';
  294. data.productId = this.data.groupPurchaseInfo.productId;
  295. }
  296. // 朗读分享记录
  297. if (this.data.shareType === 'works') {
  298. data.shareTypeEnum = 'POSTER';
  299. data.shareContentEnum = 'READ';
  300. data.productId = this.data.productId;
  301. } */
  302. httpRequestApi.shareLog({
  303. readId:this.data.id,
  304. type:this.data.type,
  305. grade:this.data.grade
  306. }).success((res) => {
  307. console.log('分享记录',res)
  308. })
  309. this.setData({
  310. imgUrls: false
  311. })
  312. },
  313. fail: res => {
  314. wx.getSetting({
  315. success(res) {
  316. if (!res.authSetting['scope.writePhotosAlbum']) {
  317. // wx.showToast({
  318. // title: '无权限操作',
  319. // icon: 'fail',
  320. // duration: 800
  321. // })
  322. wx.showModal({
  323. title: '无法保存分享图片到相册',
  324. content: '点击右上角浮点按钮->设置,进行授权',
  325. confirmText: '我知道了',
  326. showCancel: false,
  327. success(res) {
  328. console.log('用户点击确定')
  329. }
  330. })
  331. }
  332. }
  333. })
  334. }
  335. })
  336. },
  337. shareFriend: function () {
  338. httpRequestApi.shareLog({
  339. readId:this.data.id,
  340. type:this.data.type,
  341. grade:this.data.grade
  342. }).success((res) => {
  343. console.log('分享记录',res)
  344. })
  345. this.triggerEvent('customevent', {})
  346. }
  347. },
  348. onShareAppMessage: (e) => {
  349. console.log('点击分享按钮',e)
  350. if (res.from === 'button') {
  351. // 来自页面内转发按钮
  352. console.log(res.target)
  353. }else{
  354. return {
  355. title: '课文朗读,从未如此有趣。',
  356. path: '/pages/index/index'
  357. }
  358. }
  359. },
  360. })