Browse Source

新增上传接口

朱金辉 2 years ago
parent
commit
7f9b40b720

+ 2 - 2
admin/upload.go

@@ -5,11 +5,11 @@ import (
 	"github.com/gin-gonic/gin"
 	"gopkg.in/gographics/imagick.v3/imagick"
 	setting "icloudapp.cn/tools/config"
+	"icloudapp.cn/tools/controller"
 	"icloudapp.cn/tools/entity"
 	"icloudapp.cn/tools/model"
 	sEntity "icloudapp.cn/tools/service/entity"
 	utFile "icloudapp.cn/tools/util/file"
-	utString "icloudapp.cn/tools/util/string"
 	"time"
 )
 
@@ -41,7 +41,7 @@ func Material(ctx *gin.Context) {
 	size := fmt.Sprintf("%dx%d", image.GetImageWidth(), image.GetImageHeight())
 	material := model.NewMMaterial(ctx)
 	add, err := material.Add(&sEntity.Material{
-		UID:      utString.ConvertInt64(ctx.GetString("uid")),
+		UID:      controller.GetUID(ctx),
 		Name:     fileInfo.Name,
 		File:     fileInfo.File,
 		Size:     size,

+ 1 - 1
controller/base.go

@@ -20,7 +20,7 @@ func GetUID(ctx *gin.Context) int64 {
 	uidParam, exist := ctx.Get("jwt_uid")
 	var uid int64 = 0
 	if !exist {
-		return 0
+		return uid
 	}
 	if err := util.ConvertAssign(&uid, uidParam); err != nil {
 		return 0

+ 0 - 2
controller/index.go

@@ -6,7 +6,5 @@ import (
 )
 
 func Index(c *gin.Context) {
-	s := make(map[string]interface{})
-	s["msg"] = "success"
 	entity.ResponseSuccess(c, entity.EmptyBodyObject())
 }

+ 1 - 2
controller/materials.go

@@ -4,11 +4,10 @@ import (
 	"github.com/gin-gonic/gin"
 	"icloudapp.cn/tools/entity"
 	"icloudapp.cn/tools/model"
-	utString "icloudapp.cn/tools/util/string"
 )
 
 func Materials(ctx *gin.Context) {
-	uid := utString.ConvertInt64(ctx.GetString("uid"))
+	uid := GetUID(ctx)
 	mMaterial := model.NewMMaterial(ctx)
 	materials := mMaterial.Materials(uid)
 

+ 1 - 2
controller/poster.go

@@ -9,7 +9,6 @@ import (
 	"icloudapp.cn/tools/model"
 	"icloudapp.cn/tools/util"
 	"icloudapp.cn/tools/util/image"
-	utString "icloudapp.cn/tools/util/string"
 	"os"
 	"time"
 )
@@ -197,7 +196,7 @@ func DrawImage(ctx *gin.Context) {
 
 // Posters 通过用户ID获取用户名下的所有海报
 func Posters(ctx *gin.Context) {
-	uid := utString.ConvertInt64(ctx.GetString("uid"))
+	uid := GetUID(ctx)
 
 	if uid == 0 {
 		entity.ResponseNormal(ctx, entity.CodeDenied, "请登录后再试", []interface{}{})

+ 1 - 1
controller/user.go

@@ -13,7 +13,7 @@ import (
 
 // Info 获取用户信息
 func Info(ctx *gin.Context) {
-	uid := ctx.GetInt64("uid")
+	uid := GetUID(ctx)
 
 	if uid < 1 {
 		entity.ResponseError(ctx, entity.CodeInvalidParam)

+ 4 - 2
entity/userinfo.go

@@ -1,6 +1,8 @@
 package entity
 
-import "time"
+import (
+	"time"
+)
 
 // LoginRequest 登录请求参数
 type LoginRequest struct {
@@ -24,7 +26,7 @@ type Login struct {
 type RegisterRequest struct {
 	UserName string `form:"username" binding:"required"`
 	Password string `form:"password" binding:"required"`
-	Email    string `form:"email"`
+	Email    string `form:"email" binding:"email"`
 }
 
 // UserInfo 返回给接口的用户信息,带Code和Msg

+ 0 - 23
middleware/auth.go

@@ -1,23 +0,0 @@
-package middleware
-
-import (
-	"github.com/gin-gonic/gin"
-	"icloudapp.cn/tools/entity"
-)
-
-// JWTAuthMiddleware 基于JWT的认证中间件
-func AuthMiddleware() func(c *gin.Context) {
-	return func(c *gin.Context) {
-		// 客户端携带Token有三种方式 1.放在请求头 2.放在请求体 3.放在URI
-		// token验证成功,返回c.Next继续,否则返回c.Abort()直接返回
-		authHeader := c.Request.Header.Get("Authorization")
-		if authHeader == "" {
-			entity.ResponseError(c, entity.CodeNeedLogin)
-			c.Abort()
-			return
-		}
-		// 将当前请求的userID信息保存到请求的上下文c上
-		//c.Set(controller.CtxUserIDKey, mc.UserID)
-		c.Next() // 后续的处理请求的函数中 可以用过c.Get(CtxUserIDKey) 来获取当前请求的用户信息
-	}
-}

+ 1 - 1
router/api_router.go

@@ -12,7 +12,7 @@ func SetupApiRouters(r *gin.Engine) {
 	r.StaticFS("/upload", http.Dir("./upload"))
 
 	v1 := r.Group("/api/v1")
-	v1.Use(middleware.AuthMiddleware())
+	v1.Use(middleware.JWTAuthMiddleware())
 	/*
 		v1.GET("pipeline/:id", controller.GetPipelineDetailHandler)
 		v1.GET("pipeline", controller.GetPipelineListHandler)

+ 2 - 1
router/init.go

@@ -12,9 +12,10 @@ func InitRouter() *gin.Engine {
 	r := gin.Default()
 	// 上传大小限制 100M
 	r.MaxMultipartMemory = 100 << 20
+	// 启用gzip压缩
 	r.Use(gzip.Gzip(gzip.BestCompression))
 	r.Use(middleware.LoggerMiddleware())
-	//注册错误恢复
+	// 错误恢复
 	r.Use(gin.CustomRecovery(func(ctx *gin.Context, recovered interface{}) {
 		if err, ok := recovered.(string); ok {
 			ctx.String(http.StatusInternalServerError, fmt.Sprintf("error occuring: %s", err))