share.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. // path: data.path,
  208. QRData: {
  209. page: data.path,
  210. scene: data.scene
  211. }
  212. })
  213. }
  214. if (this.data.shareType === 'group') {
  215. console.log(data)
  216. console.log('团购分享')
  217. this.setData({
  218. shareFlag: !this.data.shareFlag,
  219. avatar: data.avatar,
  220. author: data.author,
  221. iconImg: data.iconImg,
  222. title: data.title,
  223. // path: data.path,
  224. QRData: {
  225. page: data.path,
  226. scene: data.scene
  227. },
  228. groupPurchaseInfo: data.groupPurchaseInfo,
  229. groupType: data.groupType
  230. })
  231. }
  232. this.poster();
  233. if (this.data.shareType === 'class') {
  234. console.log('课程分享')
  235. this.setData({
  236. shareFlag: !this.data.shareFlag
  237. })
  238. }
  239. // 动画测试
  240. this.animation = wx.createAnimation({
  241. duration: 700,
  242. timingFunction: 'ease',
  243. })
  244. this.animation.bottom(0).step();
  245. this.setData({
  246. animationData: this.animation.export()
  247. })
  248. // 动画测试
  249. },
  250. //关闭弹框
  251. close: function (e) {
  252. this.animation = wx.createAnimation({
  253. duration: 400,
  254. timingFunction: 'ease',
  255. })
  256. this.animation.bottom(-300).step();
  257. this.setData({
  258. animationData: this.animation.export()
  259. }, () => {
  260. setTimeout(() => {
  261. this.setData({
  262. shareFlag: !this.data.shareFlag,
  263. imgUrls: ''
  264. })
  265. this.triggerEvent('ShareDialogClose', {})
  266. }, 200)
  267. })
  268. wx.hideLoading()
  269. },
  270. nothing: function (e) {
  271. return
  272. },
  273. // 保存最终图片
  274. PreservationImg: function () {
  275. // this.getImgAuthorize()
  276. wx.saveImageToPhotosAlbum({
  277. filePath: this.data.imgUrls,
  278. success: (res) => {
  279. wx.showModal({
  280. title: '海报已保存至相册',
  281. content: '快去分享给小伙伴吧',
  282. confirmText: '我知道了',
  283. showCancel: false,
  284. success(res) {
  285. console.log('用户点击确定')
  286. }
  287. })
  288. /* const data = {};
  289. // 团购分享记录
  290. if (this.data.shareType === 'group') {
  291. data.shareTypeEnum = 'POSTER';
  292. data.shareContentEnum = 'GROUP';
  293. data.productId = this.data.groupPurchaseInfo.productId;
  294. }
  295. // 朗读分享记录
  296. if (this.data.shareType === 'works') {
  297. data.shareTypeEnum = 'POSTER';
  298. data.shareContentEnum = 'READ';
  299. data.productId = this.data.productId;
  300. } */
  301. httpRequestApi.shareLog({
  302. readId:this.data.id,
  303. type:this.data.type,
  304. grade:this.data.grade
  305. }).success((res) => {
  306. console.log('分享记录',res)
  307. })
  308. this.setData({
  309. imgUrls: false
  310. })
  311. },
  312. fail: res => {
  313. wx.getSetting({
  314. success(res) {
  315. if (!res.authSetting['scope.writePhotosAlbum']) {
  316. // wx.showToast({
  317. // title: '无权限操作',
  318. // icon: 'fail',
  319. // duration: 800
  320. // })
  321. wx.showModal({
  322. title: '无法保存分享图片到相册',
  323. content: '点击右上角浮点按钮->设置,进行授权',
  324. confirmText: '我知道了',
  325. showCancel: false,
  326. success(res) {
  327. console.log('用户点击确定')
  328. }
  329. })
  330. }
  331. }
  332. })
  333. }
  334. })
  335. },
  336. shareFriend: function () {
  337. httpRequestApi.shareLog({
  338. readId:this.data.id,
  339. type:this.data.type,
  340. grade:this.data.grade
  341. }).success((res) => {
  342. console.log('分享记录',res)
  343. })
  344. this.triggerEvent('customevent', {})
  345. }
  346. },
  347. onShareAppMessage: (e) => {
  348. console.log('点击分享按钮',e)
  349. if (res.from === 'button') {
  350. // 来自页面内转发按钮
  351. console.log(res.target)
  352. }else{
  353. return {
  354. title: '课文朗读,从未如此有趣。',
  355. path: '/pages/index/index'
  356. }
  357. }
  358. },
  359. })