增加面向游戏端的优惠券信息接口

This commit is contained in:
zhl 2021-07-06 11:34:37 +08:00
parent ebdfef3b67
commit c19a3475ad
2 changed files with 58 additions and 9 deletions

View File

@ -29,7 +29,7 @@
### 20210705
1. 增加[开始分享优惠券](#223), [领取分享的优惠券](#224) 接口
1. 增加[开始分享优惠券](#223), [领取分享的优惠券](#224), [优惠券详情](#225) 接口
2. [用户券列表](#211), [抽奖](#214), [邀请奖励信息](#215), [领取邮件附件](#217)等接口返回的优惠券信息, 增加expire字段
3. [获取店铺信息](#208) 的gamecfg增加返回 default_desc_txt, 用于首页介绍文字
@ -865,6 +865,7 @@
| 字段 | 说明 |
| -------- | -------------------------------------- |
| id | 优惠券的短id |
| shop | 优惠券的所属店铺 |
| sender | 分享者的accountId |
3. Response: JSON
@ -880,4 +881,38 @@
status: '状态', //0: 未使用 , 1: 已使用 9: 已过期
expire: 0 // 过期时间, 0表示永不过期
}
```
### 25. 优惠券详情
1. Method: POST
2. <span id="225">URI: /api/:accountId/coupon/info</span>
| 字段 | 说明 |
| -------- | -------------------------------------- |
| accountid | 优惠券拥有者的帐号id |
> POST参数
| 字段 | 说明 |
| -------- | -------------------------------------- |
| id | 优惠券的短id |
| shop | 优惠券的所属店铺 |
3. Response: JSON
```js
{
id: '记录id',
shop: '店铺id',
shopName: '店铺名',
coupon: '券id',
couponName: '券名',
couponUrl: '券图片url',
status: '状态', //0: 未使用 , 1: 已使用 2: 赠送中, 3: 已赠送(已被领取) ,9: 已过期
expire: 0, // 过期时间, 0表示永不过期
user: '拥有者的昵称',
userAvatar: '拥有者的头像'
}
```

View File

@ -5,6 +5,7 @@ import { ZError } from '../../common/ZError'
import { Shop } from '../../models/shop/Shop'
import { Coupon } from '../../models/shop/Coupon'
import { getCouponUrl } from '../../services/File'
import { GameUser } from 'models/user/GameUser'
class CouponController extends BaseController {
/**
@ -13,12 +14,17 @@ class CouponController extends BaseController {
*/
@role('anon')
@router('get /pub/coupon/:shop/:id')
@router('post /api/:accountId/coupon/info')
async info(req: any) {
const { id, shop } = req.params
if (!id || !shop) {
throw new ZError(10, '缺少必要参数')
}
const record = await UserCoupon.findOne({ sid: id, shop })
const shopData = await Shop.fetchByID(shop)
if (!shopData) {
throw new ZError(12, '无法找到对应的店铺信息')
}
const record = await UserCoupon.findOne({ sid: id, shop: shopData.id })
if (!record) {
throw new ZError(11, 'no matched records')
}
@ -29,8 +35,8 @@ class CouponController extends BaseController {
await record.save()
}
}
const shopData = await Shop.fetchByID(record.shop)
const coupon = await Coupon.findById(record.item)
const user = await GameUser.getByAccountID(record.accountId)
return {
id: record.sid,
shop: record.shop,
@ -40,6 +46,8 @@ class CouponController extends BaseController {
couponUrl: getCouponUrl(shopData.id, record.item),
status: record.status,
expire: record.expire,
user: user.nickname,
userAvatar: user.avatar,
}
}
/**
@ -93,11 +101,18 @@ class CouponController extends BaseController {
@role('anon')
@router('post /api/:accountId/coupon/receive_coupon')
async receive(req: any) {
const { accountId, id, sender } = req.params
if (!id || !sender) {
const { accountId, id, sender, shop } = req.params
if (!id || !sender || !shop) {
throw new ZError(10, '缺少必要参数: id')
}
const result = await UserCoupon.updateOne({ sid: id, status: 2, accountId: sender }, { status: 3 })
const shopData = await Shop.fetchByID(shop)
if (!shopData) {
throw new ZError(12, '无法找到对应的店铺信息')
}
const result = await UserCoupon.updateOne(
{ sid: id, status: 2, accountId: sender, shop: shopData.id },
{ status: 3 },
)
if (result.nModified !== 1) {
throw new ZError(11, '未找到符合条件的记录, 可能已被别人领走')
}
@ -108,15 +123,14 @@ class CouponController extends BaseController {
recordNew.source = 'receive'
recordNew.activityId = record.id
await recordNew.save()
const shop = await Shop.fetchByID(record.shop)
const coupon = await Coupon.findById(record.item)
return {
id: recordNew.sid,
shop: recordNew.shop,
shopName: shop.showName,
shopName: shopData.showName,
coupon: recordNew.item,
couponName: coupon.name,
couponUrl: getCouponUrl(shop.id, recordNew.item),
couponUrl: getCouponUrl(shopData.id, recordNew.item),
status: recordNew.status,
expire: recordNew.expire,
}