ContentSharePoster.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. // 按原比例绘制封面图(contain 居中),避免拉伸变形
  93. uni.getImageInfo({
  94. src: res.tempFilePath,
  95. success: function(info) {
  96. var rect = self._fitCoverRect(info.width, info.height, cardX + 20, coverY, coverW, coverH)
  97. ctx.drawImage(res.tempFilePath, rect.x, rect.y, rect.w, rect.h)
  98. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors)
  99. },
  100. fail: function() {
  101. ctx.drawImage(res.tempFilePath, cardX + 20, coverY, coverW, coverH)
  102. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors)
  103. }
  104. })
  105. } else {
  106. self._drawCoverPlaceholder(ctx, cardX + 20, coverY, coverW, coverH)
  107. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors)
  108. }
  109. },
  110. fail: function() {
  111. self._drawCoverPlaceholder(ctx, cardX + 20, coverY, coverW, coverH)
  112. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors)
  113. }
  114. })
  115. } else {
  116. this._drawCoverPlaceholder(ctx, cardX + 20, coverY, coverW, coverH)
  117. this._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors)
  118. }
  119. },
  120. _fitCoverRect(imgW, imgH, boxX, boxY, boxW, boxH) {
  121. if (!imgW || !imgH) {
  122. return { x: boxX, y: boxY, w: boxW, h: boxH }
  123. }
  124. var scale = Math.min(boxW / imgW, boxH / imgH)
  125. var w = imgW * scale
  126. var h = imgH * scale
  127. return {
  128. x: boxX + (boxW - w) / 2,
  129. y: boxY + (boxH - h) / 2,
  130. w: w,
  131. h: h
  132. }
  133. },
  134. _drawTopDecoration(ctx, w, colors) {
  135. var centerX = w / 2
  136. var dotColors = ['#FF8C42', '#6366F1', '#F59E0B', '#10B981', '#FF6B9D']
  137. var dotY = 28
  138. var dotR = 5
  139. var dotSpacing = 26
  140. var i, offsetX, offsetY
  141. for (i = 0; i < 5; i++) {
  142. offsetX = (i - 2) * dotSpacing
  143. offsetY = Math.abs(i - 2) * 3
  144. ctx.setFillStyle(dotColors[i])
  145. ctx.beginPath()
  146. ctx.arc(centerX + offsetX, dotY + offsetY, dotR, 0, 2 * Math.PI)
  147. ctx.fill()
  148. }
  149. ctx.setFillStyle('#ffffff')
  150. ctx.setFontSize(20)
  151. ctx.setTextAlign('center')
  152. ctx.setTextBaseline('top')
  153. ctx.fillText('浠艾福', centerX, 52)
  154. ctx.setFontSize(14)
  155. ctx.setFillStyle('rgba(255,255,255,0.8)')
  156. ctx.fillText('给全家的一站式幸福提案', centerX, 76)
  157. ctx.setStrokeStyle('rgba(255,255,255,0.25)')
  158. ctx.setLineWidth(1)
  159. ctx.beginPath()
  160. ctx.moveTo(80, 100)
  161. ctx.lineTo(w - 80, 100)
  162. ctx.stroke()
  163. },
  164. _drawContentCard(ctx, x, y, w, h, colors) {
  165. ctx.setFillStyle('rgba(0,0,0,0.04)')
  166. this._roundRect(ctx, x + 2, y + 3, w, h, 16)
  167. ctx.fill()
  168. ctx.setFillStyle(colors.light)
  169. this._roundRect(ctx, x, y, w, h, 16)
  170. ctx.fill()
  171. },
  172. _drawCoverPlaceholder(ctx, x, y, w, h) {
  173. ctx.setFillStyle('#FEF3C7')
  174. this._roundRect(ctx, x, y, w, h, 8)
  175. ctx.fill()
  176. ctx.setFillStyle('#F97316')
  177. ctx.setFontSize(16)
  178. ctx.setTextAlign('center')
  179. ctx.setTextBaseline('middle')
  180. ctx.fillText('暂无封面图', x + w / 2, y + h / 2)
  181. },
  182. _drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors) {
  183. var self = this
  184. var labelY = coverY + coverH + 16
  185. ctx.setFillStyle(colors.main)
  186. ctx.setFontSize(13)
  187. ctx.setTextAlign('left')
  188. ctx.setTextBaseline('top')
  189. ctx.fillText(this.typeLabel, cardX + 20, labelY)
  190. // 标题(2行截断)
  191. var titleY = labelY + 22
  192. ctx.setFillStyle('#1C1917')
  193. ctx.setFontSize(18)
  194. ctx.setTextAlign('left')
  195. ctx.setTextBaseline('top')
  196. this._drawWrappedText(ctx, this.title || '精彩内容', cardX + 20, titleY, cardW - 40, 24, 2)
  197. // 价格行(商品用;空则跳过)
  198. var priceY = titleY + 56
  199. if (this.priceText) {
  200. ctx.setFillStyle(colors.main)
  201. ctx.setFontSize(17)
  202. ctx.setTextAlign('left')
  203. ctx.setTextBaseline('top')
  204. ctx.fillText(this.priceText, cardX + 20, priceY)
  205. }
  206. // 简介(1-2行灰字;空则跳过)
  207. var summaryY = (this.priceText ? priceY + 34 : priceY)
  208. if (this.summary) {
  209. ctx.setFillStyle('#78716C')
  210. ctx.setFontSize(13)
  211. ctx.setTextAlign('left')
  212. ctx.setTextBaseline('top')
  213. this._drawWrappedText(ctx, this.summary, cardX + 20, summaryY, cardW - 40, 20, 2)
  214. }
  215. // 邀请语(二维码上方)
  216. var inviteY = (this.summary ? summaryY + 48 : summaryY)
  217. ctx.setFillStyle('#A8A29E')
  218. ctx.setFontSize(13)
  219. ctx.setTextAlign('center')
  220. ctx.setTextBaseline('top')
  221. ctx.fillText(this.inviteText || '长按识别二维码 · 查看详情', w / 2, inviteY)
  222. // 二维码
  223. var qrSize = 150
  224. var qrX = (w - qrSize) / 2
  225. var qrY = inviteY + 24
  226. if (this.qrCodeBase64) {
  227. var base64Data = this.qrCodeBase64
  228. if (base64Data.indexOf('data:image') === 0) {
  229. var commaIdx = base64Data.indexOf(',')
  230. if (commaIdx > -1) {
  231. base64Data = base64Data.substring(commaIdx + 1)
  232. }
  233. }
  234. var fs = uni.getFileSystemManager()
  235. var filePath = wx.env.USER_DATA_PATH + '/content_share_qr_tmp.png'
  236. fs.writeFile({
  237. filePath: filePath,
  238. data: base64Data,
  239. encoding: 'base64',
  240. success: function() {
  241. ctx.setFillStyle('#ffffff')
  242. self._roundRect(ctx, qrX - 10, qrY - 10, qrSize + 20, qrSize + 20, 8)
  243. ctx.fill()
  244. ctx.setStrokeStyle('#E7E5E4')
  245. ctx.setLineWidth(0.5)
  246. self._roundRect(ctx, qrX - 10, qrY - 10, qrSize + 20, qrSize + 20, 8)
  247. ctx.stroke()
  248. ctx.drawImage(filePath, qrX, qrY, qrSize, qrSize)
  249. self._drawBottomDecoration(ctx, w, h, qrY, qrSize, colors)
  250. },
  251. fail: function() {
  252. self._drawQrPlaceholder(ctx, qrX, qrY, qrSize)
  253. self._drawBottomDecoration(ctx, w, h, qrY, qrSize, colors)
  254. }
  255. })
  256. } else {
  257. this._drawQrPlaceholder(ctx, qrX, qrY, qrSize)
  258. this._drawBottomDecoration(ctx, w, h, qrY, qrSize, colors)
  259. }
  260. },
  261. _drawWrappedText(ctx, text, x, y, maxWidth, lineHeight, maxLines) {
  262. var chars = (text || '').split('')
  263. var line = ''
  264. var lineCount = 0
  265. for (var i = 0; i < chars.length; i++) {
  266. var testLine = line + chars[i]
  267. var testWidth = this._textWidth(ctx, testLine)
  268. if (testWidth > maxWidth && line.length > 0) {
  269. ctx.fillText(line, x, y + lineCount * lineHeight)
  270. line = chars[i]
  271. lineCount++
  272. if (lineCount >= maxLines - 1) {
  273. var remaining = chars.slice(i).join('')
  274. var remainingLine = line + remaining
  275. while (this._textWidth(ctx, remainingLine + '..') > maxWidth && remainingLine.length > 2) {
  276. remainingLine = remainingLine.substring(0, remainingLine.length - 1)
  277. }
  278. ctx.fillText(remainingLine + '..', x, y + lineCount * lineHeight)
  279. return
  280. }
  281. } else {
  282. line = testLine
  283. }
  284. }
  285. ctx.fillText(line, x, y + lineCount * lineHeight)
  286. },
  287. _textWidth(ctx, text) {
  288. if (ctx.measureText) {
  289. try { return ctx.measureText(text).width } catch (e) { return (text || '').length * 18 }
  290. }
  291. return (text || '').length * 18
  292. },
  293. _drawQrPlaceholder(ctx, x, y, size) {
  294. ctx.setFillStyle('#ffffff')
  295. this._roundRect(ctx, x - 10, y - 10, size + 20, size + 20, 8)
  296. ctx.fill()
  297. ctx.setStrokeStyle('#E7E5E4')
  298. ctx.setLineWidth(0.5)
  299. this._roundRect(ctx, x - 10, y - 10, size + 20, size + 20, 8)
  300. ctx.stroke()
  301. ctx.setFillStyle('#A8A29E')
  302. ctx.setFontSize(16)
  303. ctx.setTextAlign('center')
  304. ctx.setTextBaseline('middle')
  305. ctx.fillText('二维码加载中', x + size / 2, y + size / 2)
  306. },
  307. _drawBottomDecoration(ctx, w, h, qrY, qrSize, colors) {
  308. var centerX = w / 2
  309. var bottomY = qrY + qrSize + 16
  310. var diamondY = bottomY + 6
  311. ctx.setFillStyle('rgba(0,0,0,0.06)')
  312. ctx.beginPath()
  313. ctx.moveTo(centerX - 76, diamondY)
  314. ctx.lineTo(centerX - 72, diamondY - 4)
  315. ctx.lineTo(centerX - 68, diamondY)
  316. ctx.lineTo(centerX - 72, diamondY + 4)
  317. ctx.closePath()
  318. ctx.fill()
  319. ctx.beginPath()
  320. ctx.moveTo(centerX + 68, diamondY)
  321. ctx.lineTo(centerX + 72, diamondY - 4)
  322. ctx.lineTo(centerX + 76, diamondY)
  323. ctx.lineTo(centerX + 72, diamondY + 4)
  324. ctx.closePath()
  325. ctx.fill()
  326. ctx.setFillStyle(colors.main)
  327. ctx.setFontSize(18)
  328. ctx.setTextAlign('center')
  329. ctx.setTextBaseline('top')
  330. ctx.fillText(this.ctaText || '长按识别二维码 · 查看详情', centerX, bottomY)
  331. var dotColors = ['#FF8C42', '#6366F1', '#F59E0B', '#10B981', '#FF6B9D']
  332. var dotY = h - 22
  333. var dotR = 4
  334. var dotSpacing = 20
  335. var i_b, offsetX_b
  336. for (i_b = 0; i_b < 5; i_b++) {
  337. offsetX_b = (i_b - 2) * dotSpacing
  338. ctx.setFillStyle(dotColors[i_b])
  339. ctx.beginPath()
  340. ctx.arc(centerX + offsetX_b, dotY, dotR, 0, 2 * Math.PI)
  341. ctx.fill()
  342. }
  343. ctx.setFillStyle('rgba(255,255,255,0.45)')
  344. ctx.setFontSize(12)
  345. ctx.setTextAlign('center')
  346. ctx.setTextBaseline('top')
  347. ctx.fillText('浠艾福 · 家庭健康俱乐部', centerX, dotY + 7)
  348. ctx.draw()
  349. },
  350. _roundRect(ctx, x, y, w, h, r) {
  351. ctx.beginPath()
  352. ctx.moveTo(x + r, y)
  353. ctx.arcTo(x + w, y, x + w, y + r, r)
  354. ctx.arcTo(x + w, y + h, x + w - r, y + h, r)
  355. ctx.arcTo(x, y + h, x, y + h - r, r)
  356. ctx.arcTo(x, y, x + r, y, r)
  357. ctx.closePath()
  358. },
  359. savePoster() {
  360. var self = this
  361. uni.canvasToTempFilePath({
  362. canvasId: 'contentPosterCanvas',
  363. destWidth: 500,
  364. destHeight: 800,
  365. success: function(res) {
  366. uni.saveImageToPhotosAlbum({
  367. filePath: res.tempFilePath,
  368. success: function() {
  369. uni.showToast({ title: '已保存到相册', icon: 'success' })
  370. self.$emit('close')
  371. },
  372. fail: function(err) {
  373. if (err && err.errMsg && (err.errMsg.indexOf('deny') > -1 || err.errMsg.indexOf('auth denied') > -1 || err.errMsg.indexOf('authorize') > -1)) {
  374. uni.showModal({
  375. title: '提示',
  376. content: '需要相册权限才能保存图片',
  377. confirmText: '去设置',
  378. success: function(modalRes) {
  379. if (modalRes.confirm) {
  380. uni.openSetting()
  381. }
  382. }
  383. })
  384. } else {
  385. uni.showToast({ title: '保存失败,请重试', icon: 'none' })
  386. }
  387. }
  388. })
  389. },
  390. fail: function() {
  391. uni.showToast({ title: '生成图片失败', icon: 'none' })
  392. }
  393. }, this)
  394. }
  395. }
  396. }
  397. </script>
  398. <style scoped>
  399. .poster-overlay {
  400. position: fixed;
  401. top: 0;
  402. left: 0;
  403. right: 0;
  404. bottom: 0;
  405. background: rgba(0, 0, 0, 0.6);
  406. display: flex;
  407. align-items: center;
  408. justify-content: center;
  409. z-index: 999;
  410. }
  411. .poster-container {
  412. display: flex;
  413. flex-direction: column;
  414. align-items: center;
  415. }
  416. .poster-canvas {
  417. width: 620rpx;
  418. height: 992rpx;
  419. border-radius: 16rpx;
  420. }
  421. .poster-actions {
  422. display: flex;
  423. flex-direction: column;
  424. width: 620rpx;
  425. margin-top: 30rpx;
  426. }
  427. .poster-btn {
  428. height: 80rpx;
  429. line-height: 80rpx;
  430. font-size: 30rpx;
  431. border-radius: 40rpx;
  432. text-align: center;
  433. border: none;
  434. }
  435. .poster-btn::after { border: none; }
  436. .poster-btn.save {
  437. background: #ffffff;
  438. color: #F97316;
  439. font-weight: 600;
  440. }
  441. .poster-btn.save:active { opacity: 0.85; }
  442. </style>