share.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. },
  13. /**
  14. * 组件的初始数据
  15. */
  16. data: {
  17. shareFlag: false,
  18. imgUrls: '',
  19. avatar: '',
  20. author: '',
  21. iconImg: '',
  22. title: '',
  23. path: ''
  24. },
  25. /**
  26. * 组件的方法列表
  27. */
  28. methods: {
  29. poster: function () {
  30. const that = this;
  31. const ctx = wx.createCanvasContext('myCanvas');
  32. // 先画背景
  33. this.drawBg(ctx)
  34. .then(() => {
  35. // 下载保存图片
  36. console.log('背景画完');
  37. return this.saveImg(ctx);
  38. })
  39. .then(() => {
  40. // 画头像和作者||发起者
  41. console.log('头像画完');
  42. return this.drawAuthor(ctx);
  43. })
  44. .then(() => {
  45. console.log(this.data.shareType)
  46. if (this.data.shareType === 'works') {
  47. // 分享作品的主题内容
  48. console.log('主题画完');
  49. return this.drawWorksGut(ctx)
  50. } else {
  51. return this.drawGroupGut(ctx)
  52. }
  53. })
  54. .then(() => {
  55. ctx.drawImage('../../../static/groupImg/code.png', 217, 374, 135, 135);
  56. console.log(ctx)
  57. ctx.draw(true, function (res) {
  58. wx.canvasToTempFilePath({
  59. x: 0,
  60. y: 0,
  61. width: 370,
  62. height: 507,
  63. destWidth: 370,
  64. destHeight: 507,
  65. canvasId: 'myCanvas',
  66. success: (res) => {
  67. console.log(res.tempFilePath)
  68. wx.saveImageToPhotosAlbum({
  69. filePath: res.path,
  70. success(res) {
  71. console.log(res);
  72. }
  73. })
  74. that.setData({
  75. imgUrls: res.tempFilePath
  76. },()=>{
  77. wx.hideLoading();
  78. })
  79. },
  80. fail: (res) => {
  81. console.log(res)
  82. }
  83. })
  84. })
  85. })
  86. },
  87. share: function (data) {
  88. if (this.data.shareType === 'works') {
  89. this.setData({
  90. shareFlag: !this.data.shareFlag,
  91. avatar: data.avatar,
  92. author: data.author,
  93. iconImg: data.iconImg,
  94. title: data.title,
  95. // path: data.path,
  96. // QRData:{
  97. // page: data.path,
  98. // scene: "par=no"
  99. // }
  100. })
  101. }
  102. if (this.data.shareType === 'group') {
  103. console.log('团购分享')
  104. this.setData({
  105. shareFlag: !this.data.shareFlag
  106. })
  107. }
  108. if (this.data.shareType === 'class') {
  109. console.log('课程分享')
  110. this.setData({
  111. shareFlag: !this.data.shareFlag
  112. })
  113. }
  114. },
  115. // 保存图片到本地
  116. saveImg: function () {
  117. console.log('yibuububu')
  118. let download = new Promise((resolve, reject) => {
  119. let downAvatar = () =>{
  120. wx.downloadFile({
  121. url: this.data.avatar,
  122. success: (res) => {
  123. console.log('下载头像成功')
  124. console.log(this.data.avatar)
  125. this.setData({
  126. avatar: res.tempFilePath
  127. })
  128. resolve();
  129. }
  130. });
  131. }
  132. // resolve(()=>{
  133. // 下载icon
  134. wx.downloadFile({
  135. url: this.data.iconImg,
  136. success: (res) => {
  137. console.log('下载icon成功')
  138. console.log(this.data.iconImg)
  139. this.setData({
  140. iconImg: res.tempFilePath
  141. })
  142. // 下载头像
  143. downAvatar();
  144. }
  145. });
  146. // 生成二维码并下载
  147. // httpRequestApi.createQRCode(this.data.QRData).success(res => {
  148. // console.log(data.path)
  149. // console.log(res)
  150. // })
  151. // })
  152. })
  153. return download;
  154. },
  155. // 先画出背景
  156. drawBg: function (ctx) {
  157. let background = new Promise((resolve, reject) => {
  158. wx.showLoading({
  159. title: '海报生成中',
  160. mask: true
  161. })
  162. console.log('开始画背景')
  163. ctx.rect(0, 0, 370, 507)
  164. ctx.setFillStyle('#fff')
  165. ctx.fill()
  166. ctx.save()
  167. ctx.beginPath()
  168. ctx.setFillStyle('#fff')
  169. ctx.fill()
  170. ctx.arc(185, 48, 37, 0, 2 * Math.PI, false)
  171. ctx.clip(); //画
  172. resolve();
  173. });
  174. return background;
  175. },
  176. // 画头像和作者||发起者
  177. drawAuthor: function (ctx) {
  178. let author = new Promise((resolve, reject) => {
  179. // resolve(() => {
  180. console.log('开始画头像')
  181. ctx.drawImage(this.data.avatar, 148, 8, 100, 100); //插入图片
  182. ctx.restore(); //恢复之前保存的绘图上下文 恢复之前保存的绘图上下午即状态 可以继续绘制
  183. ctx.setFontSize(20)
  184. ctx.setFillStyle('#535353')
  185. ctx.fillText(this.data.author, (370 - ctx.measureText(this.data.author).width) / 2, 115)
  186. ctx.setFillStyle('#FF4400')
  187. // })
  188. resolve();
  189. })
  190. return author;
  191. },
  192. // 分享作品的主题内容
  193. drawWorksGut: function (ctx) {
  194. let works = new Promise((resolve, reject) => {
  195. // resolve(() => {
  196. console.log('开始画内容')
  197. ctx.fillText('已使出洪荒之力,声情并茂的为', (370 - ctx.measureText('已使出洪荒之力,声情并茂的为').width) / 2, 138)
  198. ctx.fillText(`《${this.data.title}》`, (370 - ctx.measureText(`《${this.data.title}》`).width) / 2, 160)
  199. ctx.fillText(`配了一段惊世之作`, (370 - ctx.measureText(`配了一段惊世之作`).width) / 2, 182)
  200. ctx.drawImage(this.data.iconImg, 9, 205, 352, 145);
  201. ctx.setFontSize(18)
  202. ctx.setFillStyle('#000')
  203. ctx.drawImage('../../../static/groupImg/share_bottom.png', 3, 419, 370, 192);
  204. ctx.setFontSize(20)
  205. ctx.setFillStyle('red')
  206. ctx.fillText('为TA疯狂打CALL', 9, 467)
  207. ctx.setFontSize(18)
  208. ctx.setFillStyle('#000')
  209. ctx.fillText('长按识别二维码,快去听听', 9, 493)
  210. // })
  211. resolve();
  212. })
  213. return works;
  214. },
  215. // 分享团购的主体内容
  216. drawGroupGut: function (ctx) {
  217. let group = new Promise((resolve, reject) => {
  218. resolve(() => {
  219. ctx.fillText('郎朗读书声是世上最美的声音', 50, 138)
  220. ctx.drawImage('../../../static/groupImg/Combined Shape.png', 11, 348, 196, 65);
  221. ctx.drawImage('../../../static/groupImg/Rectangle 41.png', 134, 358, 59, 20);
  222. ctx.setFontSize(14)
  223. ctx.setFillStyle('#fff')
  224. ctx.fillText('五人团', 149, 373)
  225. ctx.setFontSize(16)
  226. ctx.setFillStyle('#A95A00')
  227. ctx.fillText('原价', 24, 373)
  228. ctx.setStrokeStyle('red')
  229. ctx.moveTo(64, 368)
  230. ctx.lineTo(114, 368)
  231. ctx.stroke()
  232. ctx.fillText('¥99元', 64, 373)
  233. ctx.setFontSize(18)
  234. ctx.fillText('团购价仅需', 20, 403)
  235. ctx.fillText('元', 144, 403)
  236. ctx.setFontSize(20)
  237. ctx.setFillStyle('red')
  238. ctx.fillText('¥1', 109, 403)
  239. ctx.drawImage('../../../static/groupImg/share_bottom.png', 3, 419, 370, 192);
  240. ctx.setFontSize(20)
  241. ctx.setFillStyle('red')
  242. ctx.fillText('名额有限,售完截止', 9, 467)
  243. ctx.setFontSize(18)
  244. ctx.setFillStyle('#000')
  245. ctx.fillText('长按识别二维码参加团购', 9, 493)
  246. })
  247. })
  248. return group();
  249. },
  250. // 保存最终图片
  251. PreservationImg: function () {
  252. wx.saveImageToPhotosAlbum({
  253. filePath: this.data.imgUrls,
  254. success(res) {
  255. console.log('保存成功')
  256. }
  257. })
  258. },
  259. shareFriend: function () {
  260. this.triggerEvent('customevent', {})
  261. }
  262. },
  263. onShareAppMessage: (res) => {
  264. if (res.from === 'button') {
  265. // 来自页面内转发按钮
  266. console.log(res.target)
  267. }
  268. return {
  269. title: this.data.title,
  270. path: this.data.path
  271. }
  272. },
  273. })