feat(auth): return smoke mode in login response
This commit is contained in:
@@ -35,3 +35,4 @@ go.work
|
||||
|
||||
# Local build binary
|
||||
wx_service
|
||||
wx_service_api
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
module wx_service
|
||||
|
||||
go 1.23.6
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.4
|
||||
|
||||
require (
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/redis/go-redis/v9 v9.17.2
|
||||
golang.org/x/crypto v0.40.0
|
||||
gorm.io/driver/mysql v1.6.0
|
||||
gorm.io/driver/sqlite v1.6.0
|
||||
gorm.io/gorm v1.31.1
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2 // indirect
|
||||
github.com/bytedance/sonic v1.14.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
@@ -26,7 +31,6 @@ require (
|
||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/goccy/go-yaml v1.18.0 // indirect
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
@@ -43,7 +47,6 @@ require (
|
||||
github.com/ugorji/go/codec v1.3.0 // indirect
|
||||
go.uber.org/mock v0.5.0 // indirect
|
||||
golang.org/x/arch v0.20.0 // indirect
|
||||
golang.org/x/crypto v0.40.0 // indirect
|
||||
golang.org/x/mod v0.25.0 // indirect
|
||||
golang.org/x/net v0.42.0 // indirect
|
||||
golang.org/x/sync v0.16.0 // indirect
|
||||
@@ -51,5 +54,4 @@ require (
|
||||
golang.org/x/text v0.27.0 // indirect
|
||||
golang.org/x/tools v0.34.0 // indirect
|
||||
google.golang.org/protobuf v1.36.9 // indirect
|
||||
gorm.io/driver/sqlite v1.6.0 // indirect
|
||||
)
|
||||
|
||||
@@ -88,6 +88,9 @@ func (h *AuthHandler) LoginWithWeChat(c *gin.Context) {
|
||||
if result.User.UnionID != "" {
|
||||
userPayload["union_id"] = result.User.UnionID
|
||||
}
|
||||
if result.Mode != "" {
|
||||
userPayload["mode"] = result.Mode
|
||||
}
|
||||
|
||||
miniProgramPayload := gin.H{
|
||||
"id": result.MiniProgram.ID,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"wx_service/internal/model"
|
||||
smokemodel "wx_service/internal/smoke/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -40,6 +41,7 @@ type LoginResult struct {
|
||||
User *model.User
|
||||
SessionKey string
|
||||
MiniProgram *model.MiniProgram
|
||||
Mode string
|
||||
}
|
||||
|
||||
func NewAuthService(db *gorm.DB, miniProgramSvc *MiniProgramService) *AuthService {
|
||||
@@ -139,9 +141,38 @@ func (s *AuthService) LoginWithCode(ctx context.Context, req LoginRequest) (*Log
|
||||
SessionKey: session.SessionKey,
|
||||
MiniProgram: miniProgram,
|
||||
}
|
||||
if mode, err := s.getSmokeMode(ctx, int(user.ID)); err == nil {
|
||||
result.Mode = mode
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) getSmokeMode(ctx context.Context, uid int) (string, error) {
|
||||
var profile smokemodel.SmokeUserProfile
|
||||
err := s.db.WithContext(ctx).
|
||||
Select("mode").
|
||||
Where("uid = ? AND deleted_at IS NULL", uid).
|
||||
First(&profile).Error
|
||||
if err == nil {
|
||||
return normalizeSmokeMode(profile.Mode), nil
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return "", nil
|
||||
}
|
||||
return "", fmt.Errorf("load smoke profile mode: %w", err)
|
||||
}
|
||||
|
||||
func normalizeSmokeMode(mode string) string {
|
||||
switch strings.TrimSpace(mode) {
|
||||
case "quit":
|
||||
return "quit"
|
||||
case "record":
|
||||
return "record"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AuthService) getWeChatClient(mp *model.MiniProgram) *WeChatClient {
|
||||
s.cacheMu.RLock()
|
||||
client, ok := s.wechatClientCache[mp.ID]
|
||||
|
||||
Reference in New Issue
Block a user