200 lines
6.6 KiB
JavaScript
200 lines
6.6 KiB
JavaScript
'use strict';
|
||
|
||
// 活动报名云端任务接口(自魔术师云后端移植,契约 {code,msg,data} 保持一致)。
|
||
// 鉴权复用 maixiang 现有 authMiddleware(桌面端钉钉登录 JWT),不再使用魔术师 Key/短信登录。
|
||
|
||
const express = require('express');
|
||
const authMiddleware = require('../middleware/auth');
|
||
const service = require('../services/campaignCloudService');
|
||
|
||
const router = express.Router();
|
||
|
||
function operatorOf(req) {
|
||
return req.user || {};
|
||
}
|
||
|
||
router.get('/me', authMiddleware, async (req, res) => {
|
||
const user = operatorOf(req);
|
||
return res.json(service.ok({
|
||
id: user.sub || user.userId || 1,
|
||
phone: user.phone || null,
|
||
name: user.name || user.username || '',
|
||
nickname: user.name || user.username || '',
|
||
}));
|
||
});
|
||
|
||
router.get('/activity-ids', authMiddleware, async (_req, res) => {
|
||
return res.json(service.ok(service.getSupportedActivityIds()));
|
||
});
|
||
|
||
// ---------- 原价卡大促报名任务 ----------
|
||
|
||
router.post('/original-price-promotion', authMiddleware, async (req, res) => {
|
||
try {
|
||
const data = await service.createPromotionGroup({
|
||
operator: operatorOf(req),
|
||
mallId: req.body?.mallId,
|
||
settings: req.body?.settings,
|
||
goodsList: req.body?.goodsList,
|
||
remark: req.body?.remark,
|
||
});
|
||
return res.json(service.ok(data));
|
||
} catch (error) {
|
||
console.error('[campaign-cloud] create promotion failed:', error);
|
||
return res.status(500).json(service.fail(1, error.message || '创建报名任务失败'));
|
||
}
|
||
});
|
||
|
||
router.post('/original-price-promotion/list', authMiddleware, async (req, res) => {
|
||
try {
|
||
const data = await service.listPromotionGroups({
|
||
operator: operatorOf(req),
|
||
mallId: req.body?.mallId,
|
||
page: req.body?.page,
|
||
limit: req.body?.limit,
|
||
});
|
||
return res.json(service.ok(data));
|
||
} catch (error) {
|
||
console.error('[campaign-cloud] list promotion failed:', error);
|
||
return res.status(500).json(service.fail(1, error.message || '查询报名任务失败'));
|
||
}
|
||
});
|
||
|
||
router.post('/original-price-promotion/task/start', authMiddleware, async (req, res) => {
|
||
try {
|
||
await service.startTask({ operator: operatorOf(req), taskId: req.body?.taskId });
|
||
return res.json(service.ok(null));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '启动任务失败'));
|
||
}
|
||
});
|
||
|
||
router.post('/original-price-promotion/task/complete', authMiddleware, async (req, res) => {
|
||
try {
|
||
await service.completeTask({
|
||
operator: operatorOf(req),
|
||
taskId: req.body?.taskId,
|
||
status: req.body?.status,
|
||
errorMsg: req.body?.errorMsg,
|
||
});
|
||
return res.json(service.ok(null));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '完成任务失败'));
|
||
}
|
||
});
|
||
|
||
router.delete('/original-price-promotion/tasks/clear', authMiddleware, async (req, res) => {
|
||
try {
|
||
const data = await service.clearTerminalTasks({ operator: operatorOf(req), kind: 'promotion' });
|
||
return res.json(service.ok(data));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '清空历史任务失败'));
|
||
}
|
||
});
|
||
|
||
// ---------- 初始化(复制SKU)任务 ----------
|
||
|
||
router.post('/copy-sku', authMiddleware, async (req, res) => {
|
||
try {
|
||
const data = await service.createCopySkuTask({
|
||
operator: operatorOf(req),
|
||
mallId: req.body?.mallId,
|
||
goodsId: req.body?.goodsId,
|
||
});
|
||
return res.json(service.ok(data));
|
||
} catch (error) {
|
||
console.error('[campaign-cloud] create copy-sku failed:', error);
|
||
return res.status(500).json(service.fail(1, error.message || '创建初始化任务失败'));
|
||
}
|
||
});
|
||
|
||
router.post('/copy-sku/list', authMiddleware, async (req, res) => {
|
||
try {
|
||
const data = await service.listCopySkuTasks({
|
||
operator: operatorOf(req),
|
||
mallId: req.body?.mallId,
|
||
page: req.body?.page,
|
||
limit: req.body?.limit,
|
||
});
|
||
return res.json(service.ok(data));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '查询初始化任务失败'));
|
||
}
|
||
});
|
||
|
||
router.post('/copy-sku/task/start', authMiddleware, async (req, res) => {
|
||
try {
|
||
await service.startTask({ operator: operatorOf(req), id: req.body?.id });
|
||
return res.json(service.ok(null));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '启动任务失败'));
|
||
}
|
||
});
|
||
|
||
router.post('/copy-sku/task/complete', authMiddleware, async (req, res) => {
|
||
try {
|
||
await service.completeTask({
|
||
operator: operatorOf(req),
|
||
id: req.body?.id,
|
||
status: req.body?.status,
|
||
errorMsg: req.body?.errorMsg,
|
||
});
|
||
return res.json(service.ok(null));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '完成任务失败'));
|
||
}
|
||
});
|
||
|
||
router.delete('/copy-sku/tasks/clear', authMiddleware, async (req, res) => {
|
||
try {
|
||
const data = await service.clearTerminalTasks({ operator: operatorOf(req), kind: 'copy-sku' });
|
||
return res.json(service.ok(data));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '清空历史任务失败'));
|
||
}
|
||
});
|
||
|
||
// ---------- 商品初始化记录 ----------
|
||
|
||
router.post('/copy-sku/goods-init/report', authMiddleware, async (req, res) => {
|
||
try {
|
||
const data = await service.reportGoodsInit({ operator: operatorOf(req), payload: req.body || {} });
|
||
return res.json(service.ok(data));
|
||
} catch (error) {
|
||
console.error('[campaign-cloud] report goods init failed:', error);
|
||
return res.status(500).json(service.fail(1, error.message || '上报初始化数据失败'));
|
||
}
|
||
});
|
||
|
||
router.post('/copy-sku/goods-init/records', authMiddleware, async (req, res) => {
|
||
try {
|
||
const data = await service.getGoodsInitRecords(req.body?.goodIds);
|
||
return res.json(service.ok(data));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '查询初始化记录失败'));
|
||
}
|
||
});
|
||
|
||
router.get('/copy-sku/goods-init', authMiddleware, async (req, res) => {
|
||
try {
|
||
const data = await service.getGoodsInit(req.query?.goodsId);
|
||
if (!data) {
|
||
return res.json(service.fail(3009, '商品初始化信息不存在'));
|
||
}
|
||
return res.json(service.ok(data));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '查询初始化数据失败'));
|
||
}
|
||
});
|
||
|
||
router.delete('/copy-sku/goods-init', authMiddleware, async (req, res) => {
|
||
try {
|
||
await service.deleteGoodsInit(req.query?.goodsId);
|
||
return res.json(service.ok(null));
|
||
} catch (error) {
|
||
return res.status(500).json(service.fail(1, error.message || '删除初始化数据失败'));
|
||
}
|
||
});
|
||
|
||
module.exports = router;
|