提交文件api还需要完善
This commit is contained in:
@@ -11,20 +11,51 @@ import (
|
||||
"saas/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
func V1_file_api(r *gin.RouterGroup) {
|
||||
r.GET("/", func(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, map[string]interface{}{
|
||||
"error": "you need use Post",
|
||||
})
|
||||
Return_json(ctx, "json_error", nil)
|
||||
})
|
||||
r.POST("/upload", func(ctx *gin.Context) {
|
||||
|
||||
//文件api是一定要登录的,直接用中间件判断登录状态
|
||||
// r.Use(func(ctx *gin.Context) {
|
||||
// Use_is_login(ctx)
|
||||
// })
|
||||
|
||||
r.Use(func(ctx *gin.Context) {
|
||||
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 {
|
||||
dst := path.Join("./data/upload", file.Filename)
|
||||
ctx.SaveUploadedFile(file, dst)
|
||||
fmt.Println(file)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
r.POST("/upload", func(ctx *gin.Context) {
|
||||
//先判断有没有登录
|
||||
//获取中间件处理的结果
|
||||
_, is_login := ctx.Get("user_info")
|
||||
if is_login {
|
||||
Return_json(ctx, "api_ok", nil)
|
||||
} else {
|
||||
Return_json(ctx, "user_no_sign", nil)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
//接收头像的接口,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"saas/models"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -157,9 +156,9 @@ func V1_user_api(r *gin.RouterGroup) {
|
||||
|
||||
//先判断是否已经登录
|
||||
//获取中间件处理的结果
|
||||
user_info, is_login := ctx.Get("user_info")
|
||||
fmt.Println(is_login)
|
||||
fmt.Println(user_info)
|
||||
_, is_login := ctx.Get("user_info")
|
||||
//fmt.Println(is_login)
|
||||
//fmt.Println(user_info)
|
||||
if is_login == true {
|
||||
//fmt.Println("loged")
|
||||
cookie_any, _ := ctx.Get("cookie") //这个cookie在中间件已经判断为有效的,否则is_login不可能为true,所以直接在数据库删除应该是安全的
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"saas/models"
|
||||
"time"
|
||||
|
||||
@@ -28,6 +27,7 @@ func Fitst_use(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var data_t map[string]interface{}
|
||||
if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil {
|
||||
ctx.Set("data", &data_t)
|
||||
@@ -80,7 +80,7 @@ func Fitst_use(ctx *gin.Context) {
|
||||
models.DB.Create(&user_info) // 传入指针
|
||||
}
|
||||
//写入当前登录的用户信息 传递给下一个组件
|
||||
fmt.Println(user_info)
|
||||
//fmt.Println(user_info)
|
||||
ctx.Set("user_info", &user_info)
|
||||
ctx.Set("user", &user)
|
||||
} else {
|
||||
@@ -116,3 +116,7 @@ func Fitst_use(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Use_is_login(ctx *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user