修改使用物品的接口

This commit is contained in:
zhl 2021-01-21 13:56:15 +08:00
parent a1156888ac
commit d063d16eac
2 changed files with 27 additions and 6 deletions

View File

@ -6,7 +6,7 @@ import { Card } from '../models/subdoc/Card'
import { BaseConst } from '../constants/BaseConst'
import { Hero } from '../models/subdoc/Hero'
import { CardGroup } from '../models/CardGroup'
import { BagItem } from '../models/BagItem'
import { BagItem, ItemType } from '../models/BagItem'
export default class AccountController extends BaseController {
@role('anon')
@ -111,9 +111,28 @@ export default class AccountController extends BaseController {
@router('post /api/:accountid/useitem')
async userItem(req: any) {
let {accountid, items} = req.params;
return {}
let { accountid, itemid, count } = req.params
let record = await BagItem.findOne({accountid, itemid, itemtype: ItemType.UNKNOW})
if (!record) {
throw new ZError(11, 'item not found')
}
if (record.count < count) {
throw new ZError(12, 'not enough item')
}
record.count -= count
//TODO:: 根据使用逻辑获取真实的物品id
let data = [{ itemid: 10001, itemnum: 1 }]
for (let obj of data) {
let item = (await BagItem.findOrCreate({
accountid,
itemid: obj.itemid,
itemtype: ItemType.UNKNOW
})).doc
item.count += obj.itemnum
await item.save()
}
await record.save()
return data
}
}

View File

@ -15,9 +15,11 @@ export default class MailController extends BaseController {
}
if (data.attachments && data.attachments.length > 0) {
for (let obj of data.attachments) {
let item = (await BagItem.findOrCreate({accountid,
let item = (await BagItem.findOrCreate({
accountid,
itemid: obj.itemid,
itemtype: ItemType.UNKNOW })).doc
itemtype: ItemType.UNKNOW
})).doc
item.count += obj.itemnum
await item.save()
}