model
app/model/goods_cate.js
module.exports = app => { const mongoose = app.mongoose; const Schema = mongoose.Schema; var d=new Date(); const GoodsCateSchema = new Schema({ title:{ type:String }, cate_img: { type: String }, filter_attr: { type: String }, //筛选id link:{ type: String }, template:{ /*指定当前分类的模板*/ type:String }, pid:{ type:Schema.Types.Mixed //混合类型 }, sub_title:{ /*seo相关的标题 关键词 描述*/ type:String }, keywords:{ type:String }, description:{ type:String }, status: { type: Number,default:1 }, add_time: { type:Number, default: d.getTime() } }); return mongoose.model('GoodsCate', GoodsCateSchema,'goods_cate'); }
router.js
router.get('/admin/goodsCate', controller.admin.goodsCate.index); router.get('/admin/goodsCate/add', controller.admin.goodsCate.add); router.post('/admin/goodsCate/doAdd', controller.admin.goodsCate.doAdd);
增加
controller
app/controller/admin/goodsCate.js
async add(){ var result = await this.ctx.model.GoodsCate.find({'pid':'0'}) await this.ctx.render('admin/goodsCate/add',{ cateList:result }); } async doAdd(){ 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 }) } if(parts.field.pid!=0){ parts.field.pid=this.app.mongoose.Types.ObjectId(parts.field.pid); //调用mongoose里面的方法把字符串转换成ObjectId } let goodsCate =new this.ctx.model.GoodsCate(Object.assign(files,parts.field)); await goodsCate.save(); await this.success('/admin/goodsCate','增加分类成功'); }
view
app/view/admin/goodsCate/add.html
- 一级分类的
pid
是0,顶级分类 - 二级分类的
pid
是_id
,一级分类(如下图的手机/电话卡)
<%- include ../public/page_header.html %>增加商品分类
效果
查找
controller
app/controller/admin/goodsCate.js
- 通过
pid
关联自己查询 - match筛选
pid
为0的数据
async index() { var result=await this.ctx.model.GoodsCate.aggregate([ { $lookup:{ from:'goods_cate', localField:'_id', foreignField:'pid', as:'items' } }, { $match:{ "pid":'0' } } ]) console.log(JSON.stringify(result)); await this.ctx.render('admin/goodsCate/index',{ list:result }); }
view
app/view/admin/goodsCate/index.html
<%- include ../public/page_header.html %>
效果
- 红色为一级分类
- 蓝色为二级分类
上传图片时,生成多种分辨率的图片
安装依赖
cnpm install jimp --save
service
app/service/tools.js
- resize 尺寸
- quality 质量
- write 名字
const Jimp = require("jimp"); //生成缩略图的模块 async jimpImg(target){ //上传图片成功以后生成缩略图 Jimp.read(target, (err, lenna) => { if (err) throw err; lenna.resize(200, 200) // 尺寸 .quality(90) // 质量 .write(target+'_200x200'+path.extname(target)); // save lenna.resize(400, 400) // 尺寸 .quality(90) // 质量 .write(target+'_400x400'+path.extname(target)); // save }); }
controller
app/controller/admin/goodsCate.js 增加一行 this.service.tools.jimpImg(target);
async doAdd(){ 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 }) this.service.tools.jimpImg(target); } if(parts.field.pid!=0){ parts.field.pid=this.app.mongoose.Types.ObjectId(parts.field.pid); //调用mongoose里面的方法把字符串转换成ObjectId } let goodsCate =new this.ctx.model.GoodsCate(Object.assign(files,parts.field)); await goodsCate.save(); await this.success('/admin/goodsCate','增加分类成功'); }
效果
编辑
controller
app/controller/admin/goodsCate.js
async edit() { var id = this.ctx.request.query.id; var result = await this.ctx.model.GoodsCate.find({ "_id": id }); var cateList = await this.ctx.model.GoodsCate.find({ "pid": '0' }); await this.ctx.render('admin/goodsCate/edit', { cateList: cateList, list: result[0] }); } 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 }) //生成缩略图 this.service.tools.jimpImg(target); } if (parts.field.pid != 0) { parts.field.pid = this.app.mongoose.Types.ObjectId(parts.field.pid); //调用mongoose里面的方法把字符串转换成ObjectId } var id = parts.field.id; var updateResult = Object.assign(files, parts.field); await this.ctx.model.GoodsCate.updateOne({ "_id": id }, updateResult); await this.success('/admin/goodsCate', '修改分类成功'); }
view
app/view/admin/goodsCate/edit.html
<%- include ../public/page_header.html %>编辑商品分类
效果
默认选中上级分类
表单最多提交个数
- egg默认设置表单提交个数为
10
-
Reach fields limit (code: Request_fields_limit)
翻译后达到字段限制(代码:Request_fields_limit)
修改config,自定义个数
config/config.default.js
config.multipart = { fields: '50' };