first commit

This commit is contained in:
2026-02-17 13:06:23 +08:00
commit 7cbd3d061d
349 changed files with 126558 additions and 0 deletions
+117
View File
@@ -0,0 +1,117 @@
package middleware
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"io"
"net/http"
"time"
"xiawan/wx/db"
)
/*
**
认证
*/
func BasicAuth() gin.HandlerFunc {
return func(c *gin.Context) {
auth := c.Request.Header.Get("Authorization")
if auth == "" {
RsqK(c, "授权码不能为空")
return
}
modelAuth := &Auth{}
var key = fmt.Sprintf("api:auth:%s", auth)
modelAuth = GetCacheAuth(key)
if modelAuth == nil {
rsq := Get("http://119.45.28.143:8000/api/v1/auth?key=" + auth)
if err := json.Unmarshal([]byte(rsq), &modelAuth); err == nil {
if modelAuth.Data == 200 {
CacheAuthAdd(key, modelAuth)
c.Next()
return
}
RsqK(c, modelAuth.Msg)
return
}
} else {
if modelAuth.Data == 200 {
CacheAuthAdd(key, modelAuth)
c.Next()
return
}
RsqK(c, modelAuth.Msg)
return
}
}
}
func RsqK(c *gin.Context, msg string) {
c.AbortWithStatus(http.StatusCreated)
respVo := &RespVo{
Code: http.StatusOK,
Message: msg,
Data: http.StatusCreated,
}
c.JSON(http.StatusOK, respVo)
}
type Auth struct {
Code int32
Data int32
Msg string
}
// {"code":0,"message":"","data":""}
type RespVo struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
// 发送GET请求
// url: 请求地址
// response 请求返回的内容
func Get(url string) string {
// 超时时间:15秒
client := &http.Client{Timeout: 15 * time.Second}
resp, err := client.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var buffer [512]byte
result := bytes.NewBuffer(nil)
for {
n, err := resp.Body.Read(buffer[0:])
result.Write(buffer[0:n])
if err != nil && err == io.EOF {
break
} else if err != nil {
panic(err)
}
}
return result.String()
}
// 添加授权缓存
func CacheAuthAdd(key string, val *Auth) {
cache := db.GetCaChe()
isContains := cache.Contains(key)
if !isContains {
cache.Set(key, val, time.Hour*1)
return
}
}
// 查询授权缓存
func GetCacheAuth(key string) *Auth {
cache := db.GetCaChe()
isContains := cache.Contains(key)
if !isContains {
return nil
}
return cache.Get(key).(*Auth)
}
+17
View File
@@ -0,0 +1,17 @@
package middleware
import (
"github.com/gin-gonic/gin"
)
func Cors(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
+52
View File
@@ -0,0 +1,52 @@
package middleware
import (
"net/http"
"strings"
"xiawan/wx/api/vo"
"xiawan/wx/db"
"xiawan/wx/srv/srvconfig"
"github.com/gin-gonic/gin"
)
var NotFilterReqRouterArray = []string{
"/v1/login/GetLoginStatus",
"/v1/login/WakeUpLogin",
"/v1/login/DeviceLogin",
"/v1/login/GetLoginQrCode",
}
// FilterInstanceMiddleware 过滤不同服务器不同的实例 避免出现串号问题
func FilterInstanceMiddleware(ctx *gin.Context) {
// 放行指定请求
for _, s := range NotFilterReqRouterArray {
if s == ctx.FullPath() {
ctx.Next() //放行请求
return
}
}
// 是否带Key
queryKey, isExist := ctx.GetQuery("key")
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
ctx.Next() //放行请求
return
}
// 从数据库取该key 的用户信息
userInfoEntity := db.GetUserInfoEntity(queryKey)
if userInfoEntity == nil {
ctx.Next() //放行请求
return
}
if userInfoEntity.TargetIp == "" {
ctx.Next() //放行请求
return
}
// 判断是否为 同个服务器
if userInfoEntity.TargetIp != srvconfig.GlobalSetting.TargetIp {
ctx.JSON(http.StatusOK, vo.NewFAILDoesNotBelongToServer(queryKey))
ctx.Abort()
return
}
}