diff --git a/src/admin/controllers/shop_cfg.controller.ts b/src/admin/controllers/shop_cfg.controller.ts new file mode 100644 index 0000000..4b66e62 --- /dev/null +++ b/src/admin/controllers/shop_cfg.controller.ts @@ -0,0 +1,56 @@ +import BaseController from '../../common/base.controller' +import { permission, router } from '../../decorators/router' +import { ZError } from '../../common/ZError' +import { ShopCfg } from '../../models/shop/ShopCfg' + +class ShopCfgController extends BaseController { + @permission('shop:edit') + @router('post /api/shop/lottery') + async lottery_list(req, res) { + let { shop } = req.params + if (!shop) { + throw new ZError(11, 'params mismatch') + } + let data = (await ShopCfg.findOrCreate({ shop })).doc + let cfg = data.lottery_cfgs || [] + return cfg + } + + @permission('shop:edit') + @router('post /api/shop/lottery_update') + async lottery_save(req, res) { + let { shop, cfgs } = req.params + if (!shop) { + throw new ZError(11, 'params mismatch') + } + let data = (await ShopCfg.findOrCreate({ shop })).doc + data.lottery_cfgs = cfgs + await data.save() + return {} + } + + @permission('shop:edit') + @router('post /api/shop/share') + async share_list(req, res) { + let { shop } = req.params + if (!shop) { + throw new ZError(11, 'params mismatch') + } + let data = (await ShopCfg.findOrCreate({ shop })).doc + let cfg = data.share_cfgs || [] + return cfg + } + + @permission('shop:edit') + @router('post /api/shop/share_update') + async share_save(req, res) { + let { shop, cfgs } = req.params + if (!shop) { + throw new ZError(11, 'params mismatch') + } + let data = (await ShopCfg.findOrCreate({ shop })).doc + data.share_cfgs = cfgs + await data.save() + return {} + } +} diff --git a/src/api/controllers/lottery.controller.ts b/src/api/controllers/lottery.controller.ts index e17319a..0201ac2 100644 --- a/src/api/controllers/lottery.controller.ts +++ b/src/api/controllers/lottery.controller.ts @@ -6,7 +6,7 @@ import { ShopCfg } from '../../models/shop/ShopCfg' import { Coupon } from '../../models/shop/Coupon' import { getCouponUrl } from '../../services/File' import { UserItem } from '../../models/user/UserItem' -import { LOTTERY_TICKET } from '../../constants/BaseConst' +import { EMPTY, LOTTERY_TICKET } from '../../constants/BaseConst' import { UserReward } from '../../models/user/UserReward' import { random_prob } from '../../utils/number.util' @@ -89,17 +89,19 @@ class LotteryController extends BaseController { } let idx = random_prob(arr) const reward = cfg.lottery_cfgs[idx] - let obj = await UserReward.saveOneRecord({ - accountId, - shop: shop.id, - itemId: reward.coupon, - count: reward.count, - rewardId: reward._id + '', - activityId: 'lottery', - source: 'lottery', - }) let rewardList = [] - rewardList.push(obj) + if (reward.coupon !== EMPTY) { + let obj = await UserReward.saveOneRecord({ + accountId, + shop: shop.id, + itemId: reward.coupon, + count: reward.count, + rewardId: reward._id + '', + activityId: 'lottery', + source: 'lottery', + }) + rewardList.push(obj) + } return rewardList } } diff --git a/src/constants/BaseConst.ts b/src/constants/BaseConst.ts index e3bf2d8..7608cce 100644 --- a/src/constants/BaseConst.ts +++ b/src/constants/BaseConst.ts @@ -12,3 +12,4 @@ export class BaseConst { // 抽奖券的物品id export const LOTTERY_TICKET = 'lottery_ticket' +export const EMPTY = 'empty' diff --git a/src/models/shop/ShopCfg.ts b/src/models/shop/ShopCfg.ts index 63b5b9f..632b859 100644 --- a/src/models/shop/ShopCfg.ts +++ b/src/models/shop/ShopCfg.ts @@ -5,6 +5,8 @@ import { Severity } from '@typegoose/typegoose/lib/internal/constants' import { Base } from '@typegoose/typegoose/lib/defaultClasses' export class ShareCfgClass extends Base { + @prop() + public id: number /** * 所需人数 * @type {number} @@ -30,6 +32,8 @@ export class ShareCfgClass extends Base { } export class LotteryCfgClass extends Base { + @prop() + public id: number /** * 获取概率 * @type {number}