ContentSharePoster.vue 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. <!-- 模板选择(管理端定义的模板;未配置任何模板时不显示选择行) -->
  6. <view class="poster-tpl-row" v-if="serverTemplates.length > 0">
  7. <view class="tpl-label">海报模板</view>
  8. <view class="tpl-options">
  9. <view
  10. v-for="tpl in serverTemplates"
  11. :key="getTplKey(tpl)"
  12. class="tpl-option"
  13. :class="{ 'tpl-active': activeTemplateId === tpl.id }"
  14. @click="switchTemplate(tpl)"
  15. >
  16. <view class="tpl-dot" :style="{ background: tpl.mainColor }"></view>
  17. <text class="tpl-text">{{ tpl.name }}</text>
  18. </view>
  19. </view>
  20. </view>
  21. <!-- 背景颜色选择(渐变模板可用) -->
  22. <view class="poster-bg-row" v-if="template === 'gradient'">
  23. <view class="tpl-label">背景色</view>
  24. <view class="bg-options">
  25. <view
  26. v-for="(c, i) in bgOptions"
  27. :key="getBgKey(i)"
  28. class="bg-option"
  29. :class="{ 'bg-active': bgColor === c }"
  30. :style="{ background: c }"
  31. @click="switchBgColor(c)"
  32. ></view>
  33. </view>
  34. </view>
  35. <view class="poster-actions">
  36. <button class="poster-btn save" @click="savePoster">保存到相册</button>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import { body, primary, primaryPale, success, white, wisdom, BrandColors } from '@/utils/brand-colors.js'
  43. import { getFontScale } from '@/utils/fontScale.js'
  44. export default {
  45. name: 'ContentSharePoster',
  46. props: {
  47. show: { type: Boolean, default: false },
  48. title: { type: String, default: '' },
  49. coverImage: { type: String, default: '' },
  50. qrCodeBase64: { type: String, default: '' },
  51. typeLabel: { type: String, default: '好物推荐' },
  52. dimensionCode: { type: String, default: '' },
  53. summary: { type: String, default: '' },
  54. priceText: { type: String, default: '' },
  55. ctaText: { type: String, default: '长按识别二维码 · 查看详情' },
  56. inviteText: { type: String, default: '' },
  57. // 海报模板: gradient(default) | minimal | card
  58. template: { type: String, default: 'gradient' },
  59. // 自定义背景色(覆盖模板默认背景)
  60. bgColor: { type: String, default: '' },
  61. // 二维码尺寸覆盖(默认150rpx,建议活动类可用 100 缩小)
  62. qrSizeOverride: { type: Number, default: 150 },
  63. // 五维能量数据卡:{ label?: String, overall?: Number, dims: [{ key, label, score, color }] }
  64. // 传 null 时不画数据卡,按 coverImage -> brandFallback -> 占位图 顺序降级(原有行为不变)
  65. energyData: { type: Object, default: null },
  66. // 无 energyData 且 coverImage 为空时,是否用品牌渐变卡替代"暂无封面图"占位
  67. brandFallback: { type: Boolean, default: false },
  68. // Web 管理端定义的模板列表([{id, name, templateKey, mainColor, darkColor, lightColor, bgColors, qrSize}])
  69. // 传空数组时使用内置三套模板(原有行为不变)
  70. templates: { type: Array, default: () => [] }
  71. },
  72. watch: {
  73. show(val) {
  74. if (val) {
  75. this.$nextTick(function() {
  76. setTimeout(this.drawPoster.bind(this), 300)
  77. }.bind(this))
  78. }
  79. },
  80. // 模板列表异步到达:无选中或选中项已失效时默认选第一个,并通过 emit 同步父页面
  81. templates: {
  82. immediate: true,
  83. handler: function(list) {
  84. if (this.serverTemplates.length === 0) return
  85. if (!this.getActiveTemplate()) {
  86. this.applyTemplate(this.serverTemplates[0])
  87. }
  88. }
  89. }
  90. },
  91. computed: {
  92. serverTemplates: function() {
  93. var list = this.templates || []
  94. var out = []
  95. for (var i = 0; i < list.length; i++) {
  96. if (list[i] && list[i].id) out.push(list[i])
  97. }
  98. return out
  99. },
  100. // 当前选中的管理端模板(无则为 null)
  101. activeTemplate: function() {
  102. if (!this.activeTemplateId) return null
  103. var list = this.serverTemplates
  104. for (var i = 0; i < list.length; i++) {
  105. if (String(list[i].id) === String(this.activeTemplateId)) return list[i]
  106. }
  107. return null
  108. },
  109. // 背景色板:优先用管理端模板的配置,其次用内置七色
  110. bgOptions: function() {
  111. var tpl = this.activeTemplate
  112. if (tpl && tpl.bgColors) {
  113. try {
  114. var arr = JSON.parse(tpl.bgColors)
  115. if (arr && arr.length > 0) return arr
  116. } catch (e) {}
  117. }
  118. return this.bgColors
  119. },
  120. // 二维码尺寸:管理端模板配置优先,其次 qrSizeOverride prop
  121. effectiveQrSize: function() {
  122. var tpl = this.activeTemplate
  123. if (tpl && tpl.qrSize && tpl.qrSize > 0) return tpl.qrSize
  124. return this.qrSizeOverride || 150
  125. }
  126. },
  127. data() {
  128. return {
  129. // 选中的管理端模板 id(未使用管理端模板时为 null)
  130. activeTemplateId: null,
  131. // 可选的背景色(渐变模板下展示)
  132. bgColors: [BrandColors.body, BrandColors.wisdom, BrandColors.wealth, BrandColors.success, BrandColors.mind, BrandColors.slate800, BrandColors.white]
  133. }
  134. },
  135. methods: {
  136. getTplKey(tpl) {
  137. return 'tpl_' + tpl.id
  138. },
  139. getBgKey(i) {
  140. return 'bg_' + i
  141. },
  142. // 海报画布文字按用户字号系数放大(画布有固定排版,放大区间比页面更保守,防止溢出裁切)
  143. _fs(size) {
  144. var scale = getFontScale()
  145. if (!scale || scale < 0.9) scale = 0.9
  146. if (scale > 1.25) scale = 1.25
  147. return Math.round(size * scale)
  148. },
  149. // 选中的管理端模板(返回对象或 null)
  150. getActiveTemplate: function() {
  151. if (!this.activeTemplateId) return null
  152. var list = this.serverTemplates
  153. for (var i = 0; i < list.length; i++) {
  154. if (String(list[i].id) === String(this.activeTemplateId)) return list[i]
  155. }
  156. return null
  157. },
  158. // 应用管理端模板:选中 + 同步模板类型/背景色到父页面(背景色取模板色板第一个)
  159. applyTemplate: function(tpl) {
  160. if (!tpl) return
  161. this.activeTemplateId = tpl.id
  162. this.$emit('update:template', tpl.templateKey)
  163. var bg = tpl.mainColor
  164. if (tpl.bgColors) {
  165. try {
  166. var arr = JSON.parse(tpl.bgColors)
  167. if (arr && arr.length > 0) bg = arr[0]
  168. } catch (e) {}
  169. }
  170. this.$emit('update:bgColor', bg)
  171. var self = this
  172. this.$nextTick(function() {
  173. setTimeout(self.drawPoster.bind(self), 100)
  174. })
  175. },
  176. // 海报模板配色(管理端模板配色优先,其次按维度取内置配色)
  177. getColors: function() {
  178. var tpl = this.getActiveTemplate()
  179. if (tpl) {
  180. return {
  181. main: tpl.mainColor || BrandColors.primary,
  182. dark: tpl.darkColor || BrandColors.orangeDeep,
  183. light: tpl.lightColor || BrandColors.primaryPale
  184. }
  185. }
  186. var map = {
  187. body: { main: BrandColors.body, dark: BrandColors.orangeDeep, light: BrandColors.primaryPale },
  188. mind: { main: BrandColors.mind, dark: BrandColors.pink600, light: BrandColors.pink50 },
  189. wisdom: { main: BrandColors.wisdom, dark: BrandColors.indigo600, light: BrandColors.indigo50 },
  190. action: { main: BrandColors.success, dark: '#059669', light: BrandColors.emerald50 },
  191. wealth: { main: BrandColors.wealth, dark: '#D97706', light: BrandColors.amber50 }
  192. }
  193. return map[this.dimensionCode] || { main: BrandColors.primary, dark: BrandColors.orangeDeep, light: BrandColors.primaryPale }
  194. },
  195. // 海报模板配色
  196. getTemplateConfig() {
  197. var colors = this.getColors()
  198. var template = this.template || 'gradient'
  199. var bgColor = this.bgColor || ''
  200. // 梯度背景模板
  201. if (template === 'gradient') {
  202. return { template: 'gradient', bgColor: bgColor || colors.main, main: colors.main, dark: colors.dark, light: colors.light }
  203. }
  204. // 极简模板:深色背景+白色卡片(管理端模板的 darkColor 作为默认底色)
  205. if (template === 'minimal') {
  206. var tpl = this.getActiveTemplate()
  207. var dark = tpl ? (bgColor || colors.dark) : (bgColor || BrandColors.slate800)
  208. return { template: 'minimal', bgColor: dark, main: colors.main, dark: colors.dark, light: colors.light }
  209. }
  210. // 卡片模板:浅色背景+彩色顶栏
  211. return { template: 'card', bgColor: bgColor || BrandColors.white, main: colors.main, dark: colors.dark, light: colors.light }
  212. },
  213. // 获取当前模板的背景样式
  214. getBgStyle() {
  215. var cfg = this.getTemplateConfig()
  216. if (cfg.template === 'gradient') {
  217. var dark = cfg.dark
  218. return 'linear-gradient(135deg, ' + cfg.main + ', ' + dark + ')'
  219. }
  220. if (cfg.template === 'minimal') {
  221. return 'linear-gradient(135deg, ' + cfg.bgColor + ', ' + cfg.dark + ')'
  222. }
  223. return 'linear-gradient(135deg, ' + cfg.main + ', ' + (cfg.bgColor || BrandColors.white) + ')'
  224. },
  225. drawPoster() {
  226. var self = this
  227. uni.createSelectorQuery().in(this)
  228. .select('#contentPosterCanvas')
  229. .boundingClientRect(function(rect) {
  230. var scaleX = 1
  231. var scaleY = 1
  232. if (rect && rect.width && rect.height) {
  233. scaleX = rect.width / 500
  234. scaleY = rect.height / 800
  235. }
  236. self._renderPoster(scaleX, scaleY)
  237. })
  238. .exec()
  239. },
  240. _renderPoster(scaleX, scaleY) {
  241. var ctx = uni.createCanvasContext('contentPosterCanvas', this)
  242. var w = 500
  243. var h = 800
  244. var cfg = this.getTemplateConfig()
  245. // 画布 CSS 用 rpx,实际像素随设备变化(375px 屏约 310x496),而绘制按 500x800 设计坐标;
  246. // 不缩放坐标系时右侧/底部内容(含二维码 y≈552-702)会被裁剪出画布,导致海报显示不全。
  247. if (scaleX !== 1 || scaleY !== 1) {
  248. ctx.scale(scaleX, scaleY)
  249. }
  250. // 【模板1 · gradient】渐变背景
  251. if (cfg.template === 'gradient') {
  252. var gradient = ctx.createLinearGradient(0, 0, w * 0.3, h)
  253. gradient.addColorStop(0, cfg.main)
  254. gradient.addColorStop(0.55, cfg.dark)
  255. gradient.addColorStop(1, cfg.dark)
  256. ctx.setFillStyle(gradient)
  257. ctx.fillRect(0, 0, w, h)
  258. }
  259. // 【模板2 · minimal】纯色深色背景
  260. else if (cfg.template === 'minimal') {
  261. ctx.setFillStyle(cfg.bgColor)
  262. ctx.fillRect(0, 0, w, h)
  263. }
  264. // 【模板3 · card】白色背景
  265. else {
  266. ctx.setFillStyle(BrandColors.white)
  267. ctx.fillRect(0, 0, w, h)
  268. }
  269. this._drawTopDecoration(ctx, w, cfg)
  270. var cardX = 28
  271. var cardY = 112
  272. var cardW = 444
  273. var cardH = 600
  274. this._drawContentCard(ctx, cardX, cardY, cardW, cardH, cfg)
  275. var coverY = cardY + 20
  276. var coverW = cardW - 40
  277. var coverH = 220
  278. var self = this
  279. if (this.energyData) {
  280. // 方案C:五维能量数据卡,canvas 直接绘制(文字锐利、数据真实、无网络开销)
  281. this._drawEnergyCard(ctx, cardX + 20, coverY, coverW, coverH, cfg)
  282. this._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, cfg)
  283. } else if (this.coverImage) {
  284. uni.downloadFile({
  285. url: this.coverImage,
  286. success: function(res) {
  287. if (res.statusCode === 200) {
  288. // 按原比例绘制封面图(contain 居中),避免拉伸变形
  289. uni.getImageInfo({
  290. src: res.tempFilePath,
  291. success: function(info) {
  292. var rect = self._fitCoverRect(info.width, info.height, cardX + 20, coverY, coverW, coverH)
  293. ctx.drawImage(res.tempFilePath, rect.x, rect.y, rect.w, rect.h)
  294. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, cfg)
  295. },
  296. fail: function() {
  297. ctx.drawImage(res.tempFilePath, cardX + 20, coverY, coverW, coverH)
  298. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, cfg)
  299. }
  300. })
  301. } else {
  302. self._drawCoverPlaceholder(ctx, cardX + 20, coverY, coverW, coverH)
  303. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, cfg)
  304. }
  305. },
  306. fail: function() {
  307. self._drawCoverPlaceholder(ctx, cardX + 20, coverY, coverW, coverH)
  308. self._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, cfg)
  309. }
  310. })
  311. } else if (this.brandFallback) {
  312. this._drawBrandCard(ctx, cardX + 20, coverY, coverW, coverH, cfg)
  313. this._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, cfg)
  314. } else {
  315. this._drawCoverPlaceholder(ctx, cardX + 20, coverY, coverW, coverH)
  316. this._drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, cfg)
  317. }
  318. },
  319. _fitCoverRect(imgW, imgH, boxX, boxY, boxW, boxH) {
  320. if (!imgW || !imgH) {
  321. return { x: boxX, y: boxY, w: boxW, h: boxH }
  322. }
  323. var scale = Math.min(boxW / imgW, boxH / imgH)
  324. var w = imgW * scale
  325. var h = imgH * scale
  326. return {
  327. x: boxX + (boxW - w) / 2,
  328. y: boxY + (boxH - h) / 2,
  329. w: w,
  330. h: h
  331. }
  332. },
  333. _drawTopDecoration(ctx, w, colors) {
  334. var centerX = w / 2
  335. var dotColors = [BrandColors.body, BrandColors.wisdom, BrandColors.wealth, BrandColors.success, BrandColors.mind]
  336. var dotY = 28
  337. var dotR = 5
  338. var dotSpacing = 26
  339. var i, offsetX, offsetY
  340. for (i = 0; i < 5; i++) {
  341. offsetX = (i - 2) * dotSpacing
  342. offsetY = Math.abs(i - 2) * 3
  343. ctx.setFillStyle(dotColors[i])
  344. ctx.beginPath()
  345. ctx.arc(centerX + offsetX, dotY + offsetY, dotR, 0, 2 * Math.PI)
  346. ctx.fill()
  347. }
  348. ctx.setFillStyle(BrandColors.white)
  349. ctx.setFontSize(this._fs(20))
  350. ctx.setTextAlign('center')
  351. ctx.setTextBaseline('top')
  352. ctx.fillText('浠艾福', centerX, 52)
  353. ctx.setFontSize(this._fs(14))
  354. ctx.setFillStyle('rgba(255,255,255,0.8)')
  355. ctx.fillText('给全家的一站式幸福提案', centerX, 76)
  356. ctx.setStrokeStyle('rgba(255,255,255,0.25)')
  357. ctx.setLineWidth(1)
  358. ctx.beginPath()
  359. ctx.moveTo(80, 100)
  360. ctx.lineTo(w - 80, 100)
  361. ctx.stroke()
  362. },
  363. _drawContentCard(ctx, x, y, w, h, colors) {
  364. ctx.setFillStyle('rgba(0,0,0,0.04)')
  365. this._roundRect(ctx, x + 2, y + 3, w, h, 16)
  366. ctx.fill()
  367. ctx.setFillStyle(colors.light)
  368. this._roundRect(ctx, x, y, w, h, 16)
  369. ctx.fill()
  370. },
  371. _drawCoverPlaceholder(ctx, x, y, w, h) {
  372. ctx.setFillStyle('#FEF3C7')
  373. this._roundRect(ctx, x, y, w, h, 8)
  374. ctx.fill()
  375. ctx.setFillStyle(BrandColors.primary)
  376. ctx.setFontSize(this._fs(16))
  377. ctx.setTextAlign('center')
  378. ctx.setTextBaseline('middle')
  379. ctx.fillText('暂无封面图', x + w / 2, y + h / 2)
  380. },
  381. // 五维能量数据卡(方案C):综合分大字 + 五维彩色圆/分数/进度条
  382. _drawEnergyCard(ctx, x, y, w, h, colors) {
  383. var data = this.energyData || {}
  384. var dims = data.dims || []
  385. ctx.setFillStyle(BrandColors.white)
  386. this._roundRect(ctx, x, y, w, h, 8)
  387. ctx.fill()
  388. // 顶部:左侧标题,右侧综合分
  389. ctx.setFillStyle(BrandColors.stone500)
  390. ctx.setFontSize(this._fs(13))
  391. ctx.setTextAlign('left')
  392. ctx.setTextBaseline('top')
  393. ctx.fillText(data.label || '我们家的能量', x + 16, y + 16)
  394. var hasOverall = data.overall !== null && data.overall !== undefined && !isNaN(data.overall)
  395. if (hasOverall) {
  396. ctx.setFillStyle(colors.main)
  397. ctx.setFontSize(this._fs(30))
  398. ctx.setTextAlign('right')
  399. ctx.fillText(String(data.overall), x + w - 28, y + 8)
  400. ctx.setFillStyle(BrandColors.stone500)
  401. ctx.setFontSize(this._fs(13))
  402. ctx.fillText('分', x + w - 16, y + 28)
  403. }
  404. ctx.setStrokeStyle(BrandColors.stone100)
  405. ctx.setLineWidth(1)
  406. ctx.beginPath()
  407. ctx.moveTo(x + 16, y + 54)
  408. ctx.lineTo(x + w - 16, y + 54)
  409. ctx.stroke()
  410. // 五维柱:彩色圆 + 单字 + 分数 + 进度条
  411. var count = dims.length
  412. for (var i = 0; i < count; i++) {
  413. var d = dims[i] || {}
  414. var cx = x + 16 + ((w - 32) / count) * i + (w - 32) / count / 2
  415. var color = d.color || colors.main
  416. var score = Number(d.score)
  417. if (isNaN(score) || score < 0) score = 0
  418. if (score > 100) score = 100
  419. ctx.setFillStyle(color)
  420. ctx.beginPath()
  421. ctx.arc(cx, y + 94, 17, 0, 2 * Math.PI)
  422. ctx.fill()
  423. ctx.setFillStyle(BrandColors.white)
  424. ctx.setFontSize(this._fs(15))
  425. ctx.setTextAlign('center')
  426. ctx.setTextBaseline('middle')
  427. ctx.fillText(d.label || '', cx, y + 95)
  428. ctx.setFillStyle(BrandColors.stone900)
  429. ctx.setFontSize(this._fs(18))
  430. ctx.setTextBaseline('top')
  431. ctx.fillText(String(score), cx, y + 120)
  432. var barW = 44
  433. var barH = 4
  434. var barX = cx - barW / 2
  435. var barY = y + 148
  436. ctx.setFillStyle(BrandColors.stone100)
  437. this._roundRect(ctx, barX, barY, barW, barH, 2)
  438. ctx.fill()
  439. if (score > 0) {
  440. ctx.setFillStyle(color)
  441. this._roundRect(ctx, barX, barY, barW * score / 100, barH, 2)
  442. ctx.fill()
  443. }
  444. }
  445. ctx.setFillStyle(BrandColors.stone400)
  446. ctx.setFontSize(this._fs(11))
  447. ctx.setTextAlign('center')
  448. ctx.setTextBaseline('top')
  449. ctx.fillText('五维能量 · 浠艾福家庭测评', x + w / 2, y + 172)
  450. },
  451. // 品牌渐变卡:用户未解锁全景(无五维数据)且无封面图时的降级方案
  452. _drawBrandCard(ctx, x, y, w, h, colors) {
  453. var g = ctx.createLinearGradient(x, y, x + w, y + h)
  454. g.addColorStop(0, colors.main)
  455. g.addColorStop(1, colors.dark)
  456. ctx.setFillStyle(g)
  457. this._roundRect(ctx, x, y, w, h, 8)
  458. ctx.fill()
  459. var dotColors = [BrandColors.body, BrandColors.wisdom, BrandColors.wealth, BrandColors.success, BrandColors.mind]
  460. var centerX = x + w / 2
  461. for (var i = 0; i < 5; i++) {
  462. ctx.setFillStyle(dotColors[i])
  463. ctx.beginPath()
  464. ctx.arc(centerX + (i - 2) * 26, y + 66, 5, 0, 2 * Math.PI)
  465. ctx.fill()
  466. }
  467. ctx.setFillStyle(BrandColors.white)
  468. ctx.setFontSize(this._fs(24))
  469. ctx.setTextAlign('center')
  470. ctx.setTextBaseline('top')
  471. ctx.fillText('浠艾福', centerX, y + 86)
  472. ctx.setFillStyle('rgba(255,255,255,0.85)')
  473. ctx.setFontSize(this._fs(13))
  474. ctx.fillText('给全家的一站式幸福提案', centerX, y + 124)
  475. ctx.setFillStyle('rgba(255,255,255,0.7)')
  476. ctx.setFontSize(this._fs(12))
  477. ctx.fillText('测一测你家的五维能量', centerX, y + 152)
  478. },
  479. _drawContentAfterCover(ctx, w, h, cardX, cardW, coverY, coverH, colors) {
  480. var self = this
  481. var labelY = coverY + coverH + 16
  482. ctx.setFillStyle(colors.main)
  483. ctx.setFontSize(this._fs(13))
  484. ctx.setTextAlign('left')
  485. ctx.setTextBaseline('top')
  486. ctx.fillText(this.typeLabel, cardX + 20, labelY)
  487. // 标题(2行截断)
  488. var titleY = labelY + 22
  489. ctx.setFillStyle(BrandColors.stone900)
  490. ctx.setFontSize(this._fs(18))
  491. ctx.setTextAlign('left')
  492. ctx.setTextBaseline('top')
  493. this._drawWrappedText(ctx, this.title || '精彩内容', cardX + 20, titleY, cardW - 40, this._fs(24), 2)
  494. // 价格行(商品用;空则跳过)
  495. var priceY = titleY + 56
  496. if (this.priceText) {
  497. ctx.setFillStyle(colors.main)
  498. ctx.setFontSize(this._fs(17))
  499. ctx.setTextAlign('left')
  500. ctx.setTextBaseline('top')
  501. ctx.fillText(this.priceText, cardX + 20, priceY)
  502. }
  503. // 简介(1-2行灰字;空则跳过)
  504. var summaryY = (this.priceText ? priceY + 34 : priceY)
  505. if (this.summary) {
  506. ctx.setFillStyle(BrandColors.stone500)
  507. ctx.setFontSize(this._fs(13))
  508. ctx.setTextAlign('left')
  509. ctx.setTextBaseline('top')
  510. this._drawWrappedText(ctx, this.summary, cardX + 20, summaryY, cardW - 40, this._fs(20), 2)
  511. }
  512. // 邀请语(二维码上方)
  513. var inviteY = (this.summary ? summaryY + 48 : summaryY)
  514. ctx.setFillStyle(BrandColors.stone400)
  515. ctx.setFontSize(this._fs(13))
  516. ctx.setTextAlign('center')
  517. ctx.setTextBaseline('top')
  518. ctx.fillText(this.inviteText || '长按识别二维码 · 查看详情', w / 2, inviteY)
  519. // 二维码 - 可配置尺寸(管理端模板 qrSize 优先,其次 qrSizeOverride prop,默认150)
  520. var qrSize = this.effectiveQrSize
  521. var qrX = (w - qrSize) / 2
  522. var qrY = inviteY + 24
  523. if (this.qrCodeBase64) {
  524. var base64Data = this.qrCodeBase64
  525. if (base64Data.indexOf('data:image') === 0) {
  526. var commaIdx = base64Data.indexOf(',')
  527. if (commaIdx > -1) {
  528. base64Data = base64Data.substring(commaIdx + 1)
  529. }
  530. }
  531. var fs = uni.getFileSystemManager()
  532. var filePath = wx.env.USER_DATA_PATH + '/content_share_qr_tmp.png'
  533. fs.writeFile({
  534. filePath: filePath,
  535. data: base64Data,
  536. encoding: 'base64',
  537. success: function() {
  538. ctx.setFillStyle(BrandColors.white)
  539. self._roundRect(ctx, qrX - 10, qrY - 10, qrSize + 20, qrSize + 20, 8)
  540. ctx.fill()
  541. ctx.setStrokeStyle(BrandColors.stone200)
  542. ctx.setLineWidth(0.5)
  543. self._roundRect(ctx, qrX - 10, qrY - 10, qrSize + 20, qrSize + 20, 8)
  544. ctx.stroke()
  545. ctx.drawImage(filePath, qrX, qrY, qrSize, qrSize)
  546. self._drawBottomDecoration(ctx, w, h, qrY, qrSize, colors)
  547. },
  548. fail: function() {
  549. self._drawQrPlaceholder(ctx, qrX, qrY, qrSize)
  550. self._drawBottomDecoration(ctx, w, h, qrY, qrSize, colors)
  551. }
  552. })
  553. } else {
  554. this._drawQrPlaceholder(ctx, qrX, qrY, qrSize)
  555. this._drawBottomDecoration(ctx, w, h, qrY, qrSize, colors)
  556. }
  557. },
  558. _drawWrappedText(ctx, text, x, y, maxWidth, lineHeight, maxLines) {
  559. var chars = (text || '').split('')
  560. var line = ''
  561. var lineCount = 0
  562. for (var i = 0; i < chars.length; i++) {
  563. var testLine = line + chars[i]
  564. var testWidth = this._textWidth(ctx, testLine)
  565. if (testWidth > maxWidth && line.length > 0) {
  566. ctx.fillText(line, x, y + lineCount * lineHeight)
  567. line = chars[i]
  568. lineCount++
  569. if (lineCount >= maxLines - 1) {
  570. var remaining = chars.slice(i).join('')
  571. var remainingLine = line + remaining
  572. while (this._textWidth(ctx, remainingLine + '..') > maxWidth && remainingLine.length > 2) {
  573. remainingLine = remainingLine.substring(0, remainingLine.length - 1)
  574. }
  575. ctx.fillText(remainingLine + '..', x, y + lineCount * lineHeight)
  576. return
  577. }
  578. } else {
  579. line = testLine
  580. }
  581. }
  582. ctx.fillText(line, x, y + lineCount * lineHeight)
  583. },
  584. _textWidth(ctx, text) {
  585. if (ctx.measureText) {
  586. try { return ctx.measureText(text).width } catch (e) { return (text || '').length * 18 }
  587. }
  588. return (text || '').length * 18
  589. },
  590. _drawQrPlaceholder(ctx, x, y, size) {
  591. ctx.setFillStyle(BrandColors.white)
  592. this._roundRect(ctx, x - 10, y - 10, size + 20, size + 20, 8)
  593. ctx.fill()
  594. ctx.setStrokeStyle(BrandColors.stone200)
  595. ctx.setLineWidth(0.5)
  596. this._roundRect(ctx, x - 10, y - 10, size + 20, size + 20, 8)
  597. ctx.stroke()
  598. ctx.setFillStyle(BrandColors.stone400)
  599. ctx.setFontSize(this._fs(16))
  600. ctx.setTextAlign('center')
  601. ctx.setTextBaseline('middle')
  602. ctx.fillText('二维码加载中', x + size / 2, y + size / 2)
  603. },
  604. _drawBottomDecoration(ctx, w, h, qrY, qrSize, colors) {
  605. var centerX = w / 2
  606. var bottomY = qrY + qrSize + 16
  607. var diamondY = bottomY + 6
  608. ctx.setFillStyle('rgba(0,0,0,0.06)')
  609. ctx.beginPath()
  610. ctx.moveTo(centerX - 76, diamondY)
  611. ctx.lineTo(centerX - 72, diamondY - 4)
  612. ctx.lineTo(centerX - 68, diamondY)
  613. ctx.lineTo(centerX - 72, diamondY + 4)
  614. ctx.closePath()
  615. ctx.fill()
  616. ctx.beginPath()
  617. ctx.moveTo(centerX + 68, diamondY)
  618. ctx.lineTo(centerX + 72, diamondY - 4)
  619. ctx.lineTo(centerX + 76, diamondY)
  620. ctx.lineTo(centerX + 72, diamondY + 4)
  621. ctx.closePath()
  622. ctx.fill()
  623. ctx.setFillStyle(colors.main)
  624. ctx.setFontSize(this._fs(18))
  625. ctx.setTextAlign('center')
  626. ctx.setTextBaseline('top')
  627. ctx.fillText(this.ctaText || '长按识别二维码 · 查看详情', centerX, bottomY)
  628. var dotColors = [BrandColors.body, BrandColors.wisdom, BrandColors.wealth, BrandColors.success, BrandColors.mind]
  629. var dotY = h - 22
  630. var dotR = 4
  631. var dotSpacing = 20
  632. var i_b, offsetX_b
  633. for (i_b = 0; i_b < 5; i_b++) {
  634. offsetX_b = (i_b - 2) * dotSpacing
  635. ctx.setFillStyle(dotColors[i_b])
  636. ctx.beginPath()
  637. ctx.arc(centerX + offsetX_b, dotY, dotR, 0, 2 * Math.PI)
  638. ctx.fill()
  639. }
  640. ctx.setFillStyle('rgba(255,255,255,0.45)')
  641. ctx.setFontSize(this._fs(12))
  642. ctx.setTextAlign('center')
  643. ctx.setTextBaseline('top')
  644. ctx.fillText('浠艾福 · 家庭健康俱乐部', centerX, dotY + 7)
  645. ctx.draw()
  646. },
  647. _roundRect(ctx, x, y, w, h, r) {
  648. ctx.beginPath()
  649. ctx.moveTo(x + r, y)
  650. ctx.arcTo(x + w, y, x + w, y + r, r)
  651. ctx.arcTo(x + w, y + h, x + w - r, y + h, r)
  652. ctx.arcTo(x, y + h, x, y + h - r, r)
  653. ctx.arcTo(x, y, x + r, y, r)
  654. ctx.closePath()
  655. },
  656. switchTemplate(tpl) {
  657. // 仅支持管理端定义模板(对象);未配置模板时无选择行,不会触发
  658. if (!tpl || typeof tpl !== 'object') return
  659. this.applyTemplate(tpl)
  660. },
  661. switchBgColor(color) {
  662. this.$emit('update:bgColor', color)
  663. var self = this
  664. this.$nextTick(function() {
  665. setTimeout(self.drawPoster.bind(self), 100)
  666. })
  667. },
  668. savePoster() {
  669. var self = this
  670. uni.canvasToTempFilePath({
  671. canvasId: 'contentPosterCanvas',
  672. destWidth: 500,
  673. destHeight: 800,
  674. success: function(res) {
  675. uni.saveImageToPhotosAlbum({
  676. filePath: res.tempFilePath,
  677. success: function() {
  678. uni.showToast({ title: '已保存到相册', icon: 'success' })
  679. self.$emit('close')
  680. },
  681. fail: function(err) {
  682. if (err && err.errMsg && (err.errMsg.indexOf('deny') > -1 || err.errMsg.indexOf('auth denied') > -1 || err.errMsg.indexOf('authorize') > -1)) {
  683. uni.showModal({
  684. title: '提示',
  685. content: '需要相册权限才能保存图片',
  686. confirmText: '去设置',
  687. success: function(modalRes) {
  688. if (modalRes.confirm) {
  689. uni.openSetting()
  690. }
  691. }
  692. })
  693. } else {
  694. uni.showToast({ title: '保存失败,请重试', icon: 'none' })
  695. }
  696. }
  697. })
  698. },
  699. fail: function() {
  700. uni.showToast({ title: '生成图片失败', icon: 'none' })
  701. }
  702. }, this)
  703. }
  704. }
  705. }
  706. </script>
  707. <style scoped lang="scss">
  708. .poster-overlay {
  709. position: fixed;
  710. top: 0;
  711. left: 0;
  712. right: 0;
  713. bottom: 0;
  714. background: rgba(0, 0, 0, 0.6);
  715. display: flex;
  716. align-items: center;
  717. justify-content: center;
  718. z-index: 999;
  719. }
  720. .poster-container {
  721. display: flex;
  722. flex-direction: column;
  723. align-items: center;
  724. }
  725. .poster-canvas {
  726. width: 620rpx;
  727. height: 992rpx;
  728. margin-bottom: 80rpx;
  729. border-radius: 16rpx;
  730. }
  731. .poster-actions {
  732. display: flex;
  733. flex-direction: column;
  734. width: 620rpx;
  735. margin-top: 30rpx;
  736. }
  737. .poster-btn {
  738. height: 80rpx;
  739. line-height: 80rpx;
  740. font-size: 30rpx;
  741. border-radius: 40rpx;
  742. text-align: center;
  743. border: none;
  744. }
  745. .poster-btn::after { border: none; }
  746. .poster-btn.save {
  747. background: $app-surface;
  748. color: $brand-primary;
  749. font-weight: 600;
  750. }
  751. .poster-btn.save:active { opacity: 0.85; }
  752. .poster-tpl-row {
  753. display: flex;
  754. align-items: center;
  755. margin: 12rpx 0;
  756. font-size: 24rpx;
  757. }
  758. .tpl-label {
  759. margin-right: 20rpx;
  760. color: $facade-text-body;
  761. }
  762. .tpl-options {
  763. display: flex;
  764. }
  765. .tpl-option {
  766. flex: 1;
  767. margin: 0 4rpx;
  768. display: flex;
  769. flex-direction: column;
  770. align-items: center;
  771. cursor: pointer;
  772. }
  773. .tpl-dot {
  774. width: 24rpx;
  775. height: 24rpx;
  776. border-radius: 999rpx;
  777. margin-bottom: 4rpx;
  778. border: 1rpx solid rgba(0, 0, 0, 0.08);
  779. box-sizing: border-box;
  780. }
  781. .tpl-text {
  782. font-size: 14rpx;
  783. text-align: center;
  784. color: $facade-text-body;
  785. }
  786. .tpl-active .tpl-text { color: $brand-primary; }
  787. </style>