import BaseController from '../../common/base.controller' import { permission, router } from '../../decorators/router' import { Shop } from '../../models/shop/Shop' import { ZError } from '../../common/ZError' import { Game } from '../../models/content/Game' import { generateQrFile } from '../../services/File' import { isObjectId } from '../../utils/string.util' class ShopController extends BaseController { @permission(['shop:read', 'shopman:read']) @router('post /shops') async shopList(req) { let { start, limit, page } = req.params limit = +limit || 10 start = +start || (+page - 1) * limit|| 0 let { opt, sort } = Shop.parseQueryParam(req.params) let articles = await Shop.find(opt) .sort(sort) .skip(start) .limit(limit) let count = await Shop.count(opt) let records = [] for (let record of articles) { records.push(record.toJson()) } return { total: count, start, limit, records } } @permission(['shop:read', 'shopman:read']) @router('get /shop/:id') async detail(req: any) { let { id } = req.params const record = await Shop.findById(id) if (!record) { throw new ZError(11, 'shop not found') } return record.toJson() } @permission('self:read') @router('get /myshop') async detailSelf(req: any) { let admin = req.user if (admin.level === 1) { throw new ZError(12, 'this api not for you') } if (!admin.department) { throw new ZError(13, 'you account has no shop bind') } const record = await Shop.findById(admin.department) if (!record) { throw new ZError(11, 'shop not found') } return record.toJson() } @permission(['shop:edit', 'shopman:edit']) @router('post /shop/save') async save(req: any) { let { _id } = req.params let user = req.user let record if (!_id) { record = new Shop(req.params) record.createdBy = user.id record.show = true let game = await Game.findAvaOne() if (game) { record.gameInfo = {gameid: game.id, versionid: game.versions[0]._id+'' } } } else { record = await Shop.findById(_id) record.updateFromReq(req.params) } await record.save() return record.toJson() } @permission(['shopman:review']) @router('post /shop/publish') async publish(req: any) { let { id, publish } = req.params let record = await Shop.findById(id) if (!record) { throw new ZError(11, 'record not found') } record.publish = publish await record.save() return record.toJson() } @permission(['shop:delete','shopman:delete']) @router('post /shop/:id/delete') async delete(req: any) { let { id } = req.params if (!id) { throw new ZError(11, 'params mismatch') } await Shop.findByIdAndUpdate(id, { $set: { deleted: true, deleteTime: new Date() } }) return {} } @permission('shop:edit') @router('post /shop/gameinfo/save') async updateGameInfo(req: any) { let { shopid, gameid, versionid } = req.params if (!shopid || !gameid || !versionid) { throw new ZError(11, 'params mismatch') } let shop = await Shop.findById(shopid) if (!shop) { throw new ZError(12, 'shop not found') } shop.gameInfo = { gameid, versionid } await shop.save() return {} } @permission('shop:edit') @router('post /shop/gameinfo') async getGameInfo(req: any) { let { shopid } = req.params if (!shopid) { throw new ZError(11, 'params mismatch') } let shop = await Shop.findById(shopid) if (!shop) { throw new ZError(12, 'shop not found') } if (!shop.gameInfo) { let game = await Game.findAvaOne() if (game) { shop.gameInfo = {gameid: game.id, versionid: game.versions[0]._id+'' } await shop.save() } } return { gameid: shop.gameInfo.gameid, versionid: shop.gameInfo.versionid } } @permission('shop:edit') @router('post /shop/gameqr') async getGameQr(req: any) { let { shop, gameId, version } = req.params if (isObjectId(shop)) { let record = await Shop.findById(shop) if (!record) { throw new ZError(12, 'shop not found') } shop = record.sid } const { url } = await generateQrFile({gameId, version, shop}) return { url } } @permission('shop:edit') @router('post /shop/save_qtype') async updateQTypes(req) { let { shopid, qtypes } = req.params if (!shopid) { throw new ZError(11, 'params mismatch') } let shop = await Shop.findById(shopid) if (!shop) { throw new ZError(12, 'shop not found') } shop.qtypes = qtypes await shop.save() return {} } }