controller
app/controller/admin/goods.js
edit
async edit() {
//获取修改数据的id
var id=this.ctx.request.query.id;
//获取所有的颜色值
var colorResult=await this.ctx.model.GoodsColor.find({});
//获取所有的商品类型
var goodsType=await this.ctx.model.GoodsType.find({});
//获取商品分类
var goodsCate=await this.ctx.model.GoodsCate.aggregate([
{
$lookup:{
from:'goods_cate',
localField:'_id',
foreignField:'pid',
as:'items'
}
},
{
$match:{
"pid":'0'
}
}
])
//获取修改的商品
var goodsResult=await this.ctx.model.Goods.find({'_id':id});
//获取当前商品的颜色
// 5bbb68dcfe498e2346af9e4a,5bbb68effe498e2346af9e4b,5bc067d92e5f889dc864aa96
var colorArrTemp=goodsResult[0].goods_color.split(',');
// console.log(colorArrTemp);
var goodsColorArr=[];
colorArrTemp.forEach((value)=>{
goodsColorArr.push({"_id":value})
})
var goodsColorReulst=await this.ctx.model.GoodsColor.find({
$or:goodsColorArr
})
// console.log(colorReulst);
//获取规格信息
var goodsAttsResult=await this.ctx.model.GoodsAttr.find({"goods_id":goodsResult[0]._id});
var goodsAttsStr='';
goodsAttsResult.forEach(async (val)=>{
if(val.attribute_type==1){
goodsAttsStr+=`${val.attribute_title}: `;
}else if(val.attribute_type==2){
goodsAttsStr+=`${val.attribute_title}: `;
}else{
//获取 attr_value 获取可选值列表
var oneGoodsTypeAttributeResult=await this.ctx.model.GoodsTypeAttribute.find({
_id:val.attribute_id
})
var arr=oneGoodsTypeAttributeResult[0].attr_value.split('\n');
goodsAttsStr+=`${val.attribute_title}: `;
goodsAttsStr+=``;
goodsAttsStr+=``;
}
})
//商品的图库信息
var goodsImageResult=await this.ctx.model.GoodsImage.find({"goods_id":goodsResult[0]._id});
console.log(goodsImageResult);
await this.ctx.render('admin/goods/edit',{
colorResult:colorResult,
goodsType:goodsType,
goodsCate:goodsCate,
goods:goodsResult[0],
goodsAtts:goodsAttsStr,
goodsImage:goodsImageResult,
goodsColor:goodsColorReulst
});
}
doEdit
async doEdit() {
let parts = this.ctx.multipart({ autoFields: true });
let files = {};
let stream;
while ((stream = await parts()) != null) {
if (!stream.filename) {
break;
}
let fieldname = stream.fieldname; //file表单的名字
//上传图片的目录
let dir=await this.service.tools.getUploadFile(stream.filename);
let target = dir.uploadDir;
let writeStream = fs.createWriteStream(target);
await pump(stream, writeStream);
files=Object.assign(files,{
[fieldname]:dir.saveDir
})
}
var formFields=Object.assign(files,parts.field);
//修改商品的id
var goods_id=parts.field.id;
//修改商品信息
await this.ctx.model.Goods.updateOne({"_id":goods_id},formFields);
//修改图库信息 (增加)
var goods_image_list=formFields.goods_image_list;
if(goods_id && goods_image_list){
if(typeof(goods_image_list)=='string'){
goods_image_list=new Array(goods_image_list);
}
for(var i=0;i
view
app/view/admin/goods/edit.html
<%- include ../public/page_header.html %>
<%-goodsAtts%>