增加优惠券开始分享接口
This commit is contained in:
parent
2c37fd620e
commit
ee759d6d48
24
doc/api.md
24
doc/api.md
@ -814,5 +814,29 @@
|
||||
"sendTime": 1623316467010, // 公告发送时间
|
||||
"title": "111", // 公告标题
|
||||
"type": 2 // 公告类型 0: 普通 1: 店铺群发 2: 全体
|
||||
}
|
||||
```
|
||||
|
||||
### 23. 开始分享优惠券
|
||||
|
||||
1. Method: POST
|
||||
2. <span id="222">URI: /api/:accountId/coupon/begin_share</span>
|
||||
|
||||
| 字段 | 说明 |
|
||||
| -------- | -------------------------------------- |
|
||||
| accountid | 帐号id |
|
||||
|
||||
> POST参数
|
||||
|
||||
|
||||
| 字段 | 说明 |
|
||||
| -------- | -------------------------------------- |
|
||||
| id | 优惠券的短id |
|
||||
|
||||
3. Response: JSON
|
||||
|
||||
```js
|
||||
{
|
||||
|
||||
}
|
||||
```
|
48
src/api/controllers/coupon.controller.ts
Normal file
48
src/api/controllers/coupon.controller.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { role, router } from 'decorators/router'
|
||||
import { UserCoupon } from 'models/user/UserCoupon'
|
||||
import BaseController from '../../common/base.controller'
|
||||
import { ZError } from '../../common/ZError'
|
||||
|
||||
class CouponController extends BaseController {
|
||||
/**
|
||||
* 获取优惠券信息
|
||||
*/
|
||||
@role('anon')
|
||||
@router('get /pub/coupon/:shop/:id')
|
||||
async info(req: any) {
|
||||
return {}
|
||||
}
|
||||
/**
|
||||
* 核销
|
||||
*/
|
||||
@role('anon')
|
||||
@router('post /pub/coupon/:shop/:id')
|
||||
async use(req: any) {
|
||||
return {}
|
||||
}
|
||||
/**
|
||||
* 开始分享, 将当前优惠券改为正在分享状态
|
||||
*/
|
||||
@role('anon')
|
||||
@router('post /api/:accountId/coupon/begin_share')
|
||||
async beginShare(req: any) {
|
||||
let { accountId, id } = req.params
|
||||
if (!id) {
|
||||
throw new ZError(10, '缺少必要参数: id')
|
||||
}
|
||||
const result = await UserCoupon.updateOne({ sid: id, status: { $in: [0, 2] }, accountId }, { status: 2 })
|
||||
if (result.nModified !== 1) {
|
||||
throw new ZError(11, 'no matched records')
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* 接受分享出去的优惠券
|
||||
*/
|
||||
@role('anon')
|
||||
@router('post /api/:accountId/coupon/receive_coupon')
|
||||
async receive(req: any) {
|
||||
return {}
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ const nanoid = customAlphabet('2345678abcdefghjkmnpqrstwxy', 10)
|
||||
|
||||
@dbconn()
|
||||
@index({ accountId: 1, shop: 1 }, { unique: false })
|
||||
@index({ sid: 1, shop: 1 }, { unique: true })
|
||||
@index({ accountId: 1, shop: 1, item: 1 }, { unique: false })
|
||||
@modelOptions({ schemaOptions: { collection: 'user_coupons', timestamps: true } })
|
||||
@pre<UserCouponClass>('save', async function () {
|
||||
@ -19,34 +20,49 @@ const nanoid = customAlphabet('2345678abcdefghjkmnpqrstwxy', 10)
|
||||
let sid = ''
|
||||
while (!sid) {
|
||||
sid = nanoid()
|
||||
const records = await UserCoupon.find({ sid }, { _id: 1 }).limit(1)
|
||||
const records = await UserCoupon.find({ sid, shop: this.shop }, { _id: 1 }).limit(1)
|
||||
records.length > 0 && (sid = '')
|
||||
}
|
||||
this.sid = sid
|
||||
}
|
||||
})
|
||||
class UserCouponClass extends BaseModule {
|
||||
/**
|
||||
* 当前拥有者的帐号id
|
||||
*/
|
||||
@prop()
|
||||
public accountId: string
|
||||
|
||||
/**
|
||||
* 所属店铺id
|
||||
*/
|
||||
@prop()
|
||||
public shop: string
|
||||
|
||||
/**
|
||||
* 真实的优惠券id
|
||||
*/
|
||||
@prop()
|
||||
public item: string
|
||||
|
||||
/**
|
||||
* 短id, 用于生成二维码, 店铺唯一
|
||||
*/
|
||||
@prop()
|
||||
public sid: string
|
||||
|
||||
@prop({ default: 0 })
|
||||
public count: number
|
||||
|
||||
/**
|
||||
* 来源
|
||||
*/
|
||||
@prop()
|
||||
public source: string
|
||||
|
||||
/**
|
||||
* 活动or挑战id
|
||||
*/
|
||||
@prop()
|
||||
public activityId: string
|
||||
|
||||
/**
|
||||
* UserReward的记录id
|
||||
*/
|
||||
@prop()
|
||||
public rewardId: string
|
||||
|
||||
@ -54,6 +70,8 @@ class UserCouponClass extends BaseModule {
|
||||
* 状态
|
||||
* 0: 未使用
|
||||
* 1: 已使用
|
||||
* 2: 赠送中
|
||||
* 3: 已赠送
|
||||
* 9: 已过期
|
||||
* @type {number}
|
||||
*/
|
||||
|
@ -134,7 +134,14 @@ class UserRewardClass extends BaseModule {
|
||||
rewardType,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条奖励获取记录
|
||||
* @param history 挑战记录
|
||||
* @param accountId 帐号信息
|
||||
* @param rewardId 奖励id
|
||||
* @param coupon 优惠券id
|
||||
* @param count 数量
|
||||
*/
|
||||
public static async addOneRecord(
|
||||
history: PuzzleSessionClass,
|
||||
accountId: string,
|
||||
@ -165,7 +172,10 @@ class UserRewardClass extends BaseModule {
|
||||
let source = history.type === 1 ? 'activity' : 'exam'
|
||||
return UserReward.rewardGot({ accountId, shop: history.shop, rewardId, activityId: history.activityId, source })
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断特定条件下一个奖励是否已经获取
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
public static async rewardGot({
|
||||
accountId,
|
||||
shop,
|
||||
@ -185,7 +195,13 @@ class UserRewardClass extends BaseModule {
|
||||
).limit(1)
|
||||
return records.length > 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已经获得的奖励奖励信息, 用于判断指定的reward是否已经获得过了
|
||||
* @param accountId 帐号id
|
||||
* @param shop 所属店铺
|
||||
* @param activityId 活动id
|
||||
* @returns {Set<String>} 由reward id组成的Set
|
||||
*/
|
||||
public static async userGotSet(accountId: string, shop: string, activityId: string) {
|
||||
let records = await UserReward.find({ accountId, shop, activityId })
|
||||
let result: Set<string> = new Set()
|
||||
|
Loading…
x
Reference in New Issue
Block a user