🐛 fix the casbin invalidation bug/adjust routing strategy
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"schisandra-cloud-album/api/captcha_api"
|
||||
"schisandra-cloud-album/api/client_api"
|
||||
"schisandra-cloud-album/api/oauth_api"
|
||||
"schisandra-cloud-album/api/permission_api"
|
||||
"schisandra-cloud-album/api/role_api"
|
||||
@@ -19,6 +20,7 @@ type Apis struct {
|
||||
WebsocketApi websocket_api.WebsocketAPI
|
||||
RoleApi role_api.RoleAPI
|
||||
PermissionApi permission_api.PermissionAPI
|
||||
ClientApi client_api.ClientAPI
|
||||
}
|
||||
|
||||
// Api new函数实例化,实例化完成后会返回结构体地指针类型
|
||||
|
7
api/client_api/client.go
Normal file
7
api/client_api/client.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package client_api
|
||||
|
||||
import "sync"
|
||||
|
||||
type ClientAPI struct{}
|
||||
|
||||
var mu sync.Mutex
|
42
api/client_api/client_api.go
Normal file
42
api/client_api/client_api.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package client_api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"schisandra-cloud-album/common/constant"
|
||||
"schisandra-cloud-album/common/redis"
|
||||
"schisandra-cloud-album/common/result"
|
||||
"schisandra-cloud-album/global"
|
||||
"schisandra-cloud-album/utils"
|
||||
)
|
||||
|
||||
// GenerateClientId 生成客户端ID
|
||||
// @Summary 生成客户端ID
|
||||
// @Description 生成客户端ID
|
||||
// @Tags 微信公众号
|
||||
// @Produce json
|
||||
// @Router /api/oauth/generate_client_id [get]
|
||||
func (ClientAPI) GenerateClientId(c *gin.Context) {
|
||||
// 获取客户端IP
|
||||
ip := utils.GetClientIP(c)
|
||||
// 加锁
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
// 从Redis获取客户端ID
|
||||
clientId := redis.Get(constant.UserLoginClientRedisKey + ip).Val()
|
||||
if clientId != "" {
|
||||
result.OkWithData(clientId, c)
|
||||
return
|
||||
}
|
||||
|
||||
// 生成新的客户端ID
|
||||
v1 := uuid.NewV1()
|
||||
err := redis.Set(constant.UserLoginClientRedisKey+ip, v1.String(), 0).Err()
|
||||
if err != nil {
|
||||
global.LOG.Error(err)
|
||||
return
|
||||
}
|
||||
result.OkWithData(v1.String(), c)
|
||||
return
|
||||
}
|
@@ -150,7 +150,6 @@ func (OAuthAPI) GetUserLoginDevice(c *gin.Context) {
|
||||
os := ua.OS()
|
||||
mobile := ua.Mobile()
|
||||
mozilla := ua.Mozilla()
|
||||
m := ua.Model()
|
||||
platform := ua.Platform()
|
||||
engine, engineVersion := ua.Engine()
|
||||
device := model.ScaAuthUserDevice{
|
||||
@@ -164,7 +163,6 @@ func (OAuthAPI) GetUserLoginDevice(c *gin.Context) {
|
||||
Mobile: &mobile,
|
||||
Bot: &isBot,
|
||||
Mozilla: &mozilla,
|
||||
Model: &m,
|
||||
Platform: &platform,
|
||||
EngineName: &engine,
|
||||
EngineVersion: &engineVersion,
|
||||
|
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/server/handlers/models"
|
||||
ginI18n "github.com/gin-contrib/i18n"
|
||||
"github.com/gin-gonic/gin"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/yitter/idgenerator-go/idgen"
|
||||
"gorm.io/gorm"
|
||||
"schisandra-cloud-album/api/user_api/dto"
|
||||
@@ -29,36 +28,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// GenerateClientId 生成客户端ID
|
||||
// @Summary 生成客户端ID
|
||||
// @Description 生成客户端ID
|
||||
// @Tags 微信公众号
|
||||
// @Produce json
|
||||
// @Router /api/oauth/generate_client_id [get]
|
||||
func (OAuthAPI) GenerateClientId(c *gin.Context) {
|
||||
// 获取客户端IP
|
||||
ip := utils.GetClientIP(c)
|
||||
// 加锁
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
// 从Redis获取客户端ID
|
||||
clientId := redis.Get(constant.UserLoginClientRedisKey + ip).Val()
|
||||
if clientId != "" {
|
||||
result.OkWithData(clientId, c)
|
||||
return
|
||||
}
|
||||
|
||||
// 生成新的客户端ID
|
||||
v1 := uuid.NewV1()
|
||||
err := redis.Set(constant.UserLoginClientRedisKey+ip, v1.String(), 0).Err()
|
||||
if err != nil {
|
||||
global.LOG.Error(err)
|
||||
return
|
||||
}
|
||||
result.OkWithData(v1.String(), c)
|
||||
}
|
||||
|
||||
// CallbackNotify 微信回调
|
||||
// @Summary 微信回调
|
||||
// @Tags 微信公众号
|
||||
|
@@ -68,3 +68,18 @@ func (PermissionAPI) AssignPermissionsToRole(c *gin.Context) {
|
||||
result.OkWithMessage(ginI18n.MustGetMessage(c, "AssignSuccess"), c)
|
||||
return
|
||||
}
|
||||
|
||||
// GetUserPermissions 获取服用权限
|
||||
func (PermissionAPI) GetUserPermissions(c *gin.Context) {
|
||||
userId := c.Query("user_id")
|
||||
if userId == "" {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "GetUserFailed"), c)
|
||||
return
|
||||
}
|
||||
data, err := global.Casbin.GetImplicitRolesForUser(userId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result.OkWithData(data, c)
|
||||
return
|
||||
}
|
||||
|
@@ -154,13 +154,14 @@ func (UserAPI) AccountLogin(c *gin.Context) {
|
||||
// @Router /api/user/phone_login [post]
|
||||
func (UserAPI) PhoneLogin(c *gin.Context) {
|
||||
request := dto.PhoneLoginRequest{}
|
||||
err := c.ShouldBindJSON(&request)
|
||||
err := c.ShouldBind(&request)
|
||||
if err != nil {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "ParamsError"), c)
|
||||
return
|
||||
}
|
||||
phone := request.Phone
|
||||
captcha := request.Captcha
|
||||
autoLogin := request.AutoLogin
|
||||
if phone == "" || captcha == "" {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "PhoneAndCaptchaNotEmpty"), c)
|
||||
return
|
||||
@@ -213,7 +214,7 @@ func (UserAPI) PhoneLogin(c *gin.Context) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
handelUserLogin(addUser, request.AutoLogin, c)
|
||||
handelUserLogin(addUser, autoLogin, c)
|
||||
return nil
|
||||
})
|
||||
errChan <- err
|
||||
@@ -227,24 +228,24 @@ func (UserAPI) PhoneLogin(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
codeChan := make(chan *string)
|
||||
codeChan := make(chan string)
|
||||
go func() {
|
||||
code := redis.Get(constant.UserLoginSmsRedisKey + phone).Val()
|
||||
codeChan <- &code
|
||||
codeChan <- code
|
||||
}()
|
||||
|
||||
code := <-codeChan
|
||||
close(codeChan)
|
||||
|
||||
if code == nil {
|
||||
if code == "" {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "CaptchaExpired"), c)
|
||||
return
|
||||
}
|
||||
if &captcha != code {
|
||||
if captcha != code {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "CaptchaError"), c)
|
||||
return
|
||||
}
|
||||
handelUserLogin(user, request.AutoLogin, c)
|
||||
handelUserLogin(user, autoLogin, c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,7 +317,7 @@ func handelUserLogin(user model.ScaAuthUser, autoLogin bool, c *gin.Context) {
|
||||
if autoLogin {
|
||||
days = 7 * 24 * time.Hour
|
||||
} else {
|
||||
days = 24 * time.Hour
|
||||
days = time.Minute * 30
|
||||
}
|
||||
|
||||
refreshToken, expiresAt := utils.GenerateRefreshToken(utils.RefreshJWTPayload{UserID: user.UID}, days)
|
||||
@@ -455,7 +456,6 @@ func getUserLoginDevice(user model.ScaAuthUser, c *gin.Context) bool {
|
||||
os := ua.OS()
|
||||
mobile := ua.Mobile()
|
||||
mozilla := ua.Mozilla()
|
||||
m := ua.Model()
|
||||
platform := ua.Platform()
|
||||
engine, engineVersion := ua.Engine()
|
||||
|
||||
@@ -470,7 +470,6 @@ func getUserLoginDevice(user model.ScaAuthUser, c *gin.Context) bool {
|
||||
Mobile: &mobile,
|
||||
Bot: &isBot,
|
||||
Mozilla: &mozilla,
|
||||
Model: &m,
|
||||
Platform: &platform,
|
||||
EngineName: &engine,
|
||||
EngineVersion: &engineVersion,
|
||||
|
Reference in New Issue
Block a user