egg(81)--egg之网站信息

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>

效果

clipboard.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值