优化登录页界面和验证逻辑

pull/30/head
taoshihan1991 4 years ago
parent cdc255e629
commit b78b4a136d

@ -191,11 +191,13 @@ func check(w http.ResponseWriter, r *http.Request) {
password := r.PostFormValue("password") password := r.PostFormValue("password")
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"}) msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
w.Header().Set("content-type","text/json") w.Header().Set("content-type","text/json;charset=utf-8;")
if email != "" && server != "" && password != "" { if email != "" && server != "" && password != "" {
res := tools.CheckEmailPassword(server, email, password) res := tools.CheckEmailPassword(server, email, password)
if res { if res {
msg, _ = json.Marshal(tools.JsonResult{Code: 200, Msg: "验证成功"}) msg, _ = json.Marshal(tools.JsonResult{Code: 200, Msg: "验证成功,正在跳转..."})
auth := fmt.Sprintf("%s|%s|%s", server, email, password)
tools.SetCookie("auth",auth,&w)
w.Write(msg) w.Write(msg)
} else { } else {
w.Write(msg) w.Write(msg)

@ -0,0 +1,130 @@
<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/element-ui@2.13.1/lib/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<!-- 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>
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
html,
body {
height: 100%;
}
body {
display: -ms-flexbox;
display: flex;
-ms-flex-align: center;
align-items: center;
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
.form-signin {
width: 100%;
max-width: 400px;
padding: 20px;
margin: auto;
background: #fff;
-webkit-box-shadow: 0 1px 2px 0 rgba(101,129,156,.08);
box-shadow: 0 1px 2px 0 rgba(101,129,156,.08);
}
</style>
</head>
<body class="text-center">
<div id="app" style="width:100%">
<template>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="form-signin">
<h1 class="h3 mb-3 font-weight-normal">邮箱网页版IMAP工具</h1>
<el-form-item prop="server">
<el-input v-model="ruleForm.server" placeholder="IMAP服务器如imap.sina.net:143"></el-input>
</el-form-item>
<el-form-item prop="email">
<el-input v-model="ruleForm.email" placeholder="邮箱地址"></el-input>
</el-form-item>
<el-form-item prop="password">
<el-input v-model="ruleForm.password" placeholder="密码"></el-input>
</el-form-item>
<el-form-item>
<el-button :loading="loading" type="primary" @click="submitForm('ruleForm')">立即登录</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
<p class="mt-5 mb-3 text-muted">&copy; 2020</p>
</template>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
loading:false,
ruleForm:{
server:'',
email:'',
password:'',
},
rules: {
server: [
{ required: true, message: 'IMAP服务器如"imap.sina.net:143"包含端口号', trigger: 'blur' },
],
email: [
{ required: true, message: '邮箱地址', trigger: 'blur' },
],
password: [
{ required: true, message: '邮箱密码', trigger: 'blur' },
],
},
},
methods: {
//提交表单
submitForm(formName){
let _this=this;
this.$refs[formName].validate((valid) => {
if (valid) {
var data={}
data.server=_this.ruleForm.server;
data.email=_this.ruleForm.email;
data.password=_this.ruleForm.password;
_this.loading=true;
$.post("/check",data,function(data){
if(data.code==200){
_this.$message({
message: data.msg,
type: 'success'
});
window.location.href="/";
}else{
_this.$message({
message: data.msg,
type: 'error'
});
}
_this.loading=false;
});
} else {
return false;
}
});
},
//重置表单
resetForm(formName) {
this.loading=false;
this.$refs[formName].resetFields();
},
}
})
</script>
</html>

@ -7,32 +7,20 @@ import (
func RenderLogin(w http.ResponseWriter, render interface{}) { func RenderLogin(w http.ResponseWriter, render interface{}) {
const html = ` const html = `
<!doctype html>
<html lang="cn"> <html lang="cn">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content=""> <meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors"> <meta name="author" content="陶士涵">
<meta name="generator" content="Jekyll v3.8.6"> <title>GO-IMAPimap</title>
<title>IMAP-</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/theme-chalk/index.css"> <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/dist/vue.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/element-ui@2.13.1/lib/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap core CSS --> <!-- 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"> <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> <style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
@media (min-width: 768px) { @media (min-width: 768px) {
.bd-placeholder-img-lg { .bd-placeholder-img-lg {
font-size: 3.5rem; font-size: 3.5rem;
@ -51,35 +39,14 @@ func RenderLogin(w http.ResponseWriter, render interface{}) {
padding-bottom: 40px; padding-bottom: 40px;
background-color: #f5f5f5; background-color: #f5f5f5;
} }
.form-signin { .form-signin {
width: 100%; width: 100%;
max-width: 330px; max-width: 400px;
padding: 15px; padding: 20px;
margin: auto; margin: auto;
} background: #fff;
.form-signin .checkbox { -webkit-box-shadow: 0 1px 2px 0 rgba(101,129,156,.08);
font-weight: 400; box-shadow: 0 1px 2px 0 rgba(101,129,156,.08);
}
.form-signin .form-control {
position: relative;
box-sizing: border-box;
height: auto;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
} }
</style> </style>
@ -87,18 +54,23 @@ func RenderLogin(w http.ResponseWriter, render interface{}) {
<body class="text-center"> <body class="text-center">
<div id="app" style="width:100%"> <div id="app" style="width:100%">
<template> <template>
<div class="form-signin"> <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="form-signin">
<h1 class="h3 mb-3 font-weight-normal">IMAP</h1> <h1 class="h3 mb-3 font-weight-normal">IMAP</h1>
<label for="inputServer" class="sr-only">IMAP:</label> <el-form-item prop="server">
<input type="text" v-model="imapServer" name="server" id="inputServer" class="form-control" placeholder="IMAP服务器如imap.sina.net:143" required autofocus> <el-input v-model="ruleForm.server" placeholder="IMAP服务器如imap.sina.net:143"></el-input>
<label for="inputEmail" class="sr-only">:</label> </el-form-item>
<input type="email" v-model="imapEmail" id="inputEmail" name="email" class="form-control" placeholder="邮箱地址" required autofocus> <el-form-item prop="email">
<label for="inputPassword" class="sr-only">:</label> <el-input v-model="ruleForm.email" placeholder="邮箱地址"></el-input>
<input type="password" v-model="imapPass" name="password" id="inputPassword" class="form-control" placeholder="密码" required> </el-form-item>
{{if .}}<div class="alert alert-danger" role="alert">{{.}}</div>{{end}} <el-form-item prop="password">
<el-button :loading="false" type="primary" v-on:click="checkEmailPass"></el-button> <el-input v-model="ruleForm.password" placeholder="密码"></el-input>
<p class="mt-5 mb-3 text-muted">&copy; 2020</p> </el-form-item>
</div> <el-form-item>
<el-button :loading="loading" type="primary" @click="submitForm('ruleForm')"></el-button>
<el-button @click="resetForm('ruleForm')"></el-button>
</el-form-item>
</el-form>
<p class="mt-5 mb-3 text-muted">&copy; 2020</p>
</template> </template>
</div> </div>
</body> </body>
@ -106,42 +78,66 @@ func RenderLogin(w http.ResponseWriter, render interface{}) {
new Vue({ new Vue({
el: '#app', el: '#app',
data: { data: {
imapEmail:"",
imapServer:"",
imapPass:"",
loading:false, loading:false,
ruleForm:{
server:'',
email:'',
password:'',
},
rules: {
server: [
{ required: true, message: 'IMAP"imap.sina.net:143"', trigger: 'blur' },
],
email: [
{ required: true, message: '', trigger: 'blur' },
],
password: [
{ required: true, message: '', trigger: 'blur' },
],
},
}, },
methods: { methods: {
checkEmailPass: function () { //提交表单
var data={} submitForm(formName){
data.server=this.imapServer; let _this=this;
data.email=this.imapEmail; this.$refs[formName].validate((valid) => {
data.password=this.imapPass; if (valid) {
let _this=this; var data={}
this.loading=true; data.server=_this.ruleForm.server;
$.post("/check",data,function(data){ data.email=_this.ruleForm.email;
if(data.code==200){ data.password=_this.ruleForm.password;
_this.$message({ _this.loading=true;
message: data.msg, $.post("/check",data,function(data){
type: 'success' if(data.code==200){
}); _this.$message({
}else{ message: data.msg,
_this.$message({ type: 'success'
message: data.msg, });
type: 'error' window.location.href="/";
}); }else{
} _this.$message({
_this.loading=false; message: data.msg,
}); type: 'error'
});
} }
_this.loading=false;
});
} else {
return false;
}
});
},
//重置表单
resetForm(formName) {
this.loading=false;
this.$refs[formName].resetFields();
},
} }
}) })
</script> </script>
</html> </html>
` `
t, _ := template.New("list").Parse(html) t, _ := template.New("login").Parse(html)
t.Execute(w, render) t.Execute(w, render)
} }

@ -4,7 +4,13 @@ import (
"net/http" "net/http"
"strings" "strings"
) )
func SetCookie(name string,value string,w *http.ResponseWriter){
cookie := http.Cookie{
Name: name,
Value: value,
}
http.SetCookie(*w, &cookie)
}
func GetCookie(r *http.Request, name string) string { func GetCookie(r *http.Request, name string) string {
cookies := r.Cookies() cookies := r.Cookies()
for _, cookie := range cookies { for _, cookie := range cookies {

Loading…
Cancel
Save