initial crm server

This commit is contained in:
zchengo
2022-11-28 16:38:30 +08:00
parent 61122aef6a
commit af7cd0c44c
36 changed files with 2615 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Cors 处理跨域请求
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin")
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id, Uid, Ver")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
// 放行所有OPTIONS方法
if method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
}
// 处理请求
c.Next()
}
}
+27
View File
@@ -0,0 +1,27 @@
package middleware
import (
"crm/response"
"crm/service"
"github.com/gin-gonic/gin"
)
// JwtAuth JWT认证中间件
func JwtAuth() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.Request.Header.Get("token")
if token == "" {
response.Result(response.ErrCodeNoLogin, nil, c)
c.Abort()
return
}
user := &service.UserService{}
if err := user.VerifyToken(token); err != nil {
response.Result(response.ErrCodeTokenExpire, nil, c)
c.Abort()
return
}
c.Next()
}
}