ContentSharePoster.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <template>
  2. <view class="poster-overlay" v-if="show" @click.self="$emit('close')">
  3. <view class="poster-container">
  4. <canvas canvas-id="contentPosterCanvas" class="poster-canvas" id="contentPosterCanvas"></canvas>
  5. <view class="poster-actions">
  6. <button class="poster-btn save" @click="savePoster">保存到相册</button>
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'ContentSharePoster',
  14. props: {
  15. show: { type: Boolean, default: false },
  16. title: { type: String, default: '' },
  17. coverImage: { type: String, default: '' },
  18. qrCodeBase64: { type: String, default: '' },
  19. typeLabel: { type: String, default: '好物推荐' },
  20. dimensionCode: { type: String, default: '' },
  21. summary: { type: String, default: '' },
  22. priceText: { type: String, default: '' },
  23. ctaText: { type: String, default: '长按识别二维码 · 查看详情' },
  24. inviteText: { type: String, default: '' }
  25. },
  26. watch: {
  27. show(val) {
  28. if (val) {
  29. this.$nextTick(function() {
  30. setTimeout(this.drawPoster.bind(this), 300)
  31. }.bind(this))
  32. }
  33. }
  34. },
  35. methods: {
  36. getColors() {
  37. var map = {
  38. body: { main: '#FF8C42', dark: '#EA580C', light: '#FFF7ED' },
  39. mind: { main: '#FF6B9D', dark: '#DB2777', light: '#FDF2F8' },
  40. wisdom: { main: '#6366F1', dark: '#4F46E5', light: '#EEF2FF' },
  41. action: { main: '#10B981', dark: '#059669', light: '#ECFDF5' },
  42. wealth: { main: '#F59E0B', dark: '#D97706', light: '#FFFBEB' }
  43. }
  44. return map[this.dimensionCode] || { main: '#F97316', dark: '#EA580C', light: '#FFF7ED' }
  45. },
  46. drawPoster() {
  47. var self = this
  48. uni.createSelectorQuery().in(this)
  49. .select('#contentPosterCanvas')
  50. .boundingClientRect(function(rect) {
  51. var scaleX = 1
  52. var scaleY = 1
  53. if (rect && rect.width && rect.height) {
  54. scaleX = rect.width / 500
  55. scaleY = rect.height / 800
  56. }
  57. self._renderPoster(scaleX, scaleY)
  58. })
  59. .exec()
  60. },
  61. _renderPoster(scaleX, scaleY) {
  62. var ctx = uni.createCanvasContext('contentPosterCanvas', this)
  63. var w = 500
  64. var h = 800
  65. var colors = this.getColors()
  66. // 画布 CSS 用 rpx,实际像素随设备变化(375px 屏约 310x496),而绘制按 500x800 设计坐标;
  67. // 不缩放坐标系时右侧/底部内容(含二维码 y≈552-702)会被裁剪出画布,导致海报显示不全。
  68. if (scaleX !== 1 || scaleY !== 1) {
  69. ctx.scale(scaleX, scaleY)
  70. }
  71. var gradient = ctx.createLinearGradient(0, 0, w * 0.3, h)
  72. gradient.addColorStop(0, colors.main)
  73. gradient.addColorStop(0.55, colors.dark)
  74. gradient.addColorStop(1, colors.dark)
  75. ctx.setFillStyle(gradient)
  76. ctx.fillRect(0, 0, w, h)
  77. this._drawTopDecoration(ctx, w, colors)
  78. var cardX = 28
  79. var cardY = 112
  80. var cardW = 444
  81. var cardH = 600
  82. this._drawContentCard(ctx, cardX, cardY, cardW, cardH, colors)
  83. var coverY = cardY + 20
  84. var coverW = cardW - 40
  85. var coverH = 220
  86. var self = this
  87. if (this.coverImage) {
  88. uni.downloadFile({
  89. url: this.coverImage,
  90. success: function(res) {
  91. if (res.statusCode === 200) {
  92. ctx.drawImage(res.tempFilePath, cardX + 20, coverY, coverW, coverH)
  93. } else {
  94. self._drawCoverPlaceholder(ctx, cardX + 20, coverY, coverW, coverH)
  95. }
  96. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors)
  97. },
  98. fail: function() {
  99. self._drawCoverPlaceholder(ctx, cardX + 20, coverY, coverW, coverH)
  100. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors)
  101. }
  102. })
  103. } else {
  104. this._drawCoverPlaceholder(ctx, cardX + 20, coverY, coverW, coverH)
  105. this._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors)
  106. }
  107. },
  108. _drawTopDecoration(ctx, w, colors) {
  109. var centerX = w / 2
  110. var dotColors = ['#FF8C42', '#6366F1', '#F59E0B', '#10B981', '#FF6B9D']
  111. var dotY = 28
  112. var dotR = 5
  113. var dotSpacing = 26
  114. var i, offsetX, offsetY
  115. for (i = 0; i < 5; i++) {
  116. offsetX = (i - 2) * dotSpacing
  117. offsetY = Math.abs(i - 2) * 3
  118. ctx.setFillStyle(dotColors[i])
  119. ctx.beginPath()
  120. ctx.arc(centerX + offsetX, dotY + offsetY, dotR, 0, 2 * Math.PI)
  121. ctx.fill()
  122. }
  123. ctx.setFillStyle('#ffffff')
  124. ctx.setFontSize(20)
  125. ctx.setTextAlign('center')
  126. ctx.setTextBaseline('top')
  127. ctx.fillText('浠艾福', centerX, 52)
  128. ctx.setFontSize(14)
  129. ctx.setFillStyle('rgba(255,255,255,0.8)')
  130. ctx.fillText('给全家的一站式幸福提案', centerX, 76)
  131. ctx.setStrokeStyle('rgba(255,255,255,0.25)')
  132. ctx.setLineWidth(1)
  133. ctx.beginPath()
  134. ctx.moveTo(80, 100)
  135. ctx.lineTo(w - 80, 100)
  136. ctx.stroke()
  137. },
  138. _drawContentCard(ctx, x, y, w, h, colors) {
  139. ctx.setFillStyle('rgba(0,0,0,0.04)')
  140. this._roundRect(ctx, x + 2, y + 3, w, h, 16)
  141. ctx.fill()
  142. ctx.setFillStyle(colors.light)
  143. this._roundRect(ctx, x, y, w, h, 16)
  144. ctx.fill()
  145. },
  146. _drawCoverPlaceholder(ctx, x, y, w, h) {
  147. ctx.setFillStyle('#FEF3C7')
  148. this._roundRect(ctx, x, y, w, h, 8)
  149. ctx.fill()
  150. ctx.setFillStyle('#F97316')
  151. ctx.setFontSize(16)
  152. ctx.setTextAlign('center')
  153. ctx.setTextBaseline('middle')
  154. ctx.fillText('暂无封面图', x + w / 2, y + h / 2)
  155. },
  156. _drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors) {
  157. var self = this
  158. var labelY = coverY + coverH + 16
  159. ctx.setFillStyle(colors.main)
  160. ctx.setFontSize(13)
  161. ctx.setTextAlign('left')
  162. ctx.setTextBaseline('top')
  163. ctx.fillText(this.typeLabel, cardX + 20, labelY)
  164. // 标题(2行截断)
  165. var titleY = labelY + 22
  166. ctx.setFillStyle('#1C1917')
  167. ctx.setFontSize(18)
  168. ctx.setTextAlign('left')
  169. ctx.setTextBaseline('top')
  170. this._drawWrappedText(ctx, this.title || '精彩内容', cardX + 20, titleY, cardW - 40, 24, 2)
  171. // 价格行(商品用;空则跳过)
  172. var priceY = titleY + 56
  173. if (this.priceText) {
  174. ctx.setFillStyle(colors.main)
  175. ctx.setFontSize(17)
  176. ctx.setTextAlign('left')
  177. ctx.setTextBaseline('top')
  178. ctx.fillText(this.priceText, cardX + 20, priceY)
  179. }
  180. // 简介(1-2行灰字;空则跳过)
  181. var summaryY = (this.priceText ? priceY + 34 : priceY)
  182. if (this.summary) {
  183. ctx.setFillStyle('#78716C')
  184. ctx.setFontSize(13)
  185. ctx.setTextAlign('left')
  186. ctx.setTextBaseline('top')
  187. this._drawWrappedText(ctx, this.summary, cardX + 20, summaryY, cardW - 40, 20, 2)
  188. }
  189. // 邀请语(二维码上方)
  190. var inviteY = (this.summary ? summaryY + 48 : summaryY)
  191. ctx.setFillStyle('#A8A29E')
  192. ctx.setFontSize(13)
  193. ctx.setTextAlign('center')
  194. ctx.setTextBaseline('top')
  195. ctx.fillText(this.inviteText || '长按识别二维码 · 查看详情', w / 2, inviteY)
  196. // 二维码
  197. var qrSize = 150
  198. var qrX = (w - qrSize) / 2
  199. var qrY = inviteY + 24
  200. if (this.qrCodeBase64) {
  201. var base64Data = this.qrCodeBase64
  202. if (base64Data.indexOf('data:image') === 0) {
  203. var commaIdx = base64Data.indexOf(',')
  204. if (commaIdx > -1) {
  205. base64Data = base64Data.substring(commaIdx + 1)
  206. }
  207. }
  208. var fs = uni.getFileSystemManager()
  209. var filePath = wx.env.USER_DATA_PATH + '/content_share_qr_tmp.png'
  210. fs.writeFile({
  211. filePath: filePath,
  212. data: base64Data,
  213. encoding: 'base64',
  214. success: function() {
  215. ctx.setFillStyle('#ffffff')
  216. self._roundRect(ctx, qrX - 10, qrY - 10, qrSize + 20, qrSize + 20, 8)
  217. ctx.fill()
  218. ctx.setStrokeStyle('#E7E5E4')
  219. ctx.setLineWidth(0.5)
  220. self._roundRect(ctx, qrX - 10, qrY - 10, qrSize + 20, qrSize + 20, 8)
  221. ctx.stroke()
  222. ctx.drawImage(filePath, qrX, qrY, qrSize, qrSize)
  223. self._drawBottomDecoration(ctx, w, h, qrY, qrSize, colors)
  224. },
  225. fail: function() {
  226. self._drawQrPlaceholder(ctx, qrX, qrY, qrSize)
  227. self._drawBottomDecoration(ctx, w, h, qrY, qrSize, colors)
  228. }
  229. })
  230. } else {
  231. this._drawQrPlaceholder(ctx, qrX, qrY, qrSize)
  232. this._drawBottomDecoration(ctx, w, h, qrY, qrSize, colors)
  233. }
  234. },
  235. _drawWrappedText(ctx, text, x, y, maxWidth, lineHeight, maxLines) {
  236. var chars = (text || '').split('')
  237. var line = ''
  238. var lineCount = 0
  239. for (var i = 0; i < chars.length; i++) {
  240. var testLine = line + chars[i]
  241. var testWidth = this._textWidth(ctx, testLine)
  242. if (testWidth > maxWidth && line.length > 0) {
  243. ctx.fillText(line, x, y + lineCount * lineHeight)
  244. line = chars[i]
  245. lineCount++
  246. if (lineCount >= maxLines - 1) {
  247. var remaining = chars.slice(i).join('')
  248. var remainingLine = line + remaining
  249. while (this._textWidth(ctx, remainingLine + '..') > maxWidth && remainingLine.length > 2) {
  250. remainingLine = remainingLine.substring(0, remainingLine.length - 1)
  251. }
  252. ctx.fillText(remainingLine + '..', x, y + lineCount * lineHeight)
  253. return
  254. }
  255. } else {
  256. line = testLine
  257. }
  258. }
  259. ctx.fillText(line, x, y + lineCount * lineHeight)
  260. },
  261. _textWidth(ctx, text) {
  262. if (ctx.measureText) {
  263. try { return ctx.measureText(text).width } catch (e) { return (text || '').length * 18 }
  264. }
  265. return (text || '').length * 18
  266. },
  267. _drawQrPlaceholder(ctx, x, y, size) {
  268. ctx.setFillStyle('#ffffff')
  269. this._roundRect(ctx, x - 10, y - 10, size + 20, size + 20, 8)
  270. ctx.fill()
  271. ctx.setStrokeStyle('#E7E5E4')
  272. ctx.setLineWidth(0.5)
  273. this._roundRect(ctx, x - 10, y - 10, size + 20, size + 20, 8)
  274. ctx.stroke()
  275. ctx.setFillStyle('#A8A29E')
  276. ctx.setFontSize(16)
  277. ctx.setTextAlign('center')
  278. ctx.setTextBaseline('middle')
  279. ctx.fillText('二维码加载中', x + size / 2, y + size / 2)
  280. },
  281. _drawBottomDecoration(ctx, w, h, qrY, qrSize, colors) {
  282. var centerX = w / 2
  283. var bottomY = qrY + qrSize + 16
  284. var diamondY = bottomY + 6
  285. ctx.setFillStyle('rgba(0,0,0,0.06)')
  286. ctx.beginPath()
  287. ctx.moveTo(centerX - 76, diamondY)
  288. ctx.lineTo(centerX - 72, diamondY - 4)
  289. ctx.lineTo(centerX - 68, diamondY)
  290. ctx.lineTo(centerX - 72, diamondY + 4)
  291. ctx.closePath()
  292. ctx.fill()
  293. ctx.beginPath()
  294. ctx.moveTo(centerX + 68, diamondY)
  295. ctx.lineTo(centerX + 72, diamondY - 4)
  296. ctx.lineTo(centerX + 76, diamondY)
  297. ctx.lineTo(centerX + 72, diamondY + 4)
  298. ctx.closePath()
  299. ctx.fill()
  300. ctx.setFillStyle(colors.main)
  301. ctx.setFontSize(18)
  302. ctx.setTextAlign('center')
  303. ctx.setTextBaseline('top')
  304. ctx.fillText(this.ctaText || '长按识别二维码 · 查看详情', centerX, bottomY)
  305. var dotColors = ['#FF8C42', '#6366F1', '#F59E0B', '#10B981', '#FF6B9D']
  306. var dotY = h - 22
  307. var dotR = 4
  308. var dotSpacing = 20
  309. var i_b, offsetX_b
  310. for (i_b = 0; i_b < 5; i_b++) {
  311. offsetX_b = (i_b - 2) * dotSpacing
  312. ctx.setFillStyle(dotColors[i_b])
  313. ctx.beginPath()
  314. ctx.arc(centerX + offsetX_b, dotY, dotR, 0, 2 * Math.PI)
  315. ctx.fill()
  316. }
  317. ctx.setFillStyle('rgba(255,255,255,0.45)')
  318. ctx.setFontSize(12)
  319. ctx.setTextAlign('center')
  320. ctx.setTextBaseline('top')
  321. ctx.fillText('浠艾福 · 家庭健康俱乐部', centerX, dotY + 7)
  322. ctx.draw()
  323. },
  324. _roundRect(ctx, x, y, w, h, r) {
  325. ctx.beginPath()
  326. ctx.moveTo(x + r, y)
  327. ctx.arcTo(x + w, y, x + w, y + r, r)
  328. ctx.arcTo(x + w, y + h, x + w - r, y + h, r)
  329. ctx.arcTo(x, y + h, x, y + h - r, r)
  330. ctx.arcTo(x, y, x + r, y, r)
  331. ctx.closePath()
  332. },
  333. savePoster() {
  334. var self = this
  335. uni.canvasToTempFilePath({
  336. canvasId: 'contentPosterCanvas',
  337. destWidth: 500,
  338. destHeight: 800,
  339. success: function(res) {
  340. uni.saveImageToPhotosAlbum({
  341. filePath: res.tempFilePath,
  342. success: function() {
  343. uni.showToast({ title: '已保存到相册', icon: 'success' })
  344. self.$emit('close')
  345. },
  346. fail: function(err) {
  347. if (err && err.errMsg && (err.errMsg.indexOf('deny') > -1 || err.errMsg.indexOf('auth denied') > -1 || err.errMsg.indexOf('authorize') > -1)) {
  348. uni.showModal({
  349. title: '提示',
  350. content: '需要相册权限才能保存图片',
  351. confirmText: '去设置',
  352. success: function(modalRes) {
  353. if (modalRes.confirm) {
  354. uni.openSetting()
  355. }
  356. }
  357. })
  358. } else {
  359. uni.showToast({ title: '保存失败,请重试', icon: 'none' })
  360. }
  361. }
  362. })
  363. },
  364. fail: function() {
  365. uni.showToast({ title: '生成图片失败', icon: 'none' })
  366. }
  367. }, this)
  368. }
  369. }
  370. }
  371. </script>
  372. <style scoped>
  373. .poster-overlay {
  374. position: fixed;
  375. top: 0;
  376. left: 0;
  377. right: 0;
  378. bottom: 0;
  379. background: rgba(0, 0, 0, 0.6);
  380. display: flex;
  381. align-items: center;
  382. justify-content: center;
  383. z-index: 999;
  384. }
  385. .poster-container {
  386. display: flex;
  387. flex-direction: column;
  388. align-items: center;
  389. }
  390. .poster-canvas {
  391. width: 620rpx;
  392. height: 992rpx;
  393. border-radius: 16rpx;
  394. }
  395. .poster-actions {
  396. display: flex;
  397. flex-direction: column;
  398. width: 620rpx;
  399. margin-top: 30rpx;
  400. }
  401. .poster-btn {
  402. height: 80rpx;
  403. line-height: 80rpx;
  404. font-size: 30rpx;
  405. border-radius: 40rpx;
  406. text-align: center;
  407. border: none;
  408. }
  409. .poster-btn::after { border: none; }
  410. .poster-btn.save {
  411. background: #ffffff;
  412. color: #F97316;
  413. font-weight: 600;
  414. }
  415. .poster-btn.save:active { opacity: 0.85; }
  416. </style>