egg(77)–egg之文章分类curd

46次阅读

共计 8214 个字符,预计需要花费 21 分钟才能阅读完成。

model

app/model/article_cate.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;

var d=new Date();
const ArticleCateSchema = new Schema({
title: {type: String},
cate_img: {type: String},
link:{
type: String
},
pid:{
type:Schema.Types.Mixed // 混合类型
},
sub_title: {type: String}, /*seo 相关的标题 关键词 描述 */
keywords: {type: String},
description: {type: String},
status: {type: Number,default:1},
sort: {type: Number,default:100},
add_time: {
type:Number,
default: d.getTime()
}

});

return mongoose.model(‘ArticleCate’,ArticleCateSchema,’article_cate’);
}
router.js
router.get(‘/admin/articleCate’, controller.admin.articleCate.index);
router.get(‘/admin/articleCate/add’, controller.admin.articleCate.add);
router.get(‘/admin/articleCate/edit’, controller.admin.articleCate.edit);
router.post(‘/admin/articleCate/doEdit’, controller.admin.articleCate.doEdit);
router.post(‘/admin/articleCate/doAdd’, controller.admin.articleCate.doAdd);
controller
app/controller/admin/articleCate.js

const fs=require(‘fs’);

const pump = require(‘mz-modules/pump’);

var BaseController =require(‘./base.js’);
class ArticleCateController extends BaseController {
async index() {

var result=await this.ctx.model.ArticleCate.aggregate([

{
$lookup:{
from:’article_cate’,
localField:’_id’,
foreignField:’pid’,
as:’items’
}
},
{
$match:{
“pid”:’0′
}
}

])

await this.ctx.render(‘admin/articleCate/index’,{

list:result
});

}
async add() {

var result=await this.ctx.model.ArticleCate.find({“pid”:’0′});

await this.ctx.render(‘admin/articleCate/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
})

// 生成缩略图
this.service.tools.jimpImg(target);

}

if(parts.field.pid!=0){
parts.field.pid=this.app.mongoose.Types.ObjectId(parts.field.pid); // 调用 mongoose 里面的方法把字符串转换成 ObjectId

}

let articleCate =new this.ctx.model.ArticleCate(Object.assign(files,parts.field));
await articleCate.save();

await this.success(‘/admin/articleCate’,’ 增加分类成功 ’);

}

async edit() {

var id=this.ctx.request.query.id;

var result=await this.ctx.model.ArticleCate.find({“_id”:id});

var cateList=await this.ctx.model.ArticleCate.find({“pid”:’0′});

await this.ctx.render(‘admin/articleCate/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.ArticleCate.updateOne({“_id”:id},updateResult);

await this.success(‘/admin/articleCate’,’ 修改分类成功 ’);

}

}
module.exports = ArticleCateController;

view
增加
<%- include ../public/page_header.html %>

<div class=”panel panel-default”>
<div class=”panel-heading”>
增加文章分类
</div>
<div class=”panel-body”>
<div class=”table-responsive input-form”>
<form action=”/admin/articleCate/doAdd?_csrf=<%=csrf%>” method=”post” enctype=”multipart/form-data”>
<ul class=”form_input”>

<li> <span> 分类名称:</span> <input type=”text” name=”title” class=”input”/></li>

<li> <span> 上级分类:</span>
<select name=”pid” id=”pid”>
<option value=”0″> 顶级分类 </option>

<%for(var i=0;i<cateList.length;i++){%>
<option value=”<%=cateList[i]._id%>”><%=cateList[i].title%></option>
<%}%>

</select>
</li>

<li> <span> 分类图片:</span> <input type=”file” name=”cate_img”/></li>

<li> <span> 跳转地址:</span> <input type=”text” name=”link” class=”input”/></li>

<li> <span>Seo 标题:</span> <input type=”text” name=”sub_title” class=”input”/></li>

<li> <span>Seo 关键词: </span><input type=”text” name=”keywords” class=”input”/></li>

<li> <span>Seo 描述:</span> <textarea name=”description” id=”description” cols=”84″ rows=”4″></textarea></li>

<li> <span> 排 序:</span> <input type=”text” name=”sort” value=”100″/></li>

<li> <span> 状 态:</span> <input type=”radio” name=”status” checked value=”1″ id=”a”/> <label for=”a”> 显示 </label> <input type=”radio” name=”status” value=”0″ id=”b”/><label for=”b”> 隐藏 </label> </li>

<li>
<br/>
<button type=”submit” class=”btn btn-primary”> 提交 </button>
</li>

</ul>

</form>
</div>
</div>

</div>

</body>
</html>

查找
<%- include ../public/page_header.html %>

<div class=”panel panel-default”>
<div class=”panel-heading clear”>
<span> 文章分类列表 </span> <a href=”/admin/articleCate/add” class=”btn btn-primary fr”> 增加文章分类 </a>
</div>
<div class=”panel-body”>
<!–
列表展示
–>
<div class=”table-responsive”>
<table class=”table table-bordered”>
<thead>
<tr class=”th”>
<th> 分类名称 </th>
<th> 分类图片 </th>
<th class=”text-center”> 排序 </th>
<th class=”text-center”> 状态 </th>

<th class=”text-center”> 操作 </th>
</tr>
</thead>
<tbody>
<%for(var i=0;i<list.length;i++){%>
<tr>
<td><%=list[i].title%></td>

<td><img class=”pic” src=”<%=list[i].cate_img%>” /></td>

<td class=”text-center”><span onclick=”app.editNum(this,’ArticleCate’,’sort’,'<%=list[i]._id%>’)”><%=list[i].sort%></span></td>
<td class=”text-center”>

<%if(list[i].status==1){%>

<img src=”/public/admin/images/yes.gif” onclick=”app.changeStatus(this,’ArticleCate’,’status’,'<%=list[i]._id%>’)” />

<%}else{%>
<img src=”/public/admin/images/no.gif” onclick=”app.changeStatus(this,’ArticleCate’,’status’,'<%=list[i]._id%>’)” />
<%}%>

</td>
<td class=”text-center”> <a href=”/admin/articleCate/edit?id=<%=list[i]._id%>”> 修改 </a> <a class=”delete” href=”/admin/delete?model=ArticleCate&id=<%=list[i]._id%>”> 删除 </a></td>
</tr>

<%for(var j=0;j<list[i].items.length;j++){%>
<tr>
<td>—–<%=list[i].items[j].title%></td>

<td><img class=”pic” src=”<%=list[i].items[j].cate_img%>” /></td>

<td class=”text-center”><span onclick=”app.editNum(this,’ArticleCate’,’sort’,'<%=list[i].items[j]._id%>’)”><%=list[i].items[j].sort%></span></td>
<td class=”text-center”>

<%if(list[i].status==1){%>

<img src=”/public/admin/images/yes.gif” onclick=”app.changeStatus(this,’ArticleCate’,’status’,'<%=list[i].items[j]._id%>’)” />

<%}else{%>
<img src=”/public/admin/images/no.gif” onclick=”app.changeStatus(this,’ArticleCate’,’status’,'<%=list[i].items[j]._id%>’)” />
<%}%>

</td>
<td class=”text-center”> <a href=”/admin/articleCate/edit?id=<%=list[i].items[j]._id%>”> 修改 </a> <a class=”delete” href=”/admin/delete?model=ArticleCate&id=<%=list[i].items[j]._id%>”> 删除 </a></td>
</tr>

<%}%>

<%}%>

</tbody>
</table>
</div>
</div>
</div>

</body>
</html>

编辑
<%- include ../public/page_header.html %>

<div class=”panel panel-default”>
<div class=”panel-heading”>
编辑文章分类
</div>
<div class=”panel-body”>
<div class=”table-responsive input-form”>
<form action=”/admin/articleCate/doEdit?_csrf=<%=csrf%>” method=”post” enctype=”multipart/form-data”>
<ul class=”form_input”>

<input type=”hidden” name=”id” value=”<%=list._id%>” />

<li> <span> 分类名称:</span> <input type=”text” name=”title” class=”input” value=”<%=list.title%>”/></li>

<li> <span> 上级分类:</span>
<select name=”pid” id=”pid”>
<option value=”0″> 顶级分类 </option>

<%for(var i=0;i<cateList.length;i++){%>
<option value=”<%=cateList[i]._id%>” <%if(cateList[i]._id.toString()==list.pid){%> selected <%}%>><%=cateList[i].title%></option>
<%}%>
</select>
</li>

<li> <span> 分类图片:</span> <input type=”file” name=”cate_img”/>

<br />

<span> </span> <img class=”pic” src=”<%=list.cate_img%>” />
</li>

<li> <span> 跳转地址:</span> <input type=”text” name=”link” class=”input” value=”<%=list.link%>” /></li>

<li> <span>Seo 标题:</span> <input type=”text” name=”sub_title” class=”input” value=”<%=list.sub_title%>”/></li>

<li> <span>Seo 关键词: </span><input type=”text” name=”keywords” class=”input” value=”<%=list.keywords%>”/></li>

<li> <span>Seo 描述:</span> <textarea name=”description” id=”description” cols=”84″ rows=”4″><%=list.description%></textarea></li>

<li> <span> 排 序:</span> <input type=”text” name=”sort” value=”<%=list.sort%>”/></li>

<li> <span> 状 态:</span> <input type=”radio” name=”status” <%if(list.status==1){%> checked <%}%> value=”1″ id=”a”/> <label for=”a”> 显示 </label> <input type=”radio” <%if(list.status==0){%> checked <%}%> name=”status” value=”0″ id=”b”/><label for=”b”> 隐藏 </label> </li>

<li>
<br/>
<button type=”submit” class=”btn btn-primary”> 提交 </button>
</li>

</ul>

</form>
</div>
</div>

</div>

</body>
</html>

正文完
 0