egg(81)–egg之网站信息

37次阅读

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

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

const SettingSchema = new Schema({
site_title: {type: String},
site_logo: {type: String},
site_keywords: {
type: String
},
site_description:{
type: String
},
no_picture:{
type: String
},
site_icp:{
type: String
},
site_tel: {
type: String
},
search_keywords: {
type: String
},
tongji_code: {
type: String
}

});

return mongoose.model(‘Setting’, SettingSchema,’setting’);
}
router.js
router.get(‘/admin/setting’, controller.admin.setting.index);
router.post(‘/admin/setting/doEdit’, controller.admin.setting.doEdit);
controller
app/controller/admin/setting.js
‘use strict’;
const fs=require(‘fs’);

const pump = require(‘mz-modules/pump’);
var BaseController =require(‘./base.js’);

class SettingController extends BaseController {
async index() {

// 提前给 setting 表增加一条数据
var result=await this.ctx.model.Setting.find({});
console.log(JSON.stringify(result))
if(result){
await this.ctx.render(‘admin/setting/index’,{

list:result[0]
});

}else{
await this.ctx.render(‘admin/setting/index’);

}
}

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 updateResult=Object.assign(files,parts.field);

await this.ctx.model.Setting.updateOne({},updateResult);

await this.success(‘/admin/setting’,’ 修改系统设置成功 ’);

}
}

module.exports = SettingController;

view
app/view/admin/setting/index.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/setting/doEdit?_csrf=<%=csrf%>” method=”post” enctype=”multipart/form-data”>
<ul class=”form_input”>

<li> <span> 网站标题:</span> <input type=”text” class=”input” name=”site_title” value=”<%=list.site_title%>”/></li>

<li> <span> 网站 logo:</span> <input type=”file” name=”site_logo”/>
<br />
<span> </span> <img class=”pic” src=”<%=list.site_logo%>” />
</li>

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

<li> <span> 网站描述:</span> <textarea name=”site_description” style=”width:600px;”><%=list.site_description%></textarea></li>

<li> <span> 商品默认图片:</span> <input type=”file” name=”no_picture” />
<br />
<span> </span> <img class=”pic” src=”<%=list.no_picture%>” />

</li>
<li> <span> 备案信息:</span> <input type=”text” class=”input” name=”site_icp” value=”<%=list.site_icp%>”/></li>
<li> <span> 网站电话:</span> <input type=”text” class=”input” name=”site_tel” value=”<%=list.site_tel%>”/></li>

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

<li> <span> 统计代码:</span> <input type=”text” class=”input” name=”tongji_code” value=”<%=list.tongji_code%>”/></li>

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

</ul>

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

</div>

</body>
</html>
效果

正文完
 0