完善咨询表单与企业微信诉求推送
This commit is contained in:
+11
-10
@@ -12,6 +12,7 @@ import { initDB, getDB, UPLOAD_DIR } from './db.js';
|
||||
import { signToken, verifyToken, authMiddleware } from './middleware/auth.js';
|
||||
import { createContentIngestRouter } from './content-ingest.js';
|
||||
import { sendContactLeadToWeCom } from './wecom-notifier.js';
|
||||
import { ContactLeadValidationError, normalizeContactLeadInput } from './contact-leads.js';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const app = express();
|
||||
@@ -131,18 +132,18 @@ app.use('/api/internal/content/v1', createContentIngestRouter());
|
||||
app.use('/api/contact/leads', contactLeadRateLimit);
|
||||
|
||||
app.post('/api/contact/leads', (req, res) => {
|
||||
const { name, company = '', phone, need = '' } = req.body || {};
|
||||
if (!name || !phone) return res.status(400).json({ code: 400, msg: '姓名和手机号必填' });
|
||||
if (!/^1[3-9]\d{9}$/.test(String(phone))) return res.status(400).json({ code: 400, msg: '手机号格式错误' });
|
||||
let lead;
|
||||
try {
|
||||
lead = normalizeContactLeadInput(req.body);
|
||||
} catch (error) {
|
||||
if (error instanceof ContactLeadValidationError) {
|
||||
return res.status(400).json({ code: 400, msg: error.message });
|
||||
}
|
||||
return res.status(400).json({ code: 400, msg: '表单内容格式错误' });
|
||||
}
|
||||
const id = genId('contact');
|
||||
const createdAt = nowISO();
|
||||
const lead = {
|
||||
name: String(name).trim().slice(0, 80),
|
||||
company: String(company).trim().slice(0, 160),
|
||||
phone: String(phone),
|
||||
need: String(need).trim().slice(0, 2000),
|
||||
createdAt
|
||||
};
|
||||
lead.createdAt = createdAt;
|
||||
db.run(`INSERT INTO contact_leads (id, name, company, phone, need, created_at) VALUES (?,?,?,?,?,?)`,
|
||||
[id, lead.name, lead.company, lead.phone, lead.need, createdAt], err => {
|
||||
if (err) return res.status(500).json({ code: 500, msg: '提交失败,请稍后再试' });
|
||||
|
||||
Reference in New Issue
Block a user