ContentSharePoster.vue 29 KB

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