u
This commit is contained in:
1 parent
8a3dea8545
commit
1903861354
21 files changed
+335
-236
No files matched your search
@@ -12,11 +12,11 @@ import (
|
||||
"allapp/utils/requestx"
|
||||
"allapp/utils/responsex"
|
||||
"allapp/utils/uniqueid"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/client"
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
)
|
||||
|
||||
// LoginQq qq登录
|
||||
@@ -48,6 +48,11 @@ func LoginWechat(c fiber.Ctx) error {
|
||||
|
||||
openid, accessToken, err := getWechatAccess(dto.Code)
|
||||
if err != nil {
|
||||
log.Errorw("调用微信 access 接口失败",
|
||||
"code", dto.Code, // 不打印原始 code
|
||||
"error", err.Error(),
|
||||
)
|
||||
fmt.Println(err.Error())
|
||||
return responsex.Error(c, "微信登录失败,请重试")
|
||||
}
|
||||
|
||||
@@ -68,6 +73,10 @@ func LoginWechat(c fiber.Ctx) error {
|
||||
// 新用户 → 拉用户信息
|
||||
nickname, avatar, err := getWechatUserInfo(accessToken, openid)
|
||||
if err != nil {
|
||||
log.Errorw("调用微信用户信息接口失败",
|
||||
"openid", openid,
|
||||
"error", err,
|
||||
)
|
||||
return responsex.Error(c, "微信登录失败,请重试")
|
||||
}
|
||||
|
||||
@@ -104,7 +113,7 @@ func getWechatAccess(code string) (openid string, accessToken string, err error)
|
||||
openid, _ = accessMap["openid"].(string)
|
||||
|
||||
if accessToken == "" || openid == "" {
|
||||
return "", "", errors.New("获取微信登录凭证失败")
|
||||
return "", "", fmt.Errorf("获取微信登录凭证失败: %s", bodyData)
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
@@ -20,7 +20,7 @@ func Logger() fiber.Handler {
|
||||
duration := time.Since(start)
|
||||
|
||||
// 记录日志
|
||||
log.Infow("API Request",
|
||||
log.WithContext(c.Context()).Infow("API Request",
|
||||
"method", c.Method(),
|
||||
"path", c.Path(),
|
||||
"status", c.Response().StatusCode(),
|
||||
|
||||
@@ -17,46 +17,60 @@ const CtxSpaceIDKey = "space_id"
|
||||
func SpaceGuard() fiber.Handler {
|
||||
return func(c fiber.Ctx) error {
|
||||
userID, err := GetUserID(c)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
spaceIDStr := strings.TrimSpace(c.Get("SPACE_ID"))
|
||||
|
||||
spaceID, err := strconv.ParseInt(spaceIDStr, 10, 64)
|
||||
if err != nil || spaceID <= 0 {
|
||||
return responsex.SpaceError(c, "请选择空间")
|
||||
}
|
||||
|
||||
// 校验
|
||||
// 1 先查 Redis
|
||||
inSpace, err := redisx.SpaceIsMember(spaceID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if inSpace {
|
||||
c.Locals(CtxSpaceIDKey, spaceID)
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
// 检验空间是否存在
|
||||
space, err := db.Queries.GetSpaceById(c.Context(), spaceID)
|
||||
|
||||
// 2 Redis miss → 查数据库
|
||||
role, err := db.Queries.GetUserSpaceRole(c.Context(), repo.GetUserSpaceRoleParams{
|
||||
SpaceID: spaceID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
if errorx.IsNotFound(err) {
|
||||
// 未找到
|
||||
return responsex.SpaceError(c, "空间已失效")
|
||||
return responsex.SpaceError(c, "未加入空间")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 空间有效,判断是否在该空间内
|
||||
role, err := db.Queries.GetUserSpaceRole(c.Context(), repo.GetUserSpaceRoleParams{
|
||||
SpaceID: spaceID,
|
||||
UserID: spaceID,
|
||||
})
|
||||
if err != nil {
|
||||
if errorx.IsNotFound(err) {
|
||||
|
||||
}
|
||||
}
|
||||
// 3 写回 Redis(重建缓存)
|
||||
_ = redisx.SpaceAddOrUpdateMember(spaceID, userID, int(role))
|
||||
c.Locals(CtxSpaceIDKey, spaceID)
|
||||
|
||||
// 4 放行
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func GetSpaceID(c fiber.Ctx) (int64, error) {
|
||||
v := c.Locals(CtxSpaceIDKey)
|
||||
|
||||
if v == nil {
|
||||
return 0, responsex.SpaceError(c, "空间无效")
|
||||
}
|
||||
|
||||
spaceID, ok := v.(int64)
|
||||
|
||||
if !ok {
|
||||
return 0, responsex.SpaceError(c, "空间无效")
|
||||
}
|
||||
|
||||
return spaceID, nil
|
||||
}
|
||||
@@ -10,13 +10,22 @@ import (
|
||||
func SetupRouters(app *fiber.App) {
|
||||
api := app.Group(config.GetConf().Server.BaseUrl)
|
||||
|
||||
public := api.Group("")
|
||||
//private := api.Group("", middleware.Auth())
|
||||
// 1️⃣ 公开接口(无需登录)
|
||||
publicGroup := api.Group("")
|
||||
|
||||
// 2️⃣ 需要登录
|
||||
//authGroup := api.Group("", middleware.Auth())
|
||||
//
|
||||
//// 3️⃣ 需要登录 + 需要空间
|
||||
//spaceGroup := api.Group("", middleware.Auth(), middleware.SpaceGuard())
|
||||
|
||||
// --------------------
|
||||
// auth
|
||||
authRouter := public.Group("/auth")
|
||||
authRouter := publicGroup.Group("/auth")
|
||||
authRouter.Post("/login/qq", auth.LoginQq)
|
||||
authRouter.Post("/login/wechat", auth.LoginWechat)
|
||||
|
||||
// 其他的都带中间件
|
||||
// 示例
|
||||
// authGroup.Get("/user/profile", user.GetProfile)
|
||||
// spaceGroup.Get("/space/info", space.GetInfo)
|
||||
}
|
||||
@@ -28,4 +28,4 @@ INSERT INTO "user" (
|
||||
-- name: UpdateUserLastLoginTime :exec
|
||||
UPDATE "user"
|
||||
SET last_login_time = NOW()
|
||||
WHERE id = $1;
|
||||
WHERE id = $1;
|
||||
@@ -3,9 +3,13 @@ module allapp
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/go-playground/validator/v10 v10.30.1
|
||||
github.com/gofiber/contrib/v3/zap v1.0.0
|
||||
github.com/gofiber/fiber/v3 v3.1.0
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1
|
||||
github.com/jackc/pgx/v5 v5.8.0
|
||||
github.com/redis/go-redis/v9 v9.18.0
|
||||
github.com/shopspring/decimal v1.4.0
|
||||
github.com/spf13/viper v1.21.0
|
||||
go.uber.org/zap v1.27.1
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
@@ -18,17 +22,15 @@ require (
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.13 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.30.1 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
|
||||
github.com/gofiber/schema v1.7.0 // indirect
|
||||
github.com/gofiber/utils/v2 v2.0.2 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.8.0 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/klauspost/compress v1.18.4 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
@@ -36,9 +38,7 @@ require (
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/redis/go-redis/v9 v9.18.0 // indirect
|
||||
github.com/sagikazarmark/locafero v0.12.0 // indirect
|
||||
github.com/shopspring/decimal v1.4.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
|
||||
@@ -15,6 +15,8 @@ github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sa
|
||||
github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||
github.com/gabriel-vasile/mimetype v1.4.13 h1:46nXokslUBsAJE/wMsp5gtO500a4F3Nkz9Ufpk2AcUM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.13/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
|
||||
@@ -11,6 +11,10 @@ log:
|
||||
max_age: 7
|
||||
compress: true
|
||||
|
||||
wechat:
|
||||
app_id: wxdab3e21a1f7e392f
|
||||
app_secret: 0895443c6fedfc21cb92c9cd60de2a26
|
||||
|
||||
jwt:
|
||||
secret: 3Bde3BGEbYqtqyEUzW3ry8jKFcaPH17fRmTmqE7MDr05Lwj95uruRKrrkb44TJ4s
|
||||
access_expiry: -1
|
||||
@@ -68,22 +68,6 @@ func SpaceIsMember(spaceID int64, userID int64) (bool, error) {
|
||||
return ok, err
|
||||
}
|
||||
|
||||
// SpaceInitMembers 初始化用户成员
|
||||
func SpaceInitMembers(spaceID int64, members map[int64]int) error {
|
||||
key := spaceMemberKey(spaceID)
|
||||
|
||||
data := make(map[string]interface{})
|
||||
for userID, role := range members {
|
||||
data[strconv.FormatInt(userID, 10)] = role
|
||||
}
|
||||
|
||||
return Client.HSet(
|
||||
context.Background(),
|
||||
key,
|
||||
data,
|
||||
).Err()
|
||||
}
|
||||
|
||||
// SpaceDelete 删除空间
|
||||
func SpaceDelete(spaceID int64) error {
|
||||
key := spaceMemberKey(spaceID)
|
||||
|
||||
@@ -7,7 +7,7 @@ type Result struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
TrackId string `json:"trackId,omitempty"`
|
||||
TrackId string `json:"track_id,omitempty"`
|
||||
}
|
||||
|
||||
// 业务码
|
||||
|
||||
Reference in new issue
Block a user