优化
This commit is contained in:
@@ -89,9 +89,6 @@ func main() {
|
|||||||
ctx.HTML(404, "error_404.html", gin.H{})
|
ctx.HTML(404, "error_404.html", gin.H{})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Use(func(ctx *gin.Context) {
|
|
||||||
routers.Fitst_use(ctx)
|
|
||||||
})
|
|
||||||
routers.Def_router(r.Group("/")) //分组路由传递到def_routers。go
|
routers.Def_router(r.Group("/")) //分组路由传递到def_routers。go
|
||||||
routers.Api_router(r.Group("/api/")) //分组路由传递到api_routers。go
|
routers.Api_router(r.Group("/api/")) //分组路由传递到api_routers。go
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package routers
|
package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -9,11 +10,48 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"saas/models"
|
"saas/models"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mitchellh/mapstructure"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 中间件:打印原始请求数据
|
||||||
|
func RequestLogger() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
// 1. 记录基础信息
|
||||||
|
start := time.Now()
|
||||||
|
reqMethod := c.Request.Method
|
||||||
|
reqURL := c.Request.URL.String()
|
||||||
|
|
||||||
|
// 2. 读取并复制请求体
|
||||||
|
bodyBytes, _ := io.ReadAll(c.Request.Body)
|
||||||
|
defer c.Request.Body.Close()
|
||||||
|
|
||||||
|
// 重要:将 body 内容写回,供后续使用
|
||||||
|
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||||
|
bodyString := string(bodyBytes)
|
||||||
|
if bodyString == "" {
|
||||||
|
bodyString = "[空请求体]"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 打印请求信息
|
||||||
|
fmt.Printf("\n[请求开始] %s\n", start.Format(time.RFC3339))
|
||||||
|
fmt.Printf("[方法] %s\n[URL] %s\n", reqMethod, reqURL)
|
||||||
|
|
||||||
|
// 打印请求头(过滤敏感信息)
|
||||||
|
fmt.Println("[请求头]:")
|
||||||
|
for name, values := range c.Request.Header {
|
||||||
|
// 简化打印,实际使用可添加敏感头过滤
|
||||||
|
fmt.Printf(" %s: %s\n", name, values)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("[请求体]:\n%s\n", bodyString)
|
||||||
|
fmt.Printf("[请求结束] 耗时: %v\n\n", time.Since(start))
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func V1_file_api(r *gin.RouterGroup) {
|
func V1_file_api(r *gin.RouterGroup) {
|
||||||
r.GET("/", func(ctx *gin.Context) {
|
r.GET("/", func(ctx *gin.Context) {
|
||||||
Return_json(ctx, "json_error", nil)
|
Return_json(ctx, "json_error", nil)
|
||||||
@@ -26,31 +64,27 @@ func V1_file_api(r *gin.RouterGroup) {
|
|||||||
|
|
||||||
r.Use(func(ctx *gin.Context) {
|
r.Use(func(ctx *gin.Context) {
|
||||||
fmt.Println("file_api")
|
fmt.Println("file_api")
|
||||||
cookie := ctx.PostForm("cookie")
|
|
||||||
var cookie_t models.Cookie
|
|
||||||
if err := mapstructure.Decode(cookie, &cookie_t); err == nil {
|
|
||||||
if cookie_t.Value != "" {
|
|
||||||
cookie_vel := cookie_t.Value
|
|
||||||
fmt.Println(cookie_vel)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//fmt.Println(cookie)
|
|
||||||
file, err := ctx.FormFile("file")
|
|
||||||
if err == nil {
|
|
||||||
fmt.Println(file)
|
|
||||||
} else {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
//r.Use(RequestLogger())
|
||||||
|
|
||||||
r.POST("/upload", func(ctx *gin.Context) {
|
r.POST("/upload", func(ctx *gin.Context) {
|
||||||
//先判断有没有登录
|
//先判断有没有登录
|
||||||
//获取中间件处理的结果
|
//获取中间件处理的结果
|
||||||
_, is_login := ctx.Get("user_info")
|
_, is_login := ctx.Get("user_info")
|
||||||
if is_login {
|
if is_login {
|
||||||
|
cookie := ctx.PostForm("cookie")
|
||||||
|
fmt.Println(cookie)
|
||||||
|
|
||||||
|
//fmt.Println(cookie)
|
||||||
|
file, err := ctx.FormFile("file")
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println(file)
|
||||||
|
} else {
|
||||||
|
fmt.Println("err:", err)
|
||||||
|
fmt.Println("file:", file)
|
||||||
|
}
|
||||||
Return_json(ctx, "api_ok", nil)
|
Return_json(ctx, "api_ok", nil)
|
||||||
} else {
|
} else {
|
||||||
Return_json(ctx, "user_no_sign", nil)
|
Return_json(ctx, "user_no_sign", nil)
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
package routers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Api_router(r *gin.RouterGroup) {
|
|
||||||
|
|
||||||
r.GET("/", func(ctx *gin.Context) {
|
|
||||||
Return_json(ctx, "api_ok", nil)
|
|
||||||
})
|
|
||||||
|
|
||||||
v1_api := r.Group("/v1/")
|
|
||||||
{
|
|
||||||
V1_time_api(v1_api.Group("/time/"))
|
|
||||||
V1_user_api(v1_api.Group("/user/"))
|
|
||||||
V1_file_api(v1_api.Group("/file/"))
|
|
||||||
V1_cookie_api(v1_api.Group("/cookie/"))
|
|
||||||
V1_warehouses_api(v1_api.Group("/warehouses_api/"))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -178,7 +178,7 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//ctx.SetCookie("user", "", -1, "/", models.Wed_configs.Host, models.Wed_configs.Tls, true)
|
ctx.SetCookie("user", "", -1, "/", models.Wed_configs.Host, models.Wed_configs.Tls, true)
|
||||||
Return_json(ctx, "user_no_sign", nil)
|
Return_json(ctx, "user_no_sign", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package routers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"saas/models"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Api_router(r *gin.RouterGroup) {
|
||||||
|
|
||||||
|
r.GET("/", func(ctx *gin.Context) {
|
||||||
|
Return_json(ctx, "api_ok", nil)
|
||||||
|
})
|
||||||
|
|
||||||
|
r.Use(func(ctx *gin.Context) {
|
||||||
|
//转换传进来的数据
|
||||||
|
var jsonData map[string]interface{}
|
||||||
|
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||||
|
//分离数据
|
||||||
|
var cookie_t models.Cookie
|
||||||
|
if err = mapstructure.Decode(jsonData["cookie"], &cookie_t); err == nil {
|
||||||
|
if cookie_t.Value != "" {
|
||||||
|
cookie_vel := cookie_t.Value
|
||||||
|
ctx.Set("cookie_value", cookie_vel)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var data_t map[string]interface{}
|
||||||
|
if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil {
|
||||||
|
ctx.Set("data", &data_t)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
fmt.Println("jsonerr", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
Use_login_from_cookie(ctx)
|
||||||
|
})
|
||||||
|
|
||||||
|
v1_api := r.Group("/v1/")
|
||||||
|
{
|
||||||
|
V1_time_api(v1_api.Group("/time/"))
|
||||||
|
V1_user_api(v1_api.Group("/user/"))
|
||||||
|
V1_file_api(v1_api.Group("/file/"))
|
||||||
|
V1_cookie_api(v1_api.Group("/cookie/"))
|
||||||
|
V1_warehouses_api(v1_api.Group("/warehouses_api/"))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,6 +11,23 @@ import (
|
|||||||
|
|
||||||
func Def_router(r *gin.RouterGroup) {
|
func Def_router(r *gin.RouterGroup) {
|
||||||
|
|
||||||
|
r.Use(func(ctx *gin.Context) {
|
||||||
|
cookie_vel := ""
|
||||||
|
//读取用户cookie,判断用户是否已登录
|
||||||
|
cookie_s, is_have_cookie := ctx.Cookie("user")
|
||||||
|
if is_have_cookie == nil {
|
||||||
|
cookie_vel = cookie_s
|
||||||
|
}
|
||||||
|
|
||||||
|
//fmt.Println(cookie_vel)
|
||||||
|
if cookie_vel != "" {
|
||||||
|
ctx.Set("cookie_value", cookie_vel)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Use_login_from_cookie(ctx)
|
||||||
|
})
|
||||||
|
|
||||||
//无需权限的页面
|
//无需权限的页面
|
||||||
r.GET("/", func(ctx *gin.Context) {
|
r.GET("/", func(ctx *gin.Context) {
|
||||||
user_info, is_login := ctx.Get("user_info")
|
user_info, is_login := ctx.Get("user_info")
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package routers
|
||||||
+7
-30
@@ -5,40 +5,17 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mitchellh/mapstructure"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Fitst_use(ctx *gin.Context) {
|
// 从cookie登录用户
|
||||||
cookie_vel := ""
|
func Use_login_from_cookie(ctx *gin.Context) {
|
||||||
//读取用户cookie,判断用户是否已登录
|
//先从缓存获取cookie值
|
||||||
cookie_s, is_have_cookie := ctx.Cookie("user")
|
cookie_value, is_have_cookie := ctx.Get("cookie_value")
|
||||||
if is_have_cookie == nil {
|
if is_have_cookie {
|
||||||
cookie_vel = cookie_s
|
//fmt.Println(cookie_value)
|
||||||
}
|
|
||||||
|
|
||||||
//转换传进来的数据
|
|
||||||
var jsonData map[string]interface{}
|
|
||||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
|
||||||
//分离数据
|
|
||||||
var cookie_t models.Cookie
|
|
||||||
if err = mapstructure.Decode(jsonData["cookie"], &cookie_t); err == nil {
|
|
||||||
if cookie_t.Value != "" {
|
|
||||||
cookie_vel = cookie_t.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
var data_t map[string]interface{}
|
|
||||||
if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil {
|
|
||||||
ctx.Set("data", &data_t)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//fmt.Println(cookie_vel)
|
|
||||||
if cookie_vel != "" {
|
|
||||||
var cookie models.Cookie
|
var cookie models.Cookie
|
||||||
cookie.Value = cookie_vel
|
cookie.Value = cookie_value.(string)
|
||||||
if models.DB.Where(&cookie).First(&cookie).Error == nil {
|
if models.DB.Where(&cookie).First(&cookie).Error == nil {
|
||||||
|
|
||||||
// 有数据
|
// 有数据
|
||||||
|
|||||||
Vendored
+5
-6
@@ -36,17 +36,16 @@ function post_file(path, file, file_name, callback) {
|
|||||||
//获取保存的cookie
|
//获取保存的cookie
|
||||||
var cookie = load_json("cookie");
|
var cookie = load_json("cookie");
|
||||||
//console.log(cookie);
|
//console.log(cookie);
|
||||||
formData.append("cookie", JSON.stringify(cookie)); //插入cookie
|
if(cookie){
|
||||||
|
formData.append("cookie", cookie.Value); //插入cookie
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var re_data = {};
|
var re_data = {};
|
||||||
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
axios
|
axios
|
||||||
.post(head_path + path, formData, {
|
.post(head_path + path, formData)
|
||||||
headers: {
|
|
||||||
"Content-Type": "multipart/form-data", // 这里实际上可以省略,因为FormData会被正确识别
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
//console.log(response)
|
//console.log(response)
|
||||||
re_data["statusCode"] = response.status;
|
re_data["statusCode"] = response.status;
|
||||||
|
|||||||
@@ -559,18 +559,46 @@
|
|||||||
canvas.toBlob(resolve, 'image/jpeg', 0.9)
|
canvas.toBlob(resolve, 'image/jpeg', 0.9)
|
||||||
);
|
);
|
||||||
|
|
||||||
post_file("/upload", blob, `avatar_${Date.now()}.jpg`, (c) => {
|
// post_file("/upload", blob, `avatar_${Date.now()}.jpg`, (c) => {
|
||||||
if (c.statusCode == 200) {
|
// if (c.statusCode == 200) {
|
||||||
if (c.data.err_code == 0) {
|
// if (c.data.err_code == 0) {
|
||||||
//save_json("cookie", c.data.return.cookie)
|
// //save_json("cookie", c.data.return.cookie)
|
||||||
banner_alert('success', "更换成功", 950)
|
// banner_alert('success', "更换成功", 950)
|
||||||
} else {
|
// } else {
|
||||||
banner_alert('warning', "服务错误", 3000)
|
// banner_alert('warning', "服务错误", 3000)
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// banner_alert('danger', "网络连接错误:" + c.statusCode, 3000)
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', blob, `avatar_${Date.now()}.jpg`);
|
||||||
|
formData.append('meta', JSON.stringify({
|
||||||
|
width: canvas.width,
|
||||||
|
height: canvas.height,
|
||||||
|
scale: currentScale.toFixed(2)
|
||||||
|
}));
|
||||||
|
|
||||||
|
const response = await fetch('/api/v1/file/upload', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
headers: {
|
||||||
|
'X-Requested-With': 'XMLHttpRequest'
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) throw new Error(`服务器错误: ${response.status}`);
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
if (result.err_code == 0) {
|
||||||
|
showMessage(`✅ 上传成功!`, 'success');
|
||||||
|
set_user_avatar(result.data.path);
|
||||||
|
console.log(get_user_avatar());
|
||||||
|
avatar_toolt.hide();
|
||||||
} else {
|
} else {
|
||||||
banner_alert('danger', "网络连接错误:" + c.statusCode, 3000)
|
showMessage(`❌ 上传失败: ${result.err_msg}`, 'error');
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user