增加购买物品的接口

This commit is contained in:
zhl 2021-01-21 19:58:59 +08:00
parent 1945d427a2
commit ba0de2a1e3
2 changed files with 61 additions and 4 deletions

View File

@ -420,6 +420,41 @@
```
### 16. 购买物品
1. Method: POST
2. URI: /api/:accountid/hero/buyitem
| 字段 | 说明 |
| --------- | ------ |
| accountid | 帐号id |
> POST参数
| 字段 | 说明 |
| -------- | -------------------------------------- |
| itemid |需要购买的物品id |
|count|购买数量|
3. Response: JSON
```js
{
"itemid": 1002, //道具id
"buynum": 1, //道具数量
"totalnum": 100, //购买成功后总数量
"priceid": 21231, // 使用代币的id
"pricenum": 20 // 使用代币数量
}
```
## 三. 服务端接口列表
### 1. 上传对战记录(服务端调用)

View File

@ -143,9 +143,31 @@ export default class AccountController extends BaseController {
@router('post /api/:accountid/buyitem')
async buyItem(req: any) {
let {itemid, count} = req.params
return {}
let { itemid, count, accountid } = req.params
let pinfo = ItemCtrl.getItemPrice(itemid << 0)
if (pinfo.id == 0) {
throw new ZError(10, 'item can`t buy')
}
let record = await BagItem.findOne({
accountid,
itemid: pinfo.id
})
if (!record || record.count < pinfo.count * count) {
throw new ZError(11, 'not enough money')
}
record.count -= pinfo.count * count
let newItem = (await BagItem.findOrCreate({ accountid, itemid }))
.doc
newItem.count += count
await newItem.save()
await record.save()
return {
itemid,
buynum: count,
totalnum: newItem.count,
priceid: pinfo.id,
pricenum: pinfo.count
}
}
}