增加领取分享的优惠券的接口
This commit is contained in:
parent
ee759d6d48
commit
3484c58254
31
doc/api.md
31
doc/api.md
@ -836,7 +836,36 @@
|
||||
3. Response: JSON
|
||||
|
||||
```js
|
||||
{
|
||||
{}
|
||||
```
|
||||
|
||||
### 24. 领取分享的优惠券
|
||||
|
||||
1. Method: POST
|
||||
2. <span id="222">URI: /api/:accountId/coupon/receive_coupon</span>
|
||||
|
||||
| 字段 | 说明 |
|
||||
| -------- | -------------------------------------- |
|
||||
| accountid | 帐号id |
|
||||
|
||||
> POST参数
|
||||
|
||||
|
||||
| 字段 | 说明 |
|
||||
| -------- | -------------------------------------- |
|
||||
| id | 优惠券的短id |
|
||||
| sender | 分享者的accountId |
|
||||
|
||||
3. Response: JSON
|
||||
|
||||
```js
|
||||
{
|
||||
id: '记录id',
|
||||
shop: '店铺id',
|
||||
shopName: '店铺名',
|
||||
coupon: '券id',
|
||||
couponName: '券名',
|
||||
couponUrl: '券图片url',
|
||||
status: '状态' //0: 未使用 , 1: 已使用 9: 已过期
|
||||
}
|
||||
```
|
@ -2,10 +2,14 @@ import { role, router } from 'decorators/router'
|
||||
import { UserCoupon } from 'models/user/UserCoupon'
|
||||
import BaseController from '../../common/base.controller'
|
||||
import { ZError } from '../../common/ZError'
|
||||
import { Shop } from '../../models/shop/Shop'
|
||||
import { Coupon } from '../../models/shop/Coupon'
|
||||
import { getCouponUrl } from '../../services/File'
|
||||
|
||||
class CouponController extends BaseController {
|
||||
/**
|
||||
* 获取优惠券信息
|
||||
* 用于核销页面获取优惠券信息
|
||||
*/
|
||||
@role('anon')
|
||||
@router('get /pub/coupon/:shop/:id')
|
||||
@ -43,6 +47,31 @@ class CouponController extends BaseController {
|
||||
@role('anon')
|
||||
@router('post /api/:accountId/coupon/receive_coupon')
|
||||
async receive(req: any) {
|
||||
return {}
|
||||
const { accountId, id, sender } = req.params
|
||||
if (!id || !sender) {
|
||||
throw new ZError(10, '缺少必要参数: id')
|
||||
}
|
||||
const result = await UserCoupon.updateOne({ sid: id, status: 2, accountId: sender }, { status: 3 })
|
||||
if (result.nModified !== 1) {
|
||||
throw new ZError(11, '未找到符合条件的记录, 可能已被别人领走')
|
||||
}
|
||||
//TODO: 生成新的优惠券图
|
||||
const record = await UserCoupon.findOne({ sid: id, accountId: sender })
|
||||
const recordNew = record.copy()
|
||||
recordNew.accountId = accountId
|
||||
recordNew.source = 'share'
|
||||
recordNew.activityId = record.id
|
||||
await recordNew.save()
|
||||
const shop = await Shop.fetchByID(record.shop)
|
||||
const coupon = await Coupon.findById(record.item)
|
||||
return {
|
||||
id: record.sid,
|
||||
shop: record.shop,
|
||||
shopName: shop.showName,
|
||||
coupon: record.item,
|
||||
couponName: coupon.name,
|
||||
couponUrl: getCouponUrl(shop.id, record.item),
|
||||
status: record.status,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,8 @@ class UserCouponClass extends BaseModule {
|
||||
@prop()
|
||||
public source: string
|
||||
/**
|
||||
* 活动or挑战id
|
||||
* 活动or挑战id,
|
||||
* 对于分享领取的记录, 该值存被领取的记录的id
|
||||
*/
|
||||
@prop()
|
||||
public activityId: string
|
||||
@ -83,6 +84,15 @@ class UserCouponClass extends BaseModule {
|
||||
return record?.count || 0
|
||||
}
|
||||
|
||||
public copy() {
|
||||
let record = new UserCoupon()
|
||||
record.shop = this.shop
|
||||
record.item = this.item
|
||||
record.count = this.count
|
||||
record.status = 0
|
||||
return record
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一件物品
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user