1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| const mongoose = require('mongoose'); const GoodsModel = mongoose.model('Goods'); const utils = require('../../utils') const Data = utils.data()
class goodsController { static async getGoodsList(ctx, next) { if (!ctx.request.query.currentPage || !ctx.request.query.pageSize) { return next(ctx.throw(400, 'params error')); } const currentPage = parseInt(ctx.request.query.currentPage) const pageSize = parseInt(ctx.request.query.pageSize); let obj = null; try { const query = await utils.getParams(ctx.request.query); const doc = await GoodsModel .find(query) .populate('goodsAttrs', {_id: 0}) .populate('supplier', { password: 0, _id: 0, editTime: 0 }) .skip((currentPage - 1) * pageSize) .limit(pageSize) .sort({'_id': -1}) .exec(); const total = await GoodsModel.count(); obj = JSON.stringify({data: doc, currentPage: currentPage, pageSize: pageSize, pageTotal: total}) } catch (error) { console.log(error) obj = { error: error, msg: 'server error' }; } ctx.body = obj } static async getGoods(ctx, next) { if (!ctx.params.goodsuuid) { return next(ctx.throw(400, 'params error')); } let obj = null; try { const doc = await GoodsModel.findOne({ _id: ctx.params.goodsuuid }, { password: 0, _id: 0 }) .populate('goodsAttrs', {_id: 0}) .populate('supplier', { _id: 0, password: 0, editTime: 0 }); obj = JSON.stringify(doc) } catch (error) { console.log(error) obj = { error: error, msg: 'server error' }; } ctx.body = obj } static async addGoods(ctx, next) { const body = await utils.getParams(ctx.request.body); if (!body.goodsName || !body.goodsSummary || !body.goodsImg || !body.goodsPrice || !body.goodsType || !body.goodsStock || !body.goodsAttrs || !body.supplier) { return next(ctx.throw(400, 'params error')); } let obj = null; try { let userType = await parseInt(utils.getUuid(body.supplier)[1]) body.supplier = await utils.getUuid(body.supplier)[0] if (userType === 2) { const doc = await GoodsModel.create(body); obj = { message: 'addGoods success' } } else { obj = { message: 'userType error' } } } catch (error) { console.log(error) obj = { error: error, msg: 'server error' }; } ctx.body = obj } static async editGoods(ctx, next) { const body = await utils.getParams(ctx.request.body); if (!body.goodsuuid) { return next(ctx.throw(400, 'params error')); } let obj = null; try { const doc = await GoodsModel.update({ _id: body.goodsuuid }, body); obj = { message: 'editGoods success' } } catch (error) { console.log(error) obj = { error: error, msg: 'server error' }; } ctx.body = obj } static async removeGoods(ctx, next) { const body = await utils.getParams(ctx.request.body); const getgoodsdoc = await GoodsModel.find({ '_id': { $in: body.goodsuuids } }) if (!body.goodsuuids || !getgoodsdoc) { return next(ctx.throw(400, 'params error')); } let obj = null; try { const doc = await GoodsModel.remove({ '_id': { $in: body.goodsuuids } }); obj = { message: 'removeGoods success' } } catch (error) { console.log(error) obj = { error: error, msg: 'server error' }; } ctx.body = obj } } module.exports = goodsController;
|