share.js 9.7 KB

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