hookehuyr

feat(recall): 新增分享海报功能组件和页面

添加 RecallPoster 组件用于生成分享海报
创建 PosterPage 页面路由和视图
实现海报背景更换、竖排文字绘制和二维码展示功能
...@@ -32,6 +32,7 @@ declare module 'vue' { ...@@ -32,6 +32,7 @@ declare module 'vue' {
32 PdfPreview: typeof import('./components/ui/PdfPreview.vue')['default'] 32 PdfPreview: typeof import('./components/ui/PdfPreview.vue')['default']
33 PdfViewer: typeof import('./components/ui/PdfViewer.vue')['default'] 33 PdfViewer: typeof import('./components/ui/PdfViewer.vue')['default']
34 PostCountModel: typeof import('./components/count/postCountModel.vue')['default'] 34 PostCountModel: typeof import('./components/count/postCountModel.vue')['default']
35 + RecallPoster: typeof import('./components/ui/RecallPoster.vue')['default']
35 ReviewPopup: typeof import('./components/courses/ReviewPopup.vue')['default'] 36 ReviewPopup: typeof import('./components/courses/ReviewPopup.vue')['default']
36 RouterLink: typeof import('vue-router')['RouterLink'] 37 RouterLink: typeof import('vue-router')['RouterLink']
37 RouterView: typeof import('vue-router')['RouterView'] 38 RouterView: typeof import('vue-router')['RouterView']
......
1 +<template>
2 + <div class="recall-poster-container w-full max-w-[340px] relative select-none shrink-0 my-auto mx-auto">
3 + <!-- 最终生成的海报图片展示区域 -->
4 + <div v-if="posterImgSrc" class="relative w-full fade-in">
5 + <img :src="posterImgSrc" class="w-full h-auto rounded-2xl shadow-2xl block" alt="分享海报" />
6 + <div class="text-white/80 text-center text-xs mt-4">长按图片保存</div>
7 + </div>
8 +
9 + <!-- 生成中/加载中占位 -->
10 + <div v-else
11 + class="w-full h-[62vh] min-h-[400px] bg-white/10 backdrop-blur-sm rounded-2xl flex flex-col items-center justify-center text-white/80">
12 + <van-loading type="spinner" color="#ffffff" size="32px" />
13 + <div class="mt-4 text-sm font-medium">海报生成中...</div>
14 + </div>
15 +
16 + <!-- Canvas (Hidden) -->
17 + <canvas ref="canvasRef" class="hidden"></canvas>
18 + </div>
19 +</template>
20 +
21 +<script setup>
22 +import { ref, watch, nextTick, onMounted } from 'vue'
23 +import { showToast } from 'vant'
24 +
25 +const props = defineProps({
26 + bgUrl: {
27 + type: String,
28 + required: true
29 + },
30 + title: {
31 + type: String,
32 + default: ''
33 + },
34 + logoUrl: {
35 + type: String,
36 + default: 'https://cdn.ipadbiz.cn/mlaj/recall/poster/kai@2x.png'
37 + },
38 + qrUrl: {
39 + type: String,
40 + default: 'https://cdn.ipadbiz.cn/mlaj/recall/poster/%E4%BA%8C%E7%BB%B4%E7%A0%81@2x.png'
41 + }
42 +})
43 +
44 +const canvasRef = ref(null)
45 +const posterImgSrc = ref('')
46 +
47 +// 工具函数:加载图片
48 +const loadImage = (src) => {
49 + return new Promise((resolve, reject) => {
50 + if (!src) {
51 + reject(new Error('Image source is empty'))
52 + return
53 + }
54 + const img = new Image()
55 + // 处理跨域,Blob URL 不需要
56 + if (!src.startsWith('blob:') && !src.startsWith('data:')) {
57 + img.crossOrigin = 'anonymous'
58 + }
59 + img.onload = () => resolve(img)
60 + img.onerror = (e) => {
61 + console.error('Failed to load image:', src)
62 + // 图片加载失败不应该阻断流程,返回 null 或者透明图占位
63 + // 这里resolve null,绘制时跳过
64 + resolve(null)
65 + }
66 + img.src = src
67 + })
68 +}
69 +
70 +// 工具函数:绘制圆角矩形
71 +const drawRoundedRect = (ctx, x, y, width, height, radius) => {
72 + ctx.beginPath()
73 + ctx.moveTo(x + radius, y)
74 + ctx.lineTo(x + width - radius, y)
75 + ctx.quadraticCurveTo(x + width, y, x + width, y + radius)
76 + ctx.lineTo(x + width, y + height - radius)
77 + ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height)
78 + ctx.lineTo(x + radius, y + height)
79 + ctx.quadraticCurveTo(x, y + height, x, y + height - radius)
80 + ctx.lineTo(x, y + radius)
81 + ctx.quadraticCurveTo(x, y, x + radius, y)
82 + ctx.closePath()
83 +}
84 +
85 +// 工具函数:绘制多行文本
86 +const wrapText = (ctx, text, x, y, maxWidth, lineHeight, maxLines) => {
87 + const words = text.split('') // 中文按字分割
88 + let line = ''
89 + let lineCount = 0
90 + let currentY = y
91 +
92 + for (let n = 0; n < words.length; n++) {
93 + const testLine = line + words[n]
94 + const metrics = ctx.measureText(testLine)
95 + const testWidth = metrics.width
96 +
97 + if (testWidth > maxWidth && n > 0) {
98 + ctx.fillText(line, x, currentY)
99 + line = words[n]
100 + currentY += lineHeight
101 + lineCount++
102 + if (maxLines && lineCount >= maxLines) {
103 + // 超过最大行数,最后一行加省略号(简化处理,暂不精确计算省略号位置)
104 + return
105 + }
106 + } else {
107 + line = testLine
108 + }
109 + }
110 + ctx.fillText(line, x, currentY)
111 +}
112 +
113 +// 工具函数:绘制竖排文字
114 +const drawVerticalText = (ctx, text, x, y, fontSize, letterSpacing) => {
115 + const chars = text.split('')
116 + let currentY = y
117 + ctx.font = `500 ${fontSize}px sans-serif`
118 + ctx.textAlign = 'center'
119 + ctx.textBaseline = 'middle'
120 +
121 + chars.forEach(char => {
122 + ctx.fillText(char, x, currentY)
123 + currentY += fontSize + letterSpacing
124 + })
125 +}
126 +
127 +// 核心生成逻辑:Canvas 绘制
128 +const generatePoster = async () => {
129 + posterImgSrc.value = ''
130 +
131 + // 确保 Canvas 元素存在
132 + await nextTick()
133 + const canvas = canvasRef.value
134 + if (!canvas) return
135 +
136 + try {
137 + // 1. 准备画布尺寸 (2倍图)
138 + const scale = 2
139 + const width = 375 * scale
140 + // 图片区域高度缩小,从 600 -> 480,保持较好比例
141 + const imgAreaHeight = 480 * scale
142 + const infoAreaHeight = 130 * scale
143 + const height = imgAreaHeight + infoAreaHeight
144 +
145 + canvas.width = width
146 + canvas.height = height
147 + const ctx = canvas.getContext('2d')
148 +
149 + // 设置白色背景
150 + ctx.fillStyle = '#ffffff'
151 + ctx.fillRect(0, 0, width, height)
152 +
153 + // 2. 并行加载所有图片资源
154 + const [bgImg, logoImg, qrImg] = await Promise.all([
155 + loadImage(props.bgUrl),
156 + loadImage(props.logoUrl),
157 + loadImage(props.qrUrl)
158 + ])
159 +
160 + // 3. 绘制背景图 (Object-Cover 效果)
161 + if (bgImg) {
162 + // 计算裁剪
163 + const imgRatio = bgImg.width / bgImg.height
164 + const canvasRatio = width / imgAreaHeight
165 + let sx, sy, sWidth, sHeight
166 +
167 + if (imgRatio > canvasRatio) {
168 + // 图片更宽,裁左右
169 + sHeight = bgImg.height
170 + sWidth = sHeight * canvasRatio
171 + sx = (bgImg.width - sWidth) / 2
172 + sy = 0
173 + } else {
174 + // 图片更高,裁上下
175 + sWidth = bgImg.width
176 + sHeight = sWidth / canvasRatio
177 + sx = 0
178 + sy = (bgImg.height - sHeight) / 2
179 + }
180 +
181 + ctx.drawImage(bgImg, sx, sy, sWidth, sHeight, 0, 0, width, imgAreaHeight)
182 + }
183 +
184 + // 4. 绘制 Logo (左上角)
185 + if (logoImg) {
186 + const logoW = 96 * scale // w-24 = 96px
187 + const logoRatio = logoImg.width / logoImg.height
188 + const logoH = logoW / logoRatio
189 + const logoX = 24 * scale // left-6
190 + const logoY = 24 * scale // top-6
191 +
192 + // 添加阴影
193 + ctx.shadowColor = 'rgba(0, 0, 0, 0.2)'
194 + ctx.shadowBlur = 4 * scale
195 + ctx.shadowOffsetY = 2 * scale
196 + ctx.drawImage(logoImg, logoX, logoY, logoW, logoH)
197 + ctx.shadowColor = 'transparent' // 重置阴影
198 + }
199 +
200 + // 5. 绘制竖排文字 (右下角)
201 + // 位置: right-5 (20px), bottom-6 (24px) relative to imgArea
202 + const textRightMargin = 20 * scale
203 + const textBottomMargin = 24 * scale
204 + const fontSize = 13 * scale
205 + const letterSpacing = fontSize * 0.5 // tracking-[0.5em]
206 +
207 + ctx.fillStyle = 'rgba(255, 255, 255, 0.9)'
208 + // Shadow
209 + ctx.shadowColor = 'rgba(0, 0, 0, 0.3)'
210 + ctx.shadowBlur = 2 * scale
211 + ctx.shadowOffsetY = 1 * scale
212 +
213 + // Column 1: "每一段成长故事" (左边那列,离右边远一点)
214 + // Column 2: "见证我在美乐爱觉宇宙的" (右边那列)
215 +
216 + const colRightX = width - textRightMargin - (fontSize / 2)
217 + const colLeftX = colRightX - fontSize - 12 * scale // gap-3 = 12px
218 +
219 + const textRight = "见证我在美乐爱觉宇宙的"
220 + const textLeft = "每一段成长故事"
221 +
222 + // 计算文本对齐
223 + // 目标:整个文本块的底部与 bottom-6 对齐
224 + // 右列(第一句)作为基准
225 + // 左列(第二句)相对于右列下移 48px (mt-12)
226 +
227 + const charH = fontSize + letterSpacing
228 + const hRight = textRight.length * charH - letterSpacing
229 + const hLeft = textLeft.length * charH - letterSpacing
230 + const offset = 48 * scale // mt-12
231 +
232 + const maxBottom = imgAreaHeight - textBottomMargin
233 + // 计算起始 Y 坐标,使得最底部的点不超过 maxBottom
234 + const heightDiff = Math.max(hRight, offset + hLeft)
235 + const tRight = maxBottom - heightDiff
236 + const tLeft = tRight + offset
237 +
238 + // 设置字体
239 + ctx.font = `500 ${fontSize}px sans-serif`
240 +
241 + // 内部绘制函数
242 + const drawVert = (txt, x, startY) => {
243 + const chars = txt.split('')
244 + let cy = startY + fontSize / 2
245 + ctx.textAlign = 'center'
246 + ctx.textBaseline = 'middle'
247 + chars.forEach(char => {
248 + ctx.fillText(char, x, cy)
249 + cy += fontSize + letterSpacing
250 + })
251 + }
252 +
253 + drawVert(textRight, colRightX, tRight)
254 + drawVert(textLeft, colLeftX, tLeft)
255 +
256 + ctx.shadowColor = 'transparent'
257 +
258 +
259 + // 6. 绘制 Info Area 内容
260 + // 坐标参考
261 + const infoY = imgAreaHeight
262 +
263 + // 6.1 Title
264 + // Padding: p-5 (20px). Left side.
265 + // pr-4 (16px) for title container.
266 + // Title Width = Total Width - Padding Left - Padding Right - QR Section Width
267 + // QR Section: w-[72px] + margins?
268 + // DOM: justify-between.
269 + // QR Section is shrink-0.
270 + // QR Image: 72px. Text below.
271 + // Let's reserve 100px width for QR section on the right.
272 +
273 + const padding = 20 * scale
274 + const titleX = padding
275 + const titleY = infoY + padding
276 + const qrSectionW = 85 * scale // approx
277 + const titleMaxW = width - padding - qrSectionW - (10 * scale) // extra gap
278 +
279 + ctx.fillStyle = '#0052D9'
280 + ctx.font = `bold ${15 * scale}px sans-serif`
281 + ctx.textAlign = 'left'
282 + ctx.textBaseline = 'top'
283 +
284 + // Line height: leading-relaxed (approx 1.6?)
285 + const lineHeight = 15 * scale * 1.6
286 + wrapText(ctx, props.title, titleX, titleY, titleMaxW, lineHeight, 4)
287 +
288 + // 6.2 QR Code
289 + if (qrImg) {
290 + const qrSize = 72 * scale
291 + // Center in the right section
292 + // Section starts at width - padding - qrSectionW?
293 + // Actually DOM is flex justify-between.
294 + // QR is at the very right (minus padding).
295 + const qrX = width - padding - qrSize + (4 * scale) // slight adjustment
296 + const qrY = infoY + padding
297 +
298 + ctx.drawImage(qrImg, qrX, qrY, qrSize, qrSize)
299 +
300 + // 6.3 QR Text
301 + // text-[10px], scale-90. Effective size 9px.
302 + // text-[#666]
303 + const smallTextSize = 10 * scale * 0.9
304 + ctx.fillStyle = '#666666'
305 + ctx.font = `${smallTextSize}px sans-serif`
306 + ctx.textAlign = 'center'
307 +
308 + const textCenterX = qrX + qrSize / 2
309 + const textStartY = qrY + qrSize + (8 * scale) // mb-2 is for img
310 +
311 + ctx.fillText("跟我一起加入", textCenterX, textStartY)
312 + ctx.fillText("美乐爱觉宇宙吧", textCenterX, textStartY + smallTextSize * 1.4)
313 + }
314 +
315 + // 7. 导出图片
316 + posterImgSrc.value = canvas.toDataURL('image/png')
317 +
318 + } catch (error) {
319 + console.error('Canvas poster generation failed:', error)
320 + showToast('生成失败,请重试')
321 + }
322 +}
323 +
324 +watch(() => props.bgUrl, () => {
325 + generatePoster()
326 +})
327 +
328 +onMounted(() => {
329 + // 稍微延时确保字体等资源就绪(虽然canvas不强依赖DOM渲染,但字体加载是全局的)
330 + setTimeout(generatePoster, 500)
331 +})
332 +
333 +defineExpose({
334 + generatePoster
335 +})
336 +</script>
337 +
338 +<style scoped>
339 +.fade-in {
340 + animation: fadeIn 0.5s ease-in-out;
341 +}
342 +
343 +@keyframes fadeIn {
344 + from {
345 + opacity: 0;
346 + }
347 +
348 + to {
349 + opacity: 1;
350 + }
351 +}
352 +</style>
1 /* 1 /*
2 * @Date: 2025-03-20 20:36:36 2 * @Date: 2025-03-20 20:36:36
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-12-23 15:31:50 4 + * @LastEditTime: 2025-12-23 15:51:28
5 * @FilePath: /mlaj/src/router/routes.js 5 * @FilePath: /mlaj/src/router/routes.js
6 * @Description: 路由地址映射配置 6 * @Description: 路由地址映射配置
7 */ 7 */
...@@ -139,6 +139,12 @@ export const routes = [ ...@@ -139,6 +139,12 @@ export const routes = [
139 meta: { title: '活动历史' }, 139 meta: { title: '活动历史' },
140 }, 140 },
141 { 141 {
142 + path: '/recall/poster',
143 + name: 'Poster',
144 + component: () => import('../views/recall/PosterPage.vue'),
145 + meta: { title: '分享海报' },
146 + },
147 + {
142 path: '/checkout', 148 path: '/checkout',
143 name: 'CheckoutPage', 149 name: 'CheckoutPage',
144 component: () => import('../views/checkout/CheckoutPage.vue'), 150 component: () => import('../views/checkout/CheckoutPage.vue'),
......
1 +<!--
2 + * @Date: 2025-12-23 15:50:59
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2025-12-23 16:40:19
5 + * @FilePath: /mlaj/src/views/recall/PosterPage.vue
6 + * @Description: 分享海报页面
7 +-->
8 +<template>
9 + <div class="min-h-screen bg-[#1E40C8] relative flex flex-col">
10 +
11 + <!-- Poster Container (Scrollable Area) -->
12 + <div class="flex-1 overflow-y-auto px-6 pt-6 pb-32 flex flex-col items-center">
13 + <RecallPoster
14 + :bg-url="posterBg"
15 + :title="title"
16 + :logo-url="logoUrl"
17 + :qr-url="qrCodeUrl"
18 + />
19 + </div>
20 +
21 + <!-- Buttons (Fixed at Bottom) -->
22 + <div class="fixed bottom-0 left-0 right-0 bg-[#1E40C8] px-8 py-6 pb-10 flex gap-4 z-50 max-w-md mx-auto w-full">
23 + <van-uploader :after-read="afterRead" accept="image/*" class="flex-1 w-full" :show-upload="false">
24 + <template #default>
25 + <van-button block color="#0052D9" plain
26 + class="!rounded-lg !h-[44px] !text-[15px] !font-bold w-full !border-[#0052D9] !bg-white">
27 + 更换图片
28 + </van-button>
29 + </template>
30 + </van-uploader>
31 + </div>
32 + </div>
33 +</template>
34 +
35 +<script setup>
36 +import { ref } from 'vue'
37 +import { useRoute, useRouter } from 'vue-router'
38 +import { useTitle } from '@vueuse/core'
39 +import RecallPoster from '@/components/ui/RecallPoster.vue'
40 +
41 +const $route = useRoute();
42 +const $router = useRouter();
43 +useTitle('分享海报');
44 +
45 +// Assets
46 +const defaultBg = 'https://cdn.ipadbiz.cn/mlaj/images/test-bgg03.jpg?imageMogr2/thumbnail/800x/strip/quality/80'
47 +const logoUrl = 'https://cdn.ipadbiz.cn/mlaj/recall/poster/kai@2x.png'
48 +const qrCodeUrl = 'https://cdn.ipadbiz.cn/mlaj/recall/poster/%E4%BA%8C%E7%BB%B4%E7%A0%81@2x.png'
49 +
50 +// State
51 +const posterBg = ref(defaultBg)
52 +const title = ref('2017.7.6-7.11 【自然的恩典】“爱我中华”优秀传统文化夏令营-天津场开启~')
53 +
54 +// Actions
55 +const afterRead = (file) => {
56 + // Use Object URL for local preview to avoid uploading
57 + posterBg.value = URL.createObjectURL(file.file)
58 +}
59 +</script>
60 +
61 +<style lang="less" scoped>
62 +:deep(.van-uploader__input-wrapper) {
63 + width: 100%;
64 +}
65 +</style>