增加优惠券开始分享接口

This commit is contained in:
zhl 2021-07-02 20:39:20 +08:00
parent 2c37fd620e
commit ee759d6d48
4 changed files with 116 additions and 10 deletions

View File

@ -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
{
}
```

View 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 {}
}
}

View File

@ -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}
*/

View File

@ -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()