@@ -1,6 +1,6 @@
|
||||
web:
|
||||
host: "127.0.0.1"
|
||||
web_port: "8080"
|
||||
port: "8080"
|
||||
tls: false
|
||||
cert_private_path: ""
|
||||
cert_public_path: ""
|
||||
|
||||
@@ -34,6 +34,7 @@ require (
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
|
||||
@@ -65,6 +65,8 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
|
||||
+6
-5
@@ -92,18 +92,19 @@ func main() {
|
||||
|
||||
routers.Def_router(r.Group("/")) //分组路由传递到def_routers。go
|
||||
|
||||
var http_port = models.Wed_configs["host"].(string) + ":" + models.Wed_configs["web_port"].(string)
|
||||
if models.Wed_configs["tls"].(bool) {
|
||||
if models.Wed_configs["cert_public_path"] == "" || models.Wed_configs["cert_private_path"] == "" {
|
||||
var http_port = models.Wed_configs.Host + ":" + models.Wed_configs.Port
|
||||
var gin_port = "0.0.0.0" + ":" + models.Wed_configs.Port
|
||||
if models.Wed_configs.Tls {
|
||||
if models.Wed_configs.Cert_public_path == "" || models.Wed_configs.Cert_private_path == "" {
|
||||
fmt.Printf("需要配置证书路径")
|
||||
return
|
||||
} else {
|
||||
fmt.Println("https://" + http_port)
|
||||
r.RunTLS(http_port, models.Wed_configs["cert_public_path"].(string), models.Wed_configs["cert_private_path"].(string))
|
||||
r.RunTLS(gin_port, models.Wed_configs.Cert_public_path, models.Wed_configs.Cert_private_path)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("http://" + http_port)
|
||||
r.Run(http_port)
|
||||
r.Run(gin_port)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package models
|
||||
|
||||
import "github.com/mitchellh/mapstructure"
|
||||
|
||||
var Configs map[string]interface{}
|
||||
|
||||
var Wed_configs map[string]interface{}
|
||||
var Wed_configs Wed_configs_t
|
||||
|
||||
var Database_configs map[string]interface{}
|
||||
|
||||
@@ -25,7 +27,11 @@ func init() {
|
||||
|
||||
func All_config_init() {
|
||||
//读取web配置
|
||||
Wed_configs = Configs["web"].(map[string]interface{})
|
||||
|
||||
err := mapstructure.Decode(Configs["web"].(map[string]interface{}), &Wed_configs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//初始化数据库
|
||||
Database_init()
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package models
|
||||
|
||||
type Wed_configs_t struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port string `mapstructure:"port"`
|
||||
Tls bool `mapstructure:"tls"`
|
||||
Cert_private_path string `mapstructure:"cert_private_path"`
|
||||
Cert_public_path string `mapstructure:"cert_public_path"`
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"saas/models"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -64,66 +63,70 @@ func V1_user_api(r *gin.RouterGroup) {
|
||||
//返回前端的数据
|
||||
err_msg = "user_api_error"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
//转换传进来的数据
|
||||
var jsonData map[string]interface{}
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err != nil {
|
||||
fmt.Println("解析JSON ERROR:", err)
|
||||
panic(err)
|
||||
}
|
||||
//转换字段
|
||||
newUser := models.User{
|
||||
Name: jsonData["username"].(string),
|
||||
Pass: jsonData["userpass"].(string), // 实际应替换为哈希值
|
||||
// Date 字段无需赋值,数据库会自动填充默认值
|
||||
}
|
||||
//对用户的密码进行哈希替换
|
||||
newUser.Pass = models.Hash_user_pass(newUser.Pass)
|
||||
var jsonData Login_from
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||
//转换字段
|
||||
newUser := models.User{
|
||||
Name: jsonData.Username,
|
||||
Pass: jsonData.Password, // 实际应替换为哈希值
|
||||
// Date 字段无需赋值,数据库会自动填充默认值
|
||||
}
|
||||
|
||||
var user models.User
|
||||
user.Name = newUser.Name
|
||||
if models.DB.Where(&user).First(&user).Error == nil {
|
||||
// 有数据
|
||||
//对用户的密码进行哈希替换
|
||||
newUser.Pass = models.Hash_user_pass(newUser.Pass)
|
||||
|
||||
if user.Pass == newUser.Pass {
|
||||
//成功登录
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
//发送cookie
|
||||
//cookie时间
|
||||
var cookie_time = 0
|
||||
if jsonData["keep_login"].(bool) {
|
||||
cookie_time = models.User_configs["cookie_timeout"].(int)
|
||||
var user models.User
|
||||
user.Name = newUser.Name
|
||||
if models.DB.Where(&user).First(&user).Error == nil {
|
||||
// 有数据
|
||||
|
||||
if user.Pass == newUser.Pass {
|
||||
//成功登录
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
//发送cookie
|
||||
//cookie时间
|
||||
var cookie_time = 0
|
||||
if jsonData.Is_keep_login {
|
||||
cookie_time = models.User_configs["cookie_timeout"].(int)
|
||||
}
|
||||
|
||||
cookie := models.Rand_str_32() //生成32字节cookie
|
||||
//cookie := "testcookie"
|
||||
//fmt.Println(cookie)
|
||||
//将cookie写进数据库
|
||||
new_cookie := models.Cookie{}
|
||||
new_cookie.Domain = models.Wed_configs.Host
|
||||
new_cookie.Name = "user"
|
||||
new_cookie.Value = cookie
|
||||
new_cookie.UserID = user.ID
|
||||
|
||||
//cookie时间
|
||||
new_cookie.CreatedAt = time.Now()
|
||||
new_cookie.UpdatedAt = new_cookie.CreatedAt
|
||||
//计算cookie失效时间
|
||||
new_cookie.ExpiresAt = time.Now().Add(time.Duration(models.User_configs["cookie_timeout"].(int)) * time.Second) //计算过期时间
|
||||
new_cookie.SecureFlag = models.Wed_configs.Tls
|
||||
ctx.SetCookie("user", cookie, cookie_time, "/", models.Wed_configs.Host, models.Wed_configs.Tls, true)
|
||||
|
||||
models.DB.Create(&new_cookie) // 传入指针
|
||||
|
||||
} else {
|
||||
//密码错误
|
||||
err_msg = "user_password_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
cookie := models.Rand_str_32() //生成32字节cookie
|
||||
//cookie := "testcookie"
|
||||
//fmt.Println(cookie)
|
||||
//将cookie写进数据库
|
||||
new_cookie := models.Cookie{}
|
||||
new_cookie.Domain = models.Wed_configs["host"].(string)
|
||||
new_cookie.Name = "user"
|
||||
new_cookie.Value = cookie
|
||||
new_cookie.UserID = user.ID
|
||||
|
||||
//cookie时间
|
||||
new_cookie.CreatedAt = time.Now()
|
||||
new_cookie.UpdatedAt = new_cookie.CreatedAt
|
||||
//计算cookie失效时间
|
||||
new_cookie.ExpiresAt = time.Now().Add(time.Duration(models.User_configs["cookie_timeout"].(int)) * time.Second) //计算过期时间
|
||||
new_cookie.SecureFlag = models.Wed_configs["tls"].(bool)
|
||||
ctx.SetCookie("user", cookie, cookie_time, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
|
||||
models.DB.Create(&new_cookie) // 传入指针
|
||||
|
||||
} else {
|
||||
//密码错误
|
||||
err_msg = "user_password_err"
|
||||
//fmt.Println("用户不存在")
|
||||
err_msg = "user_name_nofind"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
//fmt.Println("用户不存在")
|
||||
err_msg = "user_name_nofind"
|
||||
err_msg = "json_error"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
@@ -151,7 +154,7 @@ func V1_user_api(r *gin.RouterGroup) {
|
||||
cookie.Value = cookie_vel
|
||||
models.DB.Where(&cookie).Delete(&cookie)
|
||||
//删除前端cookie
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs.Host, models.Wed_configs.Tls, true)
|
||||
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package routers
|
||||
|
||||
type Login_from struct {
|
||||
Is_keep_login bool `json:"is_keep_login"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
@@ -68,7 +68,7 @@ func Def_router(r *gin.RouterGroup) {
|
||||
} else {
|
||||
//找不到登录权限?? 可能被封号?
|
||||
//删除前端cookie
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs.Host, models.Wed_configs.Tls, true)
|
||||
}
|
||||
|
||||
} else {
|
||||
@@ -77,12 +77,12 @@ func Def_router(r *gin.RouterGroup) {
|
||||
//删除数据库的cookie
|
||||
models.DB.Delete(&cookie)
|
||||
//删除前端cookie
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs.Host, models.Wed_configs.Tls, true)
|
||||
}
|
||||
} else {
|
||||
//找不到cookie,未登录
|
||||
//删除前端cookie
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs.Host, models.Wed_configs.Tls, true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -161,8 +161,8 @@
|
||||
const url = '/api/v1/user/login';
|
||||
const sumt_data = {
|
||||
username: username_dom.value,
|
||||
userpass: password_dom.value,
|
||||
keep_login: keep_login_dom.checked
|
||||
password: password_dom.value,
|
||||
is_keep_login: keep_login_dom.checked
|
||||
};
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user