ContentSharePoster.vue 29 KB

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