Ship signup sheet, subscribe reminders, cover crop, and theme updates.
Wire real WeChat app config, prefer activity cover over posters, and add admin cover cropping for the home banner. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
WX_APPID=你的小程序AppID
|
||||
WX_SECRET=你的小程序AppSecret
|
||||
# WX_SUBSCRIBE_TMPL_ID=订阅消息模板ID
|
||||
# PORT=3000
|
||||
+15
-1
@@ -33,7 +33,11 @@ const SEED = {
|
||||
],
|
||||
posters: [],
|
||||
signups: [],
|
||||
checkinCodes: []
|
||||
checkinCodes: [],
|
||||
settings: {
|
||||
// 微信公众平台 → 订阅消息 → 选用「活动提醒」类模板后填入模板 ID
|
||||
subscribeTmplId: process.env.WX_SUBSCRIBE_TMPL_ID || ''
|
||||
}
|
||||
}
|
||||
|
||||
const COLLECTIONS = ['users', 'admins', 'adminAllowlist', 'activities', 'posters', 'signups', 'checkinCodes']
|
||||
@@ -49,6 +53,16 @@ function normalize(data) {
|
||||
dirty = true
|
||||
}
|
||||
})
|
||||
if (!data.settings || typeof data.settings !== 'object') {
|
||||
data.settings = { subscribeTmplId: process.env.WX_SUBSCRIBE_TMPL_ID || '' }
|
||||
dirty = true
|
||||
} else if (typeof data.settings.subscribeTmplId !== 'string') {
|
||||
data.settings.subscribeTmplId = process.env.WX_SUBSCRIBE_TMPL_ID || ''
|
||||
dirty = true
|
||||
} else if (!data.settings.subscribeTmplId && process.env.WX_SUBSCRIBE_TMPL_ID) {
|
||||
data.settings.subscribeTmplId = process.env.WX_SUBSCRIBE_TMPL_ID
|
||||
dirty = true
|
||||
}
|
||||
|
||||
// The allowlist is the only source of truth for mini-program admins.
|
||||
data.users.forEach((user) => {
|
||||
|
||||
@@ -24,7 +24,8 @@ function publicActivity(activity, posters) {
|
||||
theme: activity.theme || 0,
|
||||
cover: activity.cover || '',
|
||||
video: activity.video || '',
|
||||
poster: (poster && poster.image) || activity.cover || '',
|
||||
// Activity cover wins over linked posters so admin “封面图片” updates show immediately.
|
||||
poster: activity.cover || (poster && poster.image) || '',
|
||||
createdAt: activity.createdAt
|
||||
}
|
||||
}
|
||||
|
||||
+72
-6
@@ -1,10 +1,14 @@
|
||||
const crypto = require('crypto')
|
||||
const { HttpError } = require('./http')
|
||||
|
||||
let tokenCache = null
|
||||
|
||||
function wxConfigured() {
|
||||
return !!(process.env.WX_APPID && process.env.WX_SECRET)
|
||||
}
|
||||
|
||||
async function code2session(code, deviceId) {
|
||||
const appid = process.env.WX_APPID
|
||||
const secret = process.env.WX_SECRET
|
||||
if (!appid || !secret) {
|
||||
if (!wxConfigured()) {
|
||||
const seed = deviceId || 'guest'
|
||||
return {
|
||||
openid: `demo_${crypto.createHash('sha1').update(seed).digest('hex').slice(0, 16)}`,
|
||||
@@ -14,8 +18,8 @@ async function code2session(code, deviceId) {
|
||||
|
||||
const url = new URL('https://api.weixin.qq.com/sns/jscode2session')
|
||||
url.search = new URLSearchParams({
|
||||
appid,
|
||||
secret,
|
||||
appid: process.env.WX_APPID,
|
||||
secret: process.env.WX_SECRET,
|
||||
js_code: code,
|
||||
grant_type: 'authorization_code'
|
||||
}).toString()
|
||||
@@ -31,4 +35,66 @@ async function code2session(code, deviceId) {
|
||||
return { openid: session.openid, demo: false }
|
||||
}
|
||||
|
||||
module.exports = { code2session }
|
||||
async function getAccessToken() {
|
||||
if (!wxConfigured()) throw new HttpError(400, '未配置 WX_APPID / WX_SECRET,无法发送订阅消息')
|
||||
if (tokenCache && tokenCache.expiresAt > Date.now() + 60_000) return tokenCache.token
|
||||
|
||||
const url = new URL('https://api.weixin.qq.com/cgi-bin/token')
|
||||
url.search = new URLSearchParams({
|
||||
grant_type: 'client_credential',
|
||||
appid: process.env.WX_APPID,
|
||||
secret: process.env.WX_SECRET
|
||||
}).toString()
|
||||
|
||||
const res = await fetch(url, { signal: AbortSignal.timeout(8000) })
|
||||
const body = await res.json()
|
||||
if (!body.access_token) throw new HttpError(502, body.errmsg || '获取微信 access_token 失败')
|
||||
tokenCache = {
|
||||
token: body.access_token,
|
||||
expiresAt: Date.now() + (Number(body.expires_in) || 7200) * 1000
|
||||
}
|
||||
return tokenCache.token
|
||||
}
|
||||
|
||||
function clip(value, max) {
|
||||
const text = value == null ? '' : String(value)
|
||||
return text.length > max ? text.slice(0, max) : text
|
||||
}
|
||||
|
||||
// Default field mapping for a typical “活动提醒” template (thing / time / thing).
|
||||
function buildRemindData(activity) {
|
||||
return {
|
||||
thing1: { value: clip(activity.title || '卓米会活动', 20) },
|
||||
time2: { value: clip(activity.startAt || '待定', 20) },
|
||||
thing3: { value: clip(activity.address || '详见活动页', 20) }
|
||||
}
|
||||
}
|
||||
|
||||
async function sendSubscribeMessage({ openid, templateId, page, data }) {
|
||||
const token = await getAccessToken()
|
||||
const res = await fetch(
|
||||
`https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${encodeURIComponent(token)}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
touser: openid,
|
||||
template_id: templateId,
|
||||
page: page || '',
|
||||
data,
|
||||
miniprogram_state: process.env.WX_MINI_STATE || 'formal'
|
||||
}),
|
||||
signal: AbortSignal.timeout(8000)
|
||||
}
|
||||
)
|
||||
const body = await res.json()
|
||||
return { ok: body.errcode === 0, errcode: body.errcode, errmsg: body.errmsg || '' }
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
wxConfigured,
|
||||
code2session,
|
||||
getAccessToken,
|
||||
buildRemindData,
|
||||
sendSubscribeMessage
|
||||
}
|
||||
|
||||
+241
-20
@@ -35,10 +35,51 @@
|
||||
.split { display: grid; grid-template-columns: 1.1fr .9fr; gap: 16px; }
|
||||
.error { color: #dc2626; min-height: 1.2em; font-size: 13px; }
|
||||
img.qr { width: 140px; height: 140px; }
|
||||
.crop-modal {
|
||||
position: fixed; inset: 0; z-index: 50; display: none;
|
||||
align-items: center; justify-content: center;
|
||||
background: rgba(15, 23, 42, 0.55); padding: 16px;
|
||||
}
|
||||
.crop-modal.on { display: flex; }
|
||||
.crop-panel {
|
||||
width: min(720px, 100%); background: #fff; border-radius: 16px;
|
||||
padding: 16px; max-height: 92vh; overflow: auto;
|
||||
}
|
||||
.crop-stage {
|
||||
position: relative; width: 100%; height: min(52vh, 420px);
|
||||
background: #111827; border-radius: 12px; overflow: hidden;
|
||||
touch-action: none; user-select: none;
|
||||
}
|
||||
.crop-stage img {
|
||||
position: absolute; left: 0; top: 0;
|
||||
max-width: none; cursor: grab;
|
||||
}
|
||||
.crop-mask {
|
||||
position: absolute; inset: 0; pointer-events: none;
|
||||
box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45);
|
||||
border: 2px solid #fff; border-radius: 4px;
|
||||
}
|
||||
@media (max-width: 800px) { .grid, .split { grid-template-columns: 1fr; } }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="crop-modal" id="cropModal">
|
||||
<div class="crop-panel">
|
||||
<strong>裁剪封面</strong>
|
||||
<p class="muted">拖动图片选择区域,比例已锁定为小程序首页封面(约 2.3:1)。裁剪后再上传,预览就不会被裁掉重要内容。</p>
|
||||
<div class="crop-stage" id="cropStage">
|
||||
<img id="cropImage" alt="裁剪预览" draggable="false" />
|
||||
<div class="crop-mask" id="cropMask"></div>
|
||||
</div>
|
||||
<div class="row" style="margin-top:12px">
|
||||
<button class="ghost" id="cropZoomOut" type="button">缩小</button>
|
||||
<button class="ghost" id="cropZoomIn" type="button">放大</button>
|
||||
<button class="ghost" id="cropCancel" type="button">取消</button>
|
||||
<button class="primary" id="cropConfirm" type="button">裁剪并上传</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="login" id="loginView">
|
||||
<form class="panel" id="loginForm">
|
||||
<h1>卓米会后台</h1>
|
||||
@@ -67,6 +108,7 @@
|
||||
<button type="button" data-tab="signups">报名</button>
|
||||
<button type="button" data-tab="checkin">签到码</button>
|
||||
<button type="button" data-tab="staff">管理员</button>
|
||||
<button type="button" data-tab="settings">提醒设置</button>
|
||||
</nav>
|
||||
<main>
|
||||
<section id="stats"></section>
|
||||
@@ -75,6 +117,7 @@
|
||||
<section id="signups" hidden></section>
|
||||
<section id="checkin" hidden></section>
|
||||
<section id="staff" hidden></section>
|
||||
<section id="settings" hidden></section>
|
||||
</main>
|
||||
</div>
|
||||
<script>
|
||||
@@ -109,17 +152,164 @@
|
||||
return api('/api/admin/upload-file', { method: 'POST', body: form })
|
||||
}
|
||||
|
||||
// Banner on the mini program is ~750×320, keep the same aspect when cropping covers.
|
||||
const COVER_RATIO = 750 / 320
|
||||
const cropState = {
|
||||
targetInput: null,
|
||||
hint: null,
|
||||
naturalW: 0,
|
||||
naturalH: 0,
|
||||
scale: 1,
|
||||
x: 0,
|
||||
y: 0,
|
||||
dragging: false,
|
||||
lastX: 0,
|
||||
lastY: 0
|
||||
}
|
||||
|
||||
function layoutCrop() {
|
||||
const stage = document.getElementById('cropStage')
|
||||
const img = document.getElementById('cropImage')
|
||||
const mask = document.getElementById('cropMask')
|
||||
if (!stage || !img.naturalWidth) return
|
||||
const sw = stage.clientWidth
|
||||
const sh = stage.clientHeight
|
||||
let maskW = sw - 32
|
||||
let maskH = maskW / COVER_RATIO
|
||||
if (maskH > sh - 32) {
|
||||
maskH = sh - 32
|
||||
maskW = maskH * COVER_RATIO
|
||||
}
|
||||
mask.style.width = `${maskW}px`
|
||||
mask.style.height = `${maskH}px`
|
||||
mask.style.left = `${(sw - maskW) / 2}px`
|
||||
mask.style.top = `${(sh - maskH) / 2}px`
|
||||
|
||||
img.style.width = `${cropState.naturalW * cropState.scale}px`
|
||||
img.style.height = `${cropState.naturalH * cropState.scale}px`
|
||||
img.style.transform = `translate(${cropState.x}px, ${cropState.y}px)`
|
||||
}
|
||||
|
||||
function fitCropImage() {
|
||||
const stage = document.getElementById('cropStage')
|
||||
const maskW = Math.min(stage.clientWidth - 32, (stage.clientHeight - 32) * COVER_RATIO)
|
||||
const maskH = maskW / COVER_RATIO
|
||||
const scale = Math.max(maskW / cropState.naturalW, maskH / cropState.naturalH)
|
||||
cropState.scale = scale
|
||||
cropState.x = (stage.clientWidth - cropState.naturalW * scale) / 2
|
||||
cropState.y = (stage.clientHeight - cropState.naturalH * scale) / 2
|
||||
layoutCrop()
|
||||
}
|
||||
|
||||
function openCoverCrop(file, targetInput, hint) {
|
||||
const modal = document.getElementById('cropModal')
|
||||
const img = document.getElementById('cropImage')
|
||||
cropState.targetInput = targetInput
|
||||
cropState.hint = hint
|
||||
const url = URL.createObjectURL(file)
|
||||
img.onload = () => {
|
||||
cropState.naturalW = img.naturalWidth
|
||||
cropState.naturalH = img.naturalHeight
|
||||
URL.revokeObjectURL(url)
|
||||
fitCropImage()
|
||||
modal.classList.add('on')
|
||||
}
|
||||
img.src = url
|
||||
}
|
||||
|
||||
function closeCoverCrop() {
|
||||
document.getElementById('cropModal').classList.remove('on')
|
||||
document.getElementById('cropImage').removeAttribute('src')
|
||||
cropState.targetInput = null
|
||||
}
|
||||
|
||||
function exportCroppedFile() {
|
||||
const stage = document.getElementById('cropStage')
|
||||
const mask = document.getElementById('cropMask')
|
||||
const img = document.getElementById('cropImage')
|
||||
const maskRect = mask.getBoundingClientRect()
|
||||
const stageRect = stage.getBoundingClientRect()
|
||||
const maskLeft = maskRect.left - stageRect.left
|
||||
const maskTop = maskRect.top - stageRect.top
|
||||
const sx = (maskLeft - cropState.x) / cropState.scale
|
||||
const sy = (maskTop - cropState.y) / cropState.scale
|
||||
const sw = maskRect.width / cropState.scale
|
||||
const sh = maskRect.height / cropState.scale
|
||||
const outW = 1500
|
||||
const outH = Math.round(outW / COVER_RATIO)
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = outW
|
||||
canvas.height = outH
|
||||
const ctx = canvas.getContext('2d')
|
||||
ctx.fillStyle = '#111827'
|
||||
ctx.fillRect(0, 0, outW, outH)
|
||||
ctx.drawImage(img, sx, sy, sw, sh, 0, 0, outW, outH)
|
||||
return new Promise((resolve) => {
|
||||
canvas.toBlob((blob) => {
|
||||
resolve(new File([blob], `cover-${Date.now()}.jpg`, { type: 'image/jpeg' }))
|
||||
}, 'image/jpeg', 0.92)
|
||||
})
|
||||
}
|
||||
|
||||
;(function bindCropUi() {
|
||||
const stage = document.getElementById('cropStage')
|
||||
const img = document.getElementById('cropImage')
|
||||
stage.addEventListener('pointerdown', (event) => {
|
||||
cropState.dragging = true
|
||||
cropState.lastX = event.clientX
|
||||
cropState.lastY = event.clientY
|
||||
stage.setPointerCapture(event.pointerId)
|
||||
})
|
||||
stage.addEventListener('pointermove', (event) => {
|
||||
if (!cropState.dragging) return
|
||||
cropState.x += event.clientX - cropState.lastX
|
||||
cropState.y += event.clientY - cropState.lastY
|
||||
cropState.lastX = event.clientX
|
||||
cropState.lastY = event.clientY
|
||||
layoutCrop()
|
||||
})
|
||||
stage.addEventListener('pointerup', () => { cropState.dragging = false })
|
||||
stage.addEventListener('pointercancel', () => { cropState.dragging = false })
|
||||
document.getElementById('cropZoomIn').onclick = () => {
|
||||
cropState.scale *= 1.12
|
||||
layoutCrop()
|
||||
}
|
||||
document.getElementById('cropZoomOut').onclick = () => {
|
||||
cropState.scale /= 1.12
|
||||
layoutCrop()
|
||||
}
|
||||
document.getElementById('cropCancel').onclick = closeCoverCrop
|
||||
document.getElementById('cropConfirm').onclick = async () => {
|
||||
const hint = cropState.hint
|
||||
const input = cropState.targetInput
|
||||
try {
|
||||
if (hint) hint.textContent = '裁剪上传中...'
|
||||
const file = await exportCroppedFile()
|
||||
closeCoverCrop()
|
||||
const uploaded = await uploadMedia(file)
|
||||
if (input) input.value = uploaded.url
|
||||
if (hint) hint.textContent = '封面已裁剪并上传'
|
||||
} catch (error) {
|
||||
if (hint) hint.textContent = error.message
|
||||
alert(error.message)
|
||||
}
|
||||
}
|
||||
window.addEventListener('resize', () => {
|
||||
if (document.getElementById('cropModal').classList.contains('on')) layoutCrop()
|
||||
})
|
||||
})()
|
||||
|
||||
function mediaFields(value) {
|
||||
return `
|
||||
<label>封面图片地址</label>
|
||||
<input name="cover" id="coverUrl" value="${escapeHtml(value.cover || '')}" placeholder="/static/uploads/..." />
|
||||
<label>上传封面图片</label>
|
||||
<label>上传封面图片(可选区域裁剪)</label>
|
||||
<input type="file" id="coverFile" accept="image/png,image/jpeg,image/webp" />
|
||||
<label>视频地址</label>
|
||||
<input name="video" id="videoUrl" value="${escapeHtml(value.video || '')}" placeholder="/static/uploads/..." />
|
||||
<label>上传视频</label>
|
||||
<input type="file" id="videoFile" accept="video/mp4,video/webm,video/quicktime" />
|
||||
<div class="muted" id="mediaHint" style="margin-top:8px"></div>
|
||||
<div class="muted" id="mediaHint" style="margin-top:8px">首页封面为宽图,上传时请裁剪出要展示的区域。</div>
|
||||
`
|
||||
}
|
||||
|
||||
@@ -131,16 +321,9 @@
|
||||
if (coverFile) {
|
||||
coverFile.onchange = async (event) => {
|
||||
const file = event.target.files[0]
|
||||
event.target.value = ''
|
||||
if (!file) return
|
||||
try {
|
||||
if (hint) hint.textContent = '图片上传中...'
|
||||
const uploaded = await uploadMedia(file)
|
||||
root.querySelector('#coverUrl').value = uploaded.url
|
||||
if (hint) hint.textContent = '图片已上传'
|
||||
} catch (error) {
|
||||
if (hint) hint.textContent = error.message
|
||||
alert(error.message)
|
||||
}
|
||||
openCoverCrop(file, root.querySelector('#coverUrl'), hint)
|
||||
}
|
||||
}
|
||||
if (videoFile) {
|
||||
@@ -211,7 +394,7 @@
|
||||
async function boot() {
|
||||
showApp(true)
|
||||
await loadActivities()
|
||||
await Promise.all([loadStats(), loadPosters(), loadSignups(), loadCodes(), loadStaff()])
|
||||
await Promise.all([loadStats(), loadPosters(), loadSignups(), loadCodes(), loadStaff(), loadSettings()])
|
||||
}
|
||||
|
||||
async function loadStats() {
|
||||
@@ -281,7 +464,7 @@
|
||||
<td class="row">
|
||||
${item.status === 'past'
|
||||
? `<button class="ghost" data-pin="${item.id}" data-pinned="${item.pinned ? 1 : 0}" type="button">${item.pinned ? '取消置顶' : '置顶'}</button>`
|
||||
: ''}
|
||||
: `<button class="ghost" data-remind="${item.id}" type="button">发提醒</button>`}
|
||||
<button class="ghost" data-edit="${item.id}" type="button">编辑</button>
|
||||
<button class="danger" data-del="${item.id}" type="button">删除</button>
|
||||
</td>
|
||||
@@ -335,6 +518,22 @@
|
||||
loadActivities()
|
||||
}
|
||||
})
|
||||
document.querySelectorAll('#activities [data-remind]').forEach((button) => {
|
||||
button.onclick = async () => {
|
||||
if (!confirm('向已同意订阅的报名用户发送活动提醒?')) return
|
||||
try {
|
||||
const result = await api(`/api/admin/activities/${button.dataset.remind}/remind`, {
|
||||
method: 'POST',
|
||||
body: '{}'
|
||||
})
|
||||
alert(result.demo
|
||||
? `演示模式:模拟发送 ${result.sent}/${result.total} 人`
|
||||
: `已发送 ${result.sent}/${result.total} 人${result.failed ? `,失败 ${result.failed}` : ''}`)
|
||||
} catch (error) {
|
||||
alert(error.message)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function loadPosters() {
|
||||
@@ -358,13 +557,9 @@
|
||||
<table><thead><tr><th>海报</th><th>状态</th><th></th></tr></thead><tbody>${rows || '<tr><td colspan="3">暂无海报</td></tr>'}</tbody></table>`
|
||||
document.getElementById('posterFile').onchange = async (event) => {
|
||||
const file = event.target.files[0]
|
||||
event.target.value = ''
|
||||
if (!file) return
|
||||
try {
|
||||
const uploaded = await uploadMedia(file)
|
||||
document.getElementById('posterImage').value = uploaded.url
|
||||
} catch (error) {
|
||||
alert(error.message)
|
||||
}
|
||||
openCoverCrop(file, document.getElementById('posterImage'), null)
|
||||
}
|
||||
document.getElementById('posterForm').onsubmit = async (event) => {
|
||||
event.preventDefault()
|
||||
@@ -385,8 +580,10 @@
|
||||
const rows = signups.map((item) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(item.name)}<div class="muted">${escapeHtml(item.phone)}</div></td>
|
||||
<td>${escapeHtml(item.company || '')}<div class="muted">${escapeHtml(item.position || '')}</div></td>
|
||||
<td>${escapeHtml(item.activityTitle || '')}</td>
|
||||
<td>${escapeHtml(item.sourceSharer || '直接访问')}</td>
|
||||
<td>${item.remindAccepted ? '已订阅' : '未订阅'}</td>
|
||||
<td>${item.checkedIn ? '已签到' : '未签到'}</td>
|
||||
<td><button class="danger" data-signup="${item.id}" type="button">删除</button></td>
|
||||
</tr>`).join('')
|
||||
@@ -394,7 +591,7 @@
|
||||
<div class="row" style="margin-bottom:12px">
|
||||
<a class="primary" style="text-decoration:none" href="/api/admin/signups/export?token=${encodeURIComponent(state.token)}">导出 CSV</a>
|
||||
</div>
|
||||
<table><thead><tr><th>报名人</th><th>活动</th><th>分享人</th><th>签到</th><th></th></tr></thead><tbody>${rows || '<tr><td colspan="5">暂无报名</td></tr>'}</tbody></table>`
|
||||
<table><thead><tr><th>报名人</th><th>公司 / 职位</th><th>活动</th><th>分享人</th><th>提醒</th><th>签到</th><th></th></tr></thead><tbody>${rows || '<tr><td colspan="7">暂无报名</td></tr>'}</tbody></table>`
|
||||
document.querySelectorAll('[data-signup]').forEach((button) => {
|
||||
button.onclick = async () => {
|
||||
await api(`/api/admin/signups/${button.dataset.signup}`, { method: 'DELETE' })
|
||||
@@ -463,6 +660,30 @@
|
||||
})
|
||||
}
|
||||
|
||||
async function loadSettings() {
|
||||
const settings = await api('/api/admin/settings')
|
||||
document.getElementById('settings').innerHTML = `
|
||||
<form id="settingsForm" class="card">
|
||||
<strong>活动提醒(订阅消息)</strong>
|
||||
<p class="muted">
|
||||
1. 微信公众平台 → 功能 → 订阅消息 → 选用活动提醒类模板<br>
|
||||
2. 模板字段建议:thing1 活动名称、time2 活动时间、thing3 活动地点<br>
|
||||
3. 把模板 ID 填到下方;用户报名时会弹出授权,同意后才能收到提醒<br>
|
||||
当前微信密钥:${settings.wxConfigured ? '已配置' : '未配置(演示模式只能模拟发送)'}
|
||||
</p>
|
||||
<label>订阅消息模板 ID</label>
|
||||
<input name="subscribeTmplId" value="${escapeHtml(settings.subscribeTmplId || '')}" placeholder="例如 xxxxxxxxxxxxx" />
|
||||
<div class="row" style="margin-top:12px"><button class="primary" type="submit">保存</button></div>
|
||||
</form>`
|
||||
document.getElementById('settingsForm').onsubmit = async (event) => {
|
||||
event.preventDefault()
|
||||
const body = Object.fromEntries(new FormData(event.target).entries())
|
||||
await api('/api/admin/settings', { method: 'PUT', body: JSON.stringify(body) })
|
||||
alert('已保存')
|
||||
loadSettings()
|
||||
}
|
||||
}
|
||||
|
||||
function escapeHtml(value) {
|
||||
return String(value ?? '').replace(/[&<>"']/g, (char) => ({
|
||||
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
|
||||
|
||||
+67
-1
@@ -3,6 +3,7 @@ const express = require('express')
|
||||
const QRCode = require('qrcode')
|
||||
const db = require('../lib/db')
|
||||
const passwords = require('../lib/passwords')
|
||||
const wechat = require('../lib/wechat')
|
||||
const { uploader } = require('../lib/upload')
|
||||
const { adminAuth } = require('../lib/auth')
|
||||
const { uid, token, ok, fail, wrap, str } = require('../lib/http')
|
||||
@@ -162,6 +163,69 @@ router.delete('/activities/:id', auth, (req, res) => {
|
||||
ok(res, { id: req.params.id })
|
||||
})
|
||||
|
||||
router.get('/settings', auth, (req, res) => {
|
||||
const settings = db.read().settings || {}
|
||||
ok(res, {
|
||||
subscribeTmplId: settings.subscribeTmplId || '',
|
||||
wxConfigured: wechat.wxConfigured()
|
||||
})
|
||||
})
|
||||
|
||||
router.put('/settings', auth, (req, res) => {
|
||||
const subscribeTmplId = str(req.body && req.body.subscribeTmplId, 80)
|
||||
const settings = db.update((data) => {
|
||||
if (!data.settings) data.settings = {}
|
||||
data.settings.subscribeTmplId = subscribeTmplId
|
||||
return data.settings
|
||||
})
|
||||
ok(res, settings)
|
||||
})
|
||||
|
||||
router.post(
|
||||
'/activities/:id/remind',
|
||||
auth,
|
||||
wrap(async (req, res) => {
|
||||
const data = db.read()
|
||||
const activity = data.activities.find((item) => item.id === req.params.id)
|
||||
if (!activity) return fail(res, 404, '活动不存在')
|
||||
const tmplId = str(data.settings && data.settings.subscribeTmplId)
|
||||
if (!tmplId) return fail(res, 400, '请先在后台配置订阅消息模板 ID')
|
||||
|
||||
const targets = data.signups.filter((item) => item.activityId === activity.id && item.remindAccepted)
|
||||
if (!targets.length) return fail(res, 400, '没有可提醒的报名用户(需用户报名时同意订阅)')
|
||||
|
||||
if (!wechat.wxConfigured()) {
|
||||
return ok(res, {
|
||||
demo: true,
|
||||
total: targets.length,
|
||||
sent: targets.length,
|
||||
failed: 0,
|
||||
message: '演示模式:未配置微信密钥,已模拟发送成功'
|
||||
})
|
||||
}
|
||||
|
||||
const page = `pages/detail/detail?id=${activity.id}`
|
||||
const payload = wechat.buildRemindData(activity)
|
||||
let sent = 0
|
||||
const failures = []
|
||||
for (const signup of targets) {
|
||||
try {
|
||||
const result = await wechat.sendSubscribeMessage({
|
||||
openid: signup.openid,
|
||||
templateId: tmplId,
|
||||
page,
|
||||
data: payload
|
||||
})
|
||||
if (result.ok) sent += 1
|
||||
else failures.push({ openid: signup.openid, errmsg: result.errmsg || String(result.errcode) })
|
||||
} catch (error) {
|
||||
failures.push({ openid: signup.openid, errmsg: error.message || '发送失败' })
|
||||
}
|
||||
}
|
||||
ok(res, { demo: false, total: targets.length, sent, failed: failures.length, failures: failures.slice(0, 20) })
|
||||
})
|
||||
)
|
||||
|
||||
router.get('/posters', auth, (req, res) => {
|
||||
ok(res, db.read().posters)
|
||||
})
|
||||
@@ -227,7 +291,7 @@ function csvCell(value) {
|
||||
}
|
||||
|
||||
router.get('/signups/export', adminAuth({ allowQueryToken: true }), (req, res) => {
|
||||
const header = ['姓名', '手机号', '公司', '活动', '分享人', '报名时间', '是否签到', '签到时间']
|
||||
const header = ['姓名', '手机号', '公司', '职位', '活动', '分享人', '订阅提醒', '报名时间', '是否签到', '签到时间']
|
||||
const lines = [header.join(',')]
|
||||
signupRows(str(req.query.activityId)).forEach((item) => {
|
||||
lines.push(
|
||||
@@ -235,8 +299,10 @@ router.get('/signups/export', adminAuth({ allowQueryToken: true }), (req, res) =
|
||||
item.name,
|
||||
item.phone,
|
||||
item.company,
|
||||
item.position || '',
|
||||
item.activityTitle,
|
||||
item.sourceSharer,
|
||||
item.remindAccepted ? '是' : '否',
|
||||
item.createdAt,
|
||||
item.checkedIn ? '是' : '否',
|
||||
item.checkedInAt
|
||||
|
||||
@@ -148,12 +148,24 @@ router.delete('/activity/past/:id', userAuth, requireUserAdmin, (req, res) => {
|
||||
ok(res, { id: removed })
|
||||
})
|
||||
|
||||
router.get('/subscribe/config', (req, res) => {
|
||||
const settings = db.read().settings || {}
|
||||
const tmplId = str(settings.subscribeTmplId)
|
||||
ok(res, {
|
||||
enabled: !!tmplId,
|
||||
tmplIds: tmplId ? [tmplId] : [],
|
||||
notice: '提交即视为同意接收活动提醒'
|
||||
})
|
||||
})
|
||||
|
||||
router.post('/signup', userAuth, (req, res) => {
|
||||
const body = req.body || {}
|
||||
const activityId = str(body.activityId)
|
||||
const name = str(body.name, 20)
|
||||
const phone = str(body.phone)
|
||||
let sourceSharer = str(body.sourceSharer, 64)
|
||||
// accept / reject / ban / filter / none — matches wx.requestSubscribeMessage result values
|
||||
const remindStatus = str(body.remindStatus || 'none', 16) || 'none'
|
||||
if (!activityId || !name || !phone) return fail(res, 400, '请填写姓名和手机号')
|
||||
if (!PHONE_RE.test(phone)) return fail(res, 400, '请填写正确的手机号')
|
||||
if (sourceSharer === req.user.openid) sourceSharer = ''
|
||||
@@ -172,7 +184,10 @@ router.post('/signup', userAuth, (req, res) => {
|
||||
name,
|
||||
phone,
|
||||
company: str(body.company, 40),
|
||||
position: str(body.position, 40),
|
||||
sourceSharer,
|
||||
remindStatus,
|
||||
remindAccepted: remindStatus === 'accept',
|
||||
createdAt: new Date().toISOString(),
|
||||
checkedIn: false,
|
||||
checkedInAt: ''
|
||||
|
||||
@@ -1,3 +1,31 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
// Load local secrets from server/.env (gitignored). Never commit this file.
|
||||
function loadEnv() {
|
||||
const file = path.join(__dirname, '.env')
|
||||
if (!fs.existsSync(file)) return
|
||||
fs.readFileSync(file, 'utf8')
|
||||
.split('\n')
|
||||
.forEach((line) => {
|
||||
const text = line.trim()
|
||||
if (!text || text.startsWith('#')) return
|
||||
const i = text.indexOf('=')
|
||||
if (i < 1) return
|
||||
const key = text.slice(0, i).trim()
|
||||
let value = text.slice(i + 1).trim()
|
||||
if (
|
||||
(value.startsWith('"') && value.endsWith('"')) ||
|
||||
(value.startsWith("'") && value.endsWith("'"))
|
||||
) {
|
||||
value = value.slice(1, -1)
|
||||
}
|
||||
if (!process.env[key]) process.env[key] = value
|
||||
})
|
||||
}
|
||||
|
||||
loadEnv()
|
||||
|
||||
const app = require('./app')
|
||||
const db = require('./lib/db')
|
||||
|
||||
@@ -10,5 +38,7 @@ app.listen(PORT, () => {
|
||||
console.log(`管理后台 http://localhost:${PORT}/admin`)
|
||||
if (!process.env.WX_APPID || !process.env.WX_SECRET) {
|
||||
console.log('未配置 WX_APPID / WX_SECRET,小程序登录走演示模式')
|
||||
} else {
|
||||
console.log(`微信登录已启用 AppID ${process.env.WX_APPID}`)
|
||||
}
|
||||
})
|
||||
|
||||
+16
-1
@@ -87,9 +87,17 @@ test('full flow: admin, past activity, signup, checkin, export', async () => {
|
||||
const guest = await login('device-b')
|
||||
const signup = await call('POST', '/api/signup', {
|
||||
token: guest.token,
|
||||
body: { activityId: '93', name: '张三', phone: '13800000000', company: '=cmd', sourceSharer: user.openid }
|
||||
body: {
|
||||
activityId: '93',
|
||||
name: '张三',
|
||||
phone: '13800000000',
|
||||
company: '=cmd',
|
||||
sourceSharer: user.openid,
|
||||
remindStatus: 'accept'
|
||||
}
|
||||
})
|
||||
assert.strictEqual(signup.status, 200)
|
||||
assert.strictEqual(signup.body.data.remindAccepted, true)
|
||||
const dup = await call('POST', '/api/signup', { token: guest.token, body: { activityId: '93', name: '张三', phone: '13800000000' } })
|
||||
assert.strictEqual(dup.status, 400)
|
||||
|
||||
@@ -101,6 +109,13 @@ test('full flow: admin, past activity, signup, checkin, export', async () => {
|
||||
const stats = await call('GET', '/api/admin/stats', { token: adminToken })
|
||||
assert.strictEqual(stats.body.data.checkinCount, 1)
|
||||
|
||||
await call('PUT', '/api/admin/settings', { token: adminToken, body: { subscribeTmplId: 'tmpl_test_id' } })
|
||||
const config = await call('GET', '/api/subscribe/config')
|
||||
assert.deepStrictEqual(config.body.data.tmplIds, ['tmpl_test_id'])
|
||||
const remind = await call('POST', '/api/admin/activities/93/remind', { token: adminToken, body: {} })
|
||||
assert.strictEqual(remind.status, 200)
|
||||
assert.strictEqual(remind.body.data.sent, 1)
|
||||
|
||||
const csv = await call('GET', `/api/admin/signups/export?token=${adminToken}`)
|
||||
assert.strictEqual(csv.status, 200)
|
||||
assert.match(csv.body, /张三,13800000000,'=cmd/)
|
||||
|
||||
Reference in New Issue
Block a user