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>