新增本地验证

pull/30/head
陶士涵 4 years ago
parent 2b437a6206
commit e71c095cc8

@ -0,0 +1,25 @@
package config
import (
"encoding/json"
"github.com/taoshihan1991/imaptool/tools"
"io/ioutil"
)
const Dir = "config/"
const AccountConf = Dir +"account.json"
func GetAccount()map[string]string{
var account map[string]string
isExist,_:=tools.IsFileExist(AccountConf)
if !isExist{
return account
}
info,err:=ioutil.ReadFile(AccountConf)
if err!=nil{
return account
}
err=json.Unmarshal(info,&account)
return account
}

@ -0,0 +1,12 @@
package controller
import "github.com/taoshihan1991/imaptool/config"
func AuthLocal(username string,password string)bool{
account:=config.GetAccount()
if username==account["Username"] && password==account["Password"]{
return true
}
return false
}

@ -15,23 +15,31 @@ func ActionLogin(w http.ResponseWriter, r *http.Request){
}
//验证接口
func LoginCheck(w http.ResponseWriter, r *http.Request) {
email := r.PostFormValue("email")
server := r.PostFormValue("server")
password := r.PostFormValue("password")
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
w.Header().Set("content-type", "text/json;charset=utf-8;")
if email != "" && server != "" && password != "" {
res := tools.CheckEmailPassword(server, email, password)
if res {
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
authType := r.PostFormValue("type")
password := r.PostFormValue("password")
switch authType {
case "local":
username := r.PostFormValue("username")
if AuthLocal(username,password){
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)
} else {
w.Write(msg)
return
}
default:
email := r.PostFormValue("email")
server := r.PostFormValue("server")
if email != "" && server != "" && password != "" {
res := tools.CheckEmailPassword(server, email, password)
if res {
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)
return
}
}
} else {
w.Write(msg)
}
w.Write(msg)
}

@ -3,20 +3,19 @@ package controller
import (
"encoding/json"
"fmt"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools"
"io/ioutil"
"net/http"
"os"
)
const configDir = "config/"
const configFile=configDir+"account.json"
func ActionSetting(w http.ResponseWriter, r *http.Request){
render:=tmpl.NewSettingHtml(w)
render.SetLeft("setting_left")
render.SetBottom("setting_bottom")
account:=getAccount()
account:=config.GetAccount()
render.Username=account["Username"]
render.Password=account["Password"]
render.Display("setting",render)
@ -34,11 +33,11 @@ func SettingAccount(w http.ResponseWriter, r *http.Request){
username:=r.PostFormValue("username")
password:=r.PostFormValue("password")
isExist,_:=tools.IsFileExist(configDir)
isExist,_:=tools.IsFileExist(config.Dir)
if !isExist{
os.Mkdir(configDir,os.ModePerm)
os.Mkdir(config.Dir,os.ModePerm)
}
fileConfig:=configFile
fileConfig:=config.AccountConf
file, _ := os.OpenFile(fileConfig, os.O_RDWR|os.O_CREATE, os.ModePerm)
format:=`{
@ -61,24 +60,10 @@ func SettingGetAccount(w http.ResponseWriter, r *http.Request){
w.Write(msg)
return
}
result:=getAccount()
result:=config.GetAccount()
msg, _ := json.Marshal(tools.JsonListResult{
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
Result: result,
})
w.Write(msg)
}
func getAccount()map[string]string{
var account map[string]string
isExist,_:=tools.IsFileExist(configFile)
if !isExist{
return account
}
info,err:=ioutil.ReadFile(configFile)
if err!=nil{
return account
}
err=json.Unmarshal(info,&account)
return account
}

@ -48,15 +48,15 @@
<h1 class="h3 mb-3 font-weight-normal">登录页</h1>
<el-tabs v-model="activeName">
<el-tab-pane label="本地认证" name="first">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm">
<el-form-item prop="email">
<el-input v-model="ruleForm.email" placeholder="邮箱地址"></el-input>
<el-form :model="localAuth" :rules="rules" ref="localAuth">
<el-form-item prop="username">
<el-input v-model="localAuth.username" placeholder="用户名"></el-input>
</el-form-item>
<el-form-item prop="password">
<el-input v-model="ruleForm.password" placeholder="密码"></el-input>
<el-input v-model="localAuth.password" placeholder="密码"></el-input>
</el-form-item>
<el-form-item>
<el-button :loading="loading" type="primary" @click="">本地验证</el-button>
<el-button :loading="loading" type="primary" @click="checkLocal('localAuth')">本地验证</el-button>
</el-form-item>
</el-form>
</el-tab-pane>
@ -105,6 +105,10 @@
window:window,
activeName:"first",
loading:false,
localAuth:{
username:'',
password:'',
},
ruleForm:{
server:'',
email:'',
@ -117,8 +121,11 @@
email: [
{ required: true, message: '邮箱地址', trigger: 'blur' },
],
username: [
{ required: true, message: '用户名不能为空', trigger: 'blur' },
],
password: [
{ required: true, message: '邮箱密码', trigger: 'blur' },
{ required: true, message: '密码不能为空', trigger: 'blur' },
],
},
},
@ -157,6 +164,36 @@
resetForm(formName) {
this.loading=false;
this.$refs[formName].resetFields();
},
//本地验证
checkLocal(formName){
let _this=this;
this.$refs[formName].validate((valid) => {
if (valid) {
var data={}
data.type="local";
data.username=_this.localAuth.username;
data.password=_this.localAuth.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;
}
});
},
}
})

@ -0,0 +1,8 @@
package session
func Set(name string,value string){
}
func Get(name string){
}
Loading…
Cancel
Save