优化模板部分代码

pull/30/head
taoshihan1991 5 years ago
parent 1ad43c45bd
commit 6cb51f5b70

@ -80,7 +80,7 @@ func view(w http.ResponseWriter, r *http.Request) {
//
//mailServer:=tools.GetMailServerFromCookie(r)
//var wg sync.WaitGroup
var render = new(tools.ViewData)
var render = new(tmpl.ViewHtml)
render.Fid = fid
render.Id = uint32(id)
//wg.Add(1)
@ -110,7 +110,7 @@ func login(w http.ResponseWriter, r *http.Request) {
}
//写信界面
func write(w http.ResponseWriter, r *http.Request) {
render:=new(tools.ViewHtml)
render:=new(tmpl.CommonHtml)
tmpl.RenderWrite(w, render)
}
//验证接口

@ -0,0 +1,9 @@
<el-menu
default-active=""
mode="horizontal">
<el-menu-item index="1" class="mainLogo" v-on:click="openUrl('/list')">GO-IMAP</el-menu-item>
<el-menu-item index="2" v-on:click="openUrl('/list?fid='+fid)">收信<el-badge class="mark" :value="mailTotal" style="margin-bottom: 20px;"/>
</el-menu-item>
<el-menu-item index="3" v-on:click="openUrl('/write')">写信</el-menu-item>
<el-menu-item index="4" v-on:click="openUrl('/logout')">退出</el-menu-item>
</el-menu>

@ -1,71 +1,8 @@
<html lang="cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="陶士涵">
<title>GO-IMAP网页版邮箱imap工具</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/theme-chalk/index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-router/dist/vue-router.js"></script>
<script src="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<style>
html,
body {
height: 100%;
padding: 0;
margin: 0;
}
body {
overflow: hidden;
background-color: #f5f5f5;
}
.el-container{
margin-top:10px;
height: 100%;
}
.el-aside{
height: 100%;
background: #fff;
border: solid 1px #e6e6e6;
}
.el-aside .el-menu{
border-right: none;
}
.mainLogo{
font-size: 20px;
font-weight: bold;
}
.mainMain{
background: #fff;
margin-left: 10px;
margin-bottom: 60px;
}
.el-row{
margin-bottom: 15px;
}
.mainMain .el-col-2{
text-align: right;
font-weight: bold;
}
</style>
</head>
{{.Header}}
<body class="text-center">
<div id="app" style="width:100%">
<template>
<el-menu
default-active="2"
mode="horizontal">
<el-menu-item index="1" class="mainLogo" v-on:click="openUrl('/list')">GO-IMAP</el-menu-item>
<el-menu-item index="2" v-on:click="openUrl('/list?fid='+fid)">收信<el-badge class="mark" :value="mailTotal" style="margin-bottom: 20px;"/>
</el-menu-item>
<el-menu-item index="3">写信</el-menu-item>
<el-header style="text-align: right; font-size: 12px" class="logout">
<a href="/logout">退出</a>
</el-header>
</el-menu>
{{.Nav}}
<el-container v-loading.fullscreen.lock="fullscreenLoading">
<el-aside>
<el-menu

@ -2,15 +2,7 @@
<body class="text-center">
<div id="app" style="width:100%">
<template>
<el-menu
default-active="3"
mode="horizontal">
<el-menu-item index="1" class="mainLogo" v-on:click="openUrl('/list')">GO-IMAP</el-menu-item>
<el-menu-item index="2" v-on:click="openUrl('/list?fid='+fid)">收信<el-badge class="mark" :value="mailTotal" style="margin-bottom: 20px;"/>
</el-menu-item>
<el-menu-item index="3" v-on:click="openUrl('/write')">写信</el-menu-item>
<el-menu-item index="4" v-on:click="openUrl('/logout')">退出</el-menu-item>
</el-menu>
{{.Nav}}
<el-container v-loading.fullscreen.lock="fullscreenLoading">
<el-aside>
<el-menu
@ -23,7 +15,24 @@
</el-aside>
<el-main class="mainMain">
bbb
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
<el-form-item label="smtp地址">
<el-input v-model="form.smtp"></el-input>
</el-form-item>
<el-form-item label="主题">
<el-input v-model="form.subject"></el-input>
</el-form-item>
<el-form-item label="收件人">
<el-input v-model="form.email"></el-input>
</el-form-item>
<el-form-item label="内容">
<el-input type="textarea" v-model="form.content"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('form')">立即发送</el-button>
<el-button @click="resetForm('form')">取消</el-button>
</el-form-item>
</el-form>
</el-main>
</el-container>
@ -38,38 +47,37 @@
data: {
fullscreenLoading:true,
folders:[],
mails:[],
mailTotal:0,
page:1,
pagesize:10,
fid:"",
mailTotal:0,
form: {
smtp: '',
email: '',
subject: '',
content: '',
},
rules: {
smtp: [
{ required: true, message: 'SMTP服务器如"smtp.sina.net:25"包含端口号', trigger: 'blur' },
],
email: [
{ required: true, message: '邮箱地址', trigger: 'blur' },
],
subject: [
{ required: true, message: '主题', trigger: 'blur' },
],
content: [
{ required: true, message: '内容', trigger: 'blur' },
],
},
},
methods: {
//获取邮件夹
getFolders: function (page,fid,isLeft) {
getFolders: function () {
this.fullscreenLoading=true;
if(typeof(page)=="undefined" || page==""){
page=1;
}else{
this.page=page;
}
let data={};
data.page=page;
if(typeof(fid)!="undefined" && fid!=""){
data.fid=fid;
this.fid=fid;
}else if(this.fid!=""){
data.fid=this.fid;
}
let _this = this;
$.get('/folders',data, function (rs) {
if(!isLeft){
_this.folders=rs.result.folders;
}
_this.mails=rs.result.mails
$.get('/folders', function (rs) {
_this.folders=rs.result.folders;
_this.mailTotal=rs.result.total;
_this.pagesize=rs.result.pagesize;
_this.fid=rs.result.fid;
_this.fullscreenLoading=false;
}).then(()=>{
_this.fullscreenLoading=false;
@ -79,6 +87,21 @@
openUrl(url){
window.location.href=url;
},
//提交表单
submitForm(formName){
let _this=this;
this.$refs[formName].validate((valid) => {
if (valid) {
} else {
return false;
}
});
},
//重置表单
resetForm(formName) {
this.loading=false;
this.$refs[formName].resetFields();
},
},
created: function () {
this.getFolders();

@ -0,0 +1,27 @@
package tmpl
import (
"github.com/taoshihan1991/imaptool/tools"
"html/template"
"net/http"
)
type CommonHtml struct{
Header template.HTML
Nav template.HTML
Rw http.ResponseWriter
}
func NewRender(rw http.ResponseWriter)*CommonHtml{
obj:=new(CommonHtml)
obj.Rw=rw
header := tools.FileGetContent("html/header.html")
nav := tools.FileGetContent("html/nav.html")
obj.Header=template.HTML(header)
obj.Nav=template.HTML(nav)
return obj
}
func (obj *CommonHtml)Display(file string,data interface{}){
main := tools.FileGetContent("html/"+file+".html")
t, _ := template.New(file).Parse(main)
t.Execute(obj.Rw, data)
}

@ -13,10 +13,4 @@ func RenderList(w http.ResponseWriter, render interface{}) {
render.(*tools.IndexData).Header=template.HTML(header)
t.Execute(w, render)
}
func RenderWrite(w http.ResponseWriter, render interface{}){
header := tools.FileGetContent("html/header.html")
html := tools.FileGetContent("html/write.html")
t, _ := template.New("write").Parse(html)
render.(*tools.ViewHtml).Header=template.HTML(header)
t.Execute(w, render)
}

@ -1,78 +1,16 @@
package tmpl
import (
"github.com/taoshihan1991/imaptool/tools"
"html/template"
"net/http"
)
func RenderView(w http.ResponseWriter, render interface{}) {
const html = `
<!doctype html>
<html lang="cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Jekyll v3.8.6">
<title>IMAP-</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
body{
padding: 10px;
}
</style>
</head>
<body>
<div class="row">
<div class="col-md-3">
<ul class="list-group">
<li class="list-group-item active"></li>
{{ range $key, $value := .Folders}}
<li class="list-group-item d-flex justify-content-between align-items-center">
<a href="/list?fid={{$key}}">{{$key}}</a>
{{if ne $value 0 }}<span class="badge badge-primary badge-pill">{{$value}}</span>{{end}}
</li>
{{end}}
</ul>
</div>
<div class="col-md-9">
<table class="table table-hover">
<tbody>
<tr>
<th scope="row" width="100">:</th>
<td>{{.Date}}</td>
</tr>
<tr>
<th scope="row">:</th>
<td>{{.From}}</td>
</tr>
<tr>
<th scope="row">:</th>
<td>{{.To}}</td>
</tr>
<tr>
<th scope="row">:</th>
<td>{{.Subject}}</td>
</tr>
<tr>
<th scope="row">:</th>
<td>
{{.HtmlBody}}
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
`
html1 := tools.FileGetContent("html/view.html")
t, _ := template.New("view").Parse(html1)
t.Execute(w, render)
type ViewHtml struct{
CommonHtml
Fid string
Id uint32
}
func RenderView(w http.ResponseWriter, data interface{}) {
render:=NewRender(w)
data.(*ViewHtml).Nav=render.Nav
data.(*ViewHtml).Header=render.Header
render.Display("view",data)
}

@ -0,0 +1,9 @@
package tmpl
import (
"net/http"
)
func RenderWrite(w http.ResponseWriter, data interface{}) {
render:=NewRender(w)
render.Display("write",render)
}

@ -7,6 +7,7 @@ type MailServer struct {
}
type ViewHtml struct{
Header template.HTML
Nav template.HTML
}
type IndexData struct {
ViewHtml

Loading…
Cancel
Save