SFT/mock-server/routes/merchant.js

47 lines
1.7 KiB
JavaScript

// 商户中心 —— 商户管理路由(/api/merchant/*)。无 detail/approve 接口,与真实契约一致。
import express from 'express'
import { merchants } from '../db/seedMerchant.js'
import { nextId } from '../utils/id.js'
import { paginate } from '../utils/pagination.js'
import { sendOk, sendFail } from '../utils/response.js'
import { requireAuth } from '../middleware/requireAuth.js'
const router = express.Router()
router.use(requireAuth)
router.post('/page', (req, res) => {
const { merchantName, page, pageSize } = req.body || {}
let list = merchants
if (merchantName) list = list.filter((m) => m.merchantName.includes(merchantName))
sendOk(res, paginate(list, page, pageSize))
})
router.post('/create', (req, res) => {
const body = req.body || {}
if (!body.merchantNo) return sendFail(res, '商户编号不能为空', 400)
if (!body.merchantName) return sendFail(res, '商户名称不能为空', 400)
if (merchants.some((m) => m.merchantNo === body.merchantNo)) return sendFail(res, '商户编号已存在', 400)
const id = nextId('merchant')
const entity = { status: 'ACTIVE', remark: '', ...body, id }
merchants.push(entity)
sendOk(res, entity, '新增成功')
})
router.post('/update', (req, res) => {
const { id, ...rest } = req.body || {}
const entity = merchants.find((m) => m.id === id)
if (!entity) return sendFail(res, '商户不存在', 404)
Object.assign(entity, rest)
sendOk(res, true, '修改成功')
})
router.post('/delete', (req, res) => {
const { id } = req.body || {}
const index = merchants.findIndex((m) => m.id === id)
if (index === -1) return sendFail(res, '商户不存在', 404)
merchants.splice(index, 1)
sendOk(res, true, '删除成功')
})
export default router