From e71c095cc8d6f3946c028285c86c9667eb609c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Wed, 3 Jun 2020 23:06:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=9C=AC=E5=9C=B0=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 25 ++++++++++++++++++++ controller/auth.go | 12 ++++++++++ controller/login.go | 36 +++++++++++++++++------------ controller/setting.go | 29 ++++++------------------ static/html/login.html | 49 +++++++++++++++++++++++++++++++++++----- tools/session/session.go | 8 +++++++ 6 files changed, 117 insertions(+), 42 deletions(-) create mode 100644 config/config.go create mode 100644 controller/auth.go create mode 100644 tools/session/session.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..a168fac --- /dev/null +++ b/config/config.go @@ -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 +} diff --git a/controller/auth.go b/controller/auth.go new file mode 100644 index 0000000..afc0d01 --- /dev/null +++ b/controller/auth.go @@ -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 +} + diff --git a/controller/login.go b/controller/login.go index 52c7654..dce9f95 100644 --- a/controller/login.go +++ b/controller/login.go @@ -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) } diff --git a/controller/setting.go b/controller/setting.go index 2077f1f..0f061a8 100644 --- a/controller/setting.go +++ b/controller/setting.go @@ -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 -} \ No newline at end of file diff --git a/static/html/login.html b/static/html/login.html index 021582f..5cc5414 100644 --- a/static/html/login.html +++ b/static/html/login.html @@ -48,15 +48,15 @@

登录页

- - - + + + - + - 本地验证 + 本地验证 @@ -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; + } + }); }, } }) diff --git a/tools/session/session.go b/tools/session/session.go new file mode 100644 index 0000000..641fc8b --- /dev/null +++ b/tools/session/session.go @@ -0,0 +1,8 @@ +package session + +func Set(name string,value string){ + +} +func Get(name string){ + +} \ No newline at end of file