diff --git a/docs/api.md b/docs/api.md index a698af9..92a8dfc 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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. 上传对战记录(服务端调用) diff --git a/src/controllers/AccountController.ts b/src/controllers/AccountController.ts index 183d887..25a7eec 100644 --- a/src/controllers/AccountController.ts +++ b/src/controllers/AccountController.ts @@ -103,7 +103,7 @@ export default class AccountController extends BaseController { @router('post /api/:accountid/items') async itemList(req: any) { let { accountid, type } = req.params - let queryData: any = {accountid} + let queryData: any = { accountid } if (type != undefined) { queryData.itemtype = type << 0 } @@ -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 + } } }