Document memo/shop admin routes, deploy scripts, and cache-first feed performance in README. Co-authored-by: Cursor <cursoragent@cursor.com>
549 lines
17 KiB
TypeScript
549 lines
17 KiB
TypeScript
import { useCallback, useEffect, useMemo, useState } from 'react'
|
||
import { api } from '../api'
|
||
import './BookEdit.css'
|
||
|
||
type GoodsType = 'virtual' | 'physical'
|
||
|
||
type Product = {
|
||
id: string
|
||
title: string
|
||
desc: string
|
||
cover: string
|
||
coverEmoji: string
|
||
groupName: string
|
||
goodsType: GoodsType
|
||
priceFen: number
|
||
stock: number
|
||
wxProductId: string
|
||
offerKey?: string
|
||
sort: number
|
||
enabled: boolean
|
||
}
|
||
|
||
type ProductOffer = {
|
||
key: string
|
||
label: string
|
||
name: string
|
||
product_id: string
|
||
price_fen: number
|
||
price_yuan: string
|
||
free: boolean
|
||
image: string
|
||
remark?: string
|
||
}
|
||
|
||
type ShopOrder = {
|
||
id: string
|
||
outTradeNo: string
|
||
openid: string
|
||
productId: string
|
||
productTitle: string
|
||
goodsType: GoodsType
|
||
qty: number
|
||
amountFen: number
|
||
status: string
|
||
email: string
|
||
address?: Record<string, string>
|
||
carmis?: string[]
|
||
trackingNo?: string
|
||
expressCompany?: string
|
||
createdAt: number
|
||
}
|
||
|
||
type Tab = 'products' | 'carmis' | 'orders'
|
||
|
||
const emptyProduct: Product = {
|
||
id: '',
|
||
title: '',
|
||
desc: '',
|
||
cover: '',
|
||
coverEmoji: '🛒',
|
||
groupName: '全部',
|
||
goodsType: 'virtual',
|
||
priceFen: 18800,
|
||
stock: 0,
|
||
wxProductId: '',
|
||
offerKey: '',
|
||
sort: 100,
|
||
enabled: true,
|
||
}
|
||
|
||
export default function Shop() {
|
||
const [tab, setTab] = useState<Tab>('products')
|
||
const [products, setProducts] = useState<Product[]>([])
|
||
const [orders, setOrders] = useState<ShopOrder[]>([])
|
||
const [offers, setOffers] = useState<ProductOffer[]>([])
|
||
const [draft, setDraft] = useState<Product>(emptyProduct)
|
||
const [carmiProductId, setCarmiProductId] = useState('')
|
||
const [carmiText, setCarmiText] = useState('')
|
||
const [shipId, setShipId] = useState('')
|
||
const [trackingNo, setTrackingNo] = useState('')
|
||
const [expressCompany, setExpressCompany] = useState('')
|
||
const [err, setErr] = useState('')
|
||
const [msg, setMsg] = useState('')
|
||
const [busy, setBusy] = useState(false)
|
||
|
||
const load = useCallback(async () => {
|
||
setErr('')
|
||
const [p, o, offerRes] = await Promise.all([
|
||
api<{ items: Product[] }>('/api/admin/shop/products'),
|
||
api<{ items: ShopOrder[] }>('/api/admin/shop/orders'),
|
||
api<{ items: ProductOffer[] }>('/api/admin/product-offers'),
|
||
])
|
||
setProducts(p.items || [])
|
||
setOrders(o.items || [])
|
||
setOffers((offerRes.items || []).filter((x) => !x.free))
|
||
if (!carmiProductId && p.items?.length) {
|
||
const v = p.items.find((x) => x.goodsType === 'virtual')
|
||
if (v) setCarmiProductId(v.id)
|
||
}
|
||
}, [carmiProductId])
|
||
|
||
useEffect(() => {
|
||
load().catch((e) => setErr(e.message || '加载失败'))
|
||
}, [load])
|
||
|
||
const virtualProducts = useMemo(
|
||
() => products.filter((p) => p.goodsType === 'virtual'),
|
||
[products],
|
||
)
|
||
|
||
const activeOfferKey = useMemo(() => {
|
||
if (draft.offerKey) return draft.offerKey
|
||
return (
|
||
offers.find((o) => o.product_id === draft.wxProductId || o.key === draft.wxProductId)?.key ||
|
||
''
|
||
)
|
||
}, [draft.offerKey, draft.wxProductId, offers])
|
||
|
||
function pickOffer(o: ProductOffer) {
|
||
setDraft((d) => ({
|
||
...d,
|
||
offerKey: o.key,
|
||
wxProductId: o.product_id,
|
||
priceFen: o.price_fen,
|
||
}))
|
||
}
|
||
|
||
async function saveProduct() {
|
||
setBusy(true)
|
||
setMsg('')
|
||
setErr('')
|
||
try {
|
||
await api('/api/admin/shop/products', {
|
||
method: 'POST',
|
||
body: JSON.stringify(draft),
|
||
})
|
||
setMsg(
|
||
draft.wxProductId
|
||
? `已保存 · 道具 ${draft.wxProductId} · ¥${(draft.priceFen / 100).toFixed(0)}`
|
||
: '商品已保存(未选道具)',
|
||
)
|
||
setDraft(emptyProduct)
|
||
await load()
|
||
} catch (e) {
|
||
setErr((e as Error).message || '保存失败')
|
||
} finally {
|
||
setBusy(false)
|
||
}
|
||
}
|
||
|
||
async function removeProduct(id: string) {
|
||
if (!confirm(`删除商品 ${id}?`)) return
|
||
setBusy(true)
|
||
try {
|
||
await api(`/api/admin/shop/products/${id}`, { method: 'DELETE' })
|
||
await load()
|
||
} catch (e) {
|
||
setErr((e as Error).message || '删除失败')
|
||
} finally {
|
||
setBusy(false)
|
||
}
|
||
}
|
||
|
||
async function importCarmis() {
|
||
const codes = carmiText
|
||
.split(/\r?\n/)
|
||
.map((s) => s.trim())
|
||
.filter(Boolean)
|
||
if (!carmiProductId || !codes.length) {
|
||
setErr('请选择商品并粘贴卡密(一行一个)')
|
||
return
|
||
}
|
||
setBusy(true)
|
||
setErr('')
|
||
setMsg('')
|
||
try {
|
||
const res = await api<{ imported: number; stock: number }>('/api/admin/shop/carmis', {
|
||
method: 'POST',
|
||
body: JSON.stringify({ productId: carmiProductId, codes }),
|
||
})
|
||
setMsg(`已导入 ${res.imported} 条,当前库存 ${res.stock}`)
|
||
setCarmiText('')
|
||
await load()
|
||
} catch (e) {
|
||
setErr((e as Error).message || '导入失败')
|
||
} finally {
|
||
setBusy(false)
|
||
}
|
||
}
|
||
|
||
async function ship(orderId: string) {
|
||
setBusy(true)
|
||
setErr('')
|
||
setMsg('')
|
||
try {
|
||
await api(`/api/admin/shop/orders/${orderId}/ship`, {
|
||
method: 'POST',
|
||
body: JSON.stringify({ trackingNo, expressCompany, shipNote: '' }),
|
||
})
|
||
setMsg('已标记发货')
|
||
setShipId('')
|
||
setTrackingNo('')
|
||
setExpressCompany('')
|
||
await load()
|
||
} catch (e) {
|
||
setErr((e as Error).message || '发货失败')
|
||
} finally {
|
||
setBusy(false)
|
||
}
|
||
}
|
||
|
||
return (
|
||
<div className="page">
|
||
<header className="page-head">
|
||
<div>
|
||
<h1>电商</h1>
|
||
<p>付费方案与 VIP / 小册相同(已发布虚拟支付道具);每个商品单独选道具,勿共用同一道具。</p>
|
||
</div>
|
||
</header>
|
||
|
||
{err ? <div className="banner err">{err}</div> : null}
|
||
{msg ? <div className="banner ok">{msg}</div> : null}
|
||
|
||
<div className="panel" style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
|
||
{(
|
||
[
|
||
['products', '商品'],
|
||
['carmis', '卡密'],
|
||
['orders', '订单'],
|
||
] as const
|
||
).map(([k, label]) => (
|
||
<button
|
||
key={k}
|
||
type="button"
|
||
className={tab === k ? 'primary-btn' : 'ghost-btn'}
|
||
onClick={() => setTab(k)}
|
||
>
|
||
{label}
|
||
</button>
|
||
))}
|
||
</div>
|
||
|
||
{tab === 'products' ? (
|
||
<>
|
||
<div className="panel monetize-panel">
|
||
<h2 style={{ marginTop: 0 }}>{draft.id ? `编辑 ${draft.id}` : '新建 / 编辑商品'}</h2>
|
||
<p className="hint">先点选付费方案(道具 + 价格),再填写标题与类型后保存。</p>
|
||
|
||
<div className="offer-grid offer-grid-6" style={{ marginBottom: 16 }}>
|
||
{offers.map((o) => {
|
||
const active = o.key === activeOfferKey
|
||
const img = o.image ? `/${o.image.replace(/^\/+/, '')}` : ''
|
||
return (
|
||
<button
|
||
key={o.key}
|
||
type="button"
|
||
className={`offer-card ${active ? 'active' : ''}`}
|
||
disabled={busy}
|
||
onClick={() => pickOffer(o)}
|
||
>
|
||
{img ? (
|
||
<img className="offer-img" src={img} alt={o.label} />
|
||
) : (
|
||
<div className="offer-free-badge">{o.product_id.slice(0, 6)}</div>
|
||
)}
|
||
<strong>{o.label}</strong>
|
||
<span className="offer-id">{o.product_id}</span>
|
||
<em>{o.price_yuan}</em>
|
||
{o.remark ? <i className="offer-remark">{o.remark}</i> : null}
|
||
{active ? <b className="offer-on">已选</b> : null}
|
||
</button>
|
||
)
|
||
})}
|
||
</div>
|
||
|
||
<div className="unlock-hero monetize-unlock" style={{ marginBottom: 16 }}>
|
||
<div className="price-box mono-box" style={{ width: '100%' }}>
|
||
<div>
|
||
<label>道具 ID</label>
|
||
<code>{draft.wxProductId || '—'}</code>
|
||
</div>
|
||
<div>
|
||
<label>价格</label>
|
||
<code>
|
||
¥{(draft.priceFen / 100).toFixed(0)}({draft.priceFen} 分)
|
||
</code>
|
||
</div>
|
||
<div>
|
||
<label>方案</label>
|
||
<code>{draft.offerKey || '—'}</code>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="form-grid">
|
||
<label>
|
||
ID(空则自动)
|
||
<input
|
||
value={draft.id}
|
||
onChange={(e) => setDraft({ ...draft, id: e.target.value })}
|
||
placeholder="yt-device"
|
||
/>
|
||
</label>
|
||
<label>
|
||
标题
|
||
<input
|
||
value={draft.title}
|
||
onChange={(e) => setDraft({ ...draft, title: e.target.value })}
|
||
/>
|
||
</label>
|
||
<label>
|
||
分组
|
||
<input
|
||
value={draft.groupName}
|
||
onChange={(e) => setDraft({ ...draft, groupName: e.target.value })}
|
||
/>
|
||
</label>
|
||
<label>
|
||
类型
|
||
<select
|
||
value={draft.goodsType}
|
||
onChange={(e) =>
|
||
setDraft({ ...draft, goodsType: e.target.value as GoodsType })
|
||
}
|
||
>
|
||
<option value="virtual">虚拟发卡</option>
|
||
<option value="physical">实体发货</option>
|
||
</select>
|
||
</label>
|
||
<label>
|
||
库存(实体手动;虚拟跟卡密)
|
||
<input
|
||
type="number"
|
||
value={draft.stock}
|
||
onChange={(e) => setDraft({ ...draft, stock: Number(e.target.value) || 0 })}
|
||
/>
|
||
</label>
|
||
<label>
|
||
Emoji
|
||
<input
|
||
value={draft.coverEmoji}
|
||
onChange={(e) => setDraft({ ...draft, coverEmoji: e.target.value })}
|
||
/>
|
||
</label>
|
||
<label style={{ gridColumn: '1 / -1' }}>
|
||
描述
|
||
<input
|
||
value={draft.desc}
|
||
onChange={(e) => setDraft({ ...draft, desc: e.target.value })}
|
||
/>
|
||
</label>
|
||
</div>
|
||
<div style={{ display: 'flex', gap: 8, marginTop: 12, flexWrap: 'wrap' }}>
|
||
<label style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
|
||
<input
|
||
type="checkbox"
|
||
checked={draft.enabled}
|
||
onChange={(e) => setDraft({ ...draft, enabled: e.target.checked })}
|
||
/>
|
||
上架
|
||
</label>
|
||
<button type="button" className="primary-btn" disabled={busy} onClick={saveProduct}>
|
||
保存
|
||
</button>
|
||
<button type="button" className="ghost-btn" onClick={() => setDraft(emptyProduct)}>
|
||
清空
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="panel table-wrap">
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>ID</th>
|
||
<th>标题</th>
|
||
<th>类型</th>
|
||
<th>道具</th>
|
||
<th>价格</th>
|
||
<th>库存</th>
|
||
<th>状态</th>
|
||
<th />
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{products.map((p) => (
|
||
<tr key={p.id}>
|
||
<td className="mono">{p.id}</td>
|
||
<td>
|
||
{p.coverEmoji} {p.title}
|
||
</td>
|
||
<td>{p.goodsType === 'virtual' ? '虚拟' : '实体'}</td>
|
||
<td className="mono">{p.wxProductId || '—'}</td>
|
||
<td>¥{(p.priceFen / 100).toFixed(0)}</td>
|
||
<td>{p.stock}</td>
|
||
<td>{p.enabled ? '上架' : '下架'}</td>
|
||
<td>
|
||
<button type="button" className="ghost-btn" onClick={() => setDraft(p)}>
|
||
编辑
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className="ghost-btn"
|
||
onClick={() => removeProduct(p.id)}
|
||
>
|
||
删
|
||
</button>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
{!products.length && (
|
||
<tr>
|
||
<td colSpan={8}>暂无商品</td>
|
||
</tr>
|
||
)}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</>
|
||
) : null}
|
||
|
||
{tab === 'carmis' ? (
|
||
<div className="panel">
|
||
<h2 style={{ marginTop: 0 }}>导入卡密</h2>
|
||
<p style={{ opacity: 0.7 }}>一行一个;导入后自动更新虚拟商品库存。</p>
|
||
<label>
|
||
虚拟商品
|
||
<select value={carmiProductId} onChange={(e) => setCarmiProductId(e.target.value)}>
|
||
<option value="">选择…</option>
|
||
{virtualProducts.map((p) => (
|
||
<option key={p.id} value={p.id}>
|
||
{p.title}(库存 {p.stock} · {p.wxProductId || '无道具'})
|
||
</option>
|
||
))}
|
||
</select>
|
||
</label>
|
||
<textarea
|
||
rows={10}
|
||
style={{ width: '100%', marginTop: 12 }}
|
||
value={carmiText}
|
||
onChange={(e) => setCarmiText(e.target.value)}
|
||
placeholder={'CARD-001\nCARD-002'}
|
||
/>
|
||
<button
|
||
type="button"
|
||
className="primary-btn"
|
||
style={{ marginTop: 12 }}
|
||
disabled={busy}
|
||
onClick={importCarmis}
|
||
>
|
||
导入
|
||
</button>
|
||
</div>
|
||
) : null}
|
||
|
||
{tab === 'orders' ? (
|
||
<div className="panel table-wrap">
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>订单</th>
|
||
<th>商品</th>
|
||
<th>类型</th>
|
||
<th>金额</th>
|
||
<th>状态</th>
|
||
<th>用户/地址/卡密</th>
|
||
<th>发货</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{orders.map((o) => (
|
||
<tr key={o.id}>
|
||
<td className="mono">
|
||
{o.id}
|
||
<div style={{ opacity: 0.6 }}>{o.outTradeNo}</div>
|
||
</td>
|
||
<td>
|
||
{o.productTitle} ×{o.qty}
|
||
</td>
|
||
<td>{o.goodsType === 'virtual' ? '虚拟' : '实体'}</td>
|
||
<td>¥{(o.amountFen / 100).toFixed(2)}</td>
|
||
<td>{o.status}</td>
|
||
<td style={{ maxWidth: 240, fontSize: 12 }}>
|
||
<div className="mono">{o.openid.slice(0, 16)}…</div>
|
||
{o.email ? <div>{o.email}</div> : null}
|
||
{o.address?.userName ? (
|
||
<div>
|
||
{o.address.userName} {o.address.telNumber}
|
||
<br />
|
||
{o.address.provinceName}
|
||
{o.address.cityName}
|
||
{o.address.countyName}
|
||
{o.address.detailInfo}
|
||
</div>
|
||
) : null}
|
||
{o.carmis?.length ? <div>卡密: {o.carmis.join(', ')}</div> : null}
|
||
{o.trackingNo ? (
|
||
<div>
|
||
物流 {o.expressCompany} {o.trackingNo}
|
||
</div>
|
||
) : null}
|
||
</td>
|
||
<td>
|
||
{o.goodsType === 'physical' && o.status === 'paid' ? (
|
||
shipId === o.id ? (
|
||
<div style={{ display: 'grid', gap: 4 }}>
|
||
<input
|
||
placeholder="快递公司"
|
||
value={expressCompany}
|
||
onChange={(e) => setExpressCompany(e.target.value)}
|
||
/>
|
||
<input
|
||
placeholder="运单号"
|
||
value={trackingNo}
|
||
onChange={(e) => setTrackingNo(e.target.value)}
|
||
/>
|
||
<button
|
||
type="button"
|
||
className="primary-btn"
|
||
disabled={busy}
|
||
onClick={() => ship(o.id)}
|
||
>
|
||
确认发货
|
||
</button>
|
||
</div>
|
||
) : (
|
||
<button type="button" className="ghost-btn" onClick={() => setShipId(o.id)}>
|
||
发货
|
||
</button>
|
||
)
|
||
) : (
|
||
'—'
|
||
)}
|
||
</td>
|
||
</tr>
|
||
))}
|
||
{!orders.length && (
|
||
<tr>
|
||
<td colSpan={7}>暂无订单</td>
|
||
</tr>
|
||
)}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
) : null}
|
||
</div>
|
||
)
|
||
}
|