-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
147 lines (121 loc) · 4.46 KB
/
app.js
File metadata and controls
147 lines (121 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const puppeteer = require('puppeteer')
const fs = require('fs')
const path = require('path')
class PosterGenerator {
constructor() {
this.browser = null
this.templateDir = path.join(__dirname, 'templates')
this.outputDir = path.join(__dirname, 'output')
}
async init() {
this.browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox'],
})
}
async generate(templateName, data, outputFileName) {
const page = await this.browser.newPage()
// 读取HTML模板
const htmlPath = path.join(this.templateDir, `${templateName}.html`)
let html = fs.readFileSync(htmlPath, 'utf-8')
// 替换模板变量
html = this.replaceTemplateVariables(html, data)
// 设置HTML内容
await page.setContent(html, { waitUntil: 'networkidle0' })
// 设置视口
await page.setViewport({ width: 750, height: 1334 })
// 截图
const screenshot = await page.screenshot({
type: 'jpeg',
quality: 90,
fullPage: false,
})
await page.close()
// 确保输出目录存在
if (!fs.existsSync(this.outputDir)) {
fs.mkdirSync(this.outputDir, { recursive: true })
}
// 生成输出文件名
const fileName = outputFileName || `${templateName}-${Date.now()}.jpg`
const outputPath = path.join(this.outputDir, fileName)
// 保存图片到本地
fs.writeFileSync(outputPath, screenshot)
console.log(`图片已保存到: ${outputPath}`)
return outputPath
}
replaceTemplateVariables(html, data) {
let result = html
// 处理格式化函数 formatPrice
result = result.replace(
/\{\{formatPrice\s+(\w+)\}\}/g,
(match, key) => {
const value = data[key] || 0
// 格式化价格:每3位数字加一个逗号
return (
value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') +
'万'
)
}
)
// 处理条件语句 {{#if hasParking}}...{{/if}}
result = result.replace(
/\{\{#if\s+(\w+)\}\}([\s\S]*?)\{\{\/if\}\}/g,
(match, key, content) => {
if (data[key]) {
// 递归处理内容中的变量
return this.replaceTemplateVariables(content, data)
}
return ''
}
)
// 处理条件比较 {{#ifCond taxFree '==' 'true'}}...{{/ifCond}}
result = result.replace(
/\{\{#ifCond\s+(\w+)\s+('==='?|'!=='?|'=='|'!=')\s+('.*?'|".*?"|\w+)\}\}([\s\S]*?)\{\{\/ifCond\}\}/g,
(match, key, operator, value, content) => {
const dataValue = data[key]
const compareValue = value.replace(/['"]/g, '') // 移除引号
let condition = false
if (operator === "'=='" || operator === "'==='") {
condition = String(dataValue) === compareValue
} else if (operator === "'!='" || operator === "'!==") {
condition = String(dataValue) !== compareValue
}
if (condition) {
return this.replaceTemplateVariables(content, data)
}
return ''
}
)
// 简单的模板变量替换
for (const [key, value] of Object.entries(data)) {
const regex = new RegExp(`\\{\\{${key}\\}\\}`, 'g')
result = result.replace(regex, value)
}
return result
}
async close() {
if (this.browser) {
await this.browser.close()
}
}
}
module.exports = PosterGenerator
const posterGenerator = new PosterGenerator()
// 模拟房源数据
const mockHouseData = {
price: 268, // 总价(万元)
area: 89, // 面积(平米)
gift: 15, // 赠送面积(平米)
layout: '3室2厅3室', // 户型
decoration: '精装', // 装修
hasParking: true, // 是否有车位
parkingPrice: 25, // 车位价格(万元)
extraArea: 12, // 附加面积(平米)
floor: '15/30', // 楼层
taxFree: 'true', // 是否免税
}
;(async () => {
await posterGenerator.init()
await posterGenerator.generate('house-poster', mockHouseData)
await posterGenerator.close()
})()