diff --git a/config/go-fly-data.sql b/config/go-fly-data.sql new file mode 100644 index 0000000..e69de29 diff --git a/config/go-fly.sql b/config/go-fly.sql new file mode 100644 index 0000000..1376b87 --- /dev/null +++ b/config/go-fly.sql @@ -0,0 +1,10 @@ +CREATE TABLE `user` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(50) NOT NULL DEFAULT '', + `password` varchar(50) NOT NULL DEFAULT '', + `nickname` varchar(50) NOT NULL DEFAULT '', + `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `deleted_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 \ No newline at end of file diff --git a/controller/auth.go b/controller/auth.go index 39474e1..118fdc5 100644 --- a/controller/auth.go +++ b/controller/auth.go @@ -7,8 +7,6 @@ import ( ) func CheckPass(username string, password string) string { - models.FindUsers() - account := config.GetAccount() if account == nil { account = make(map[string]string) @@ -27,7 +25,13 @@ func CheckPass(username string, password string) string { } return "" } - +func CheckKefuPass(username string, password string) (models.User,bool) { + info:=models.FindUser(username) + if info.Name==""||info.Password!=tools.Md5(password){ + return info,false + } + return info,true +} func AuthLocal(username string, password string) string { account := config.GetAccount() if account == nil { diff --git a/controller/login.go b/controller/login.go index 0f43741..8b6b76b 100644 --- a/controller/login.go +++ b/controller/login.go @@ -1,13 +1,9 @@ package controller import ( - "encoding/json" - "fmt" "github.com/gin-gonic/gin" "github.com/taoshihan1991/imaptool/tools" - "html/template" "log" - "net/http" "time" ) @@ -38,47 +34,36 @@ func LoginCheckPass(c *gin.Context) { }) return } + case "kefulogin": + info,ok:=CheckKefuPass(username, password) + userinfo:= make(map[string]interface{}) + if !ok{ + c.JSON(200, gin.H{ + "code": 400, + "msg": "验证失败", + }) + return + } + userinfo["name"] = info.Name + userinfo["type"] = "kefu" + userinfo["create_time"] = time.Now().Unix() + token, _ := tools.MakeToken(userinfo) + userinfo["ref_token"]=true + refToken, _ := tools.MakeToken(userinfo) c.JSON(200, gin.H{ - "code": 400, - "msg": "验证失败", + "code": 200, + "msg": "验证成功,正在跳转", + "result": gin.H{ + "token": token, + "ref_token":refToken, + "create_time":userinfo["create_time"], + }, }) - } -} -func ActionLogin(w http.ResponseWriter, r *http.Request) { - html := tools.FileGetContent("html/login.html") - t, _ := template.New("login").Parse(html) - t.Execute(w, nil) -} + return -//验证接口 -func LoginCheck(w http.ResponseWriter, r *http.Request) { - w.Header().Set("content-type", "text/json;charset=utf-8;") - msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"}) - authType := r.PostFormValue("type") - password := r.PostFormValue("password") - switch authType { - case "local": - username := r.PostFormValue("username") - sessionId := AuthLocal(username, password) - if sessionId != "" { - tools.SetCookie("session_id", sessionId, &w) - msg, _ = json.Marshal(tools.JsonResult{Code: 200, Msg: "验证成功,正在跳转..."}) - 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 - } - } } - w.Write(msg) + c.JSON(200, gin.H{ + "code": 400, + "msg": "验证失败", + }) } diff --git a/models/users.go b/models/users.go index db1fff0..1957713 100644 --- a/models/users.go +++ b/models/users.go @@ -17,8 +17,8 @@ func CreateUser(name string,password string){ } DB.Create(user) } -func FindUsers()[]User{ - var users []User - DB.Find(&users) - return users +func FindUser(username string)User{ + var user User + DB.Where("name = ?", username).First(&user) + return user } \ No newline at end of file diff --git a/server.go b/server.go index 0451931..58b523f 100644 --- a/server.go +++ b/server.go @@ -43,10 +43,6 @@ func main() { mux.HandleFunc("/", controller.ActionIndex) //邮件夹 mux.HandleFunc("/list", controller.ActionFolder) - //登陆界面 - //mux.HandleFunc("/login", controller.ActionLogin) - //验证接口 - mux.HandleFunc("/check", controller.LoginCheck) //邮件夹接口 mux.HandleFunc("/folders", controller.FoldersList) //新邮件夹接口 diff --git a/static/html/login.html b/static/html/login.html index c171f9c..9597773 100644 --- a/static/html/login.html +++ b/static/html/login.html @@ -53,7 +53,7 @@