♻️ update code structure
This commit is contained in:
@@ -263,7 +263,7 @@ func (CommentController) ReplyList(c *gin.Context) {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "ParamsError"), c)
|
||||
return
|
||||
}
|
||||
response := commentReplyService.GetCommentReplyService(replyListRequest.UserID, replyListRequest.TopicId, replyListRequest.CommentId, replyListRequest.Page, replyListRequest.Size)
|
||||
response := commentReplyService.GetCommentReplyListService(replyListRequest.UserID, replyListRequest.TopicId, replyListRequest.CommentId, replyListRequest.Page, replyListRequest.Size)
|
||||
result.OkWithData(response, c)
|
||||
return
|
||||
}
|
||||
|
@@ -2,30 +2,22 @@ package oauth_controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mssola/useragent"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"schisandra-cloud-album/common/constant"
|
||||
"schisandra-cloud-album/common/redis"
|
||||
"schisandra-cloud-album/common/result"
|
||||
"schisandra-cloud-album/global"
|
||||
"schisandra-cloud-album/model"
|
||||
"schisandra-cloud-album/service/impl"
|
||||
"schisandra-cloud-album/utils"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var mu sync.Mutex
|
||||
|
||||
type OAuthController struct{}
|
||||
|
||||
var userSocialService = impl.UserSocialServiceImpl{}
|
||||
var userService = impl.UserServiceImpl{}
|
||||
var userDeviceService = impl.UserDeviceServiceImpl{}
|
||||
|
||||
type Token struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
@@ -129,65 +121,3 @@ func HandelUserLogin(userId string, c *gin.Context) (bool, result.Response) {
|
||||
}
|
||||
return true, responseData
|
||||
}
|
||||
|
||||
// GetUserLoginDevice 获取用户登录设备
|
||||
func (OAuthController) GetUserLoginDevice(c *gin.Context) {
|
||||
userId := c.Query("user_id")
|
||||
if userId == "" {
|
||||
return
|
||||
}
|
||||
userAgent := c.GetHeader("User-Agent")
|
||||
if userAgent == "" {
|
||||
global.LOG.Errorln("user-agent is empty")
|
||||
return
|
||||
}
|
||||
ua := useragent.New(userAgent)
|
||||
|
||||
ip := utils.GetClientIP(c)
|
||||
location, err := global.IP2Location.SearchByStr(ip)
|
||||
location = utils.RemoveZeroAndAdjust(location)
|
||||
if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return
|
||||
}
|
||||
isBot := ua.Bot()
|
||||
browser, browserVersion := ua.Browser()
|
||||
os := ua.OS()
|
||||
mobile := ua.Mobile()
|
||||
mozilla := ua.Mozilla()
|
||||
platform := ua.Platform()
|
||||
engine, engineVersion := ua.Engine()
|
||||
device := model.ScaAuthUserDevice{
|
||||
UserID: &userId,
|
||||
IP: &ip,
|
||||
Location: &location,
|
||||
Agent: userAgent,
|
||||
Browser: &browser,
|
||||
BrowserVersion: &browserVersion,
|
||||
OperatingSystem: &os,
|
||||
Mobile: &mobile,
|
||||
Bot: &isBot,
|
||||
Mozilla: &mozilla,
|
||||
Platform: &platform,
|
||||
EngineName: &engine,
|
||||
EngineVersion: &engineVersion,
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
userDevice, err := userDeviceService.GetUserDeviceByUIDIPAgentService(userId, ip, userAgent)
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = userDeviceService.AddUserDeviceService(&device)
|
||||
if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
} else {
|
||||
err := userDeviceService.UpdateUserDeviceService(userDevice.ID, &device)
|
||||
if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@@ -1,134 +0,0 @@
|
||||
package user_controller
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
ginI18n "github.com/gin-contrib/i18n"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mssola/useragent"
|
||||
"gorm.io/gorm"
|
||||
"schisandra-cloud-album/common/constant"
|
||||
"schisandra-cloud-album/common/redis"
|
||||
"schisandra-cloud-album/common/result"
|
||||
"schisandra-cloud-album/global"
|
||||
"schisandra-cloud-album/model"
|
||||
"schisandra-cloud-album/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
// getUserLoginDevice 获取用户登录设备
|
||||
func getUserLoginDevice(user model.ScaAuthUser, c *gin.Context) bool {
|
||||
|
||||
// 检查user.UID是否为空
|
||||
if user.UID == nil {
|
||||
global.LOG.Errorln("user.UID is nil")
|
||||
return false
|
||||
}
|
||||
userAgent := c.GetHeader("User-Agent")
|
||||
if userAgent == "" {
|
||||
global.LOG.Errorln("user-agent is empty")
|
||||
return false
|
||||
}
|
||||
ua := useragent.New(userAgent)
|
||||
|
||||
ip := utils.GetClientIP(c)
|
||||
location, err := global.IP2Location.SearchByStr(ip)
|
||||
if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return false
|
||||
}
|
||||
location = utils.RemoveZeroAndAdjust(location)
|
||||
|
||||
isBot := ua.Bot()
|
||||
browser, browserVersion := ua.Browser()
|
||||
os := ua.OS()
|
||||
mobile := ua.Mobile()
|
||||
mozilla := ua.Mozilla()
|
||||
platform := ua.Platform()
|
||||
engine, engineVersion := ua.Engine()
|
||||
|
||||
device := model.ScaAuthUserDevice{
|
||||
UserID: user.UID,
|
||||
IP: &ip,
|
||||
Location: &location,
|
||||
Agent: userAgent,
|
||||
Browser: &browser,
|
||||
BrowserVersion: &browserVersion,
|
||||
OperatingSystem: &os,
|
||||
Mobile: &mobile,
|
||||
Bot: &isBot,
|
||||
Mozilla: &mozilla,
|
||||
Platform: &platform,
|
||||
EngineName: &engine,
|
||||
EngineVersion: &engineVersion,
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
userDevice, err := userDeviceService.GetUserDeviceByUIDIPAgentService(*user.UID, ip, userAgent)
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = userDeviceService.AddUserDeviceService(&device)
|
||||
if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return false
|
||||
}
|
||||
} else if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return false
|
||||
} else {
|
||||
err := userDeviceService.UpdateUserDeviceService(userDevice.ID, &device)
|
||||
if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// handelUserLogin 处理用户登录
|
||||
func handelUserLogin(user model.ScaAuthUser, autoLogin bool, c *gin.Context) {
|
||||
// 检查 user.UID 是否为 nil
|
||||
if user.UID == nil {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "ParamsError"), c)
|
||||
return
|
||||
}
|
||||
if !getUserLoginDevice(user, c) {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
|
||||
return
|
||||
}
|
||||
accessToken, err := utils.GenerateAccessToken(utils.AccessJWTPayload{UserID: user.UID})
|
||||
if err != nil {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
|
||||
return
|
||||
}
|
||||
|
||||
var days time.Duration
|
||||
if autoLogin {
|
||||
days = 7 * 24 * time.Hour
|
||||
} else {
|
||||
days = time.Minute * 30
|
||||
}
|
||||
|
||||
refreshToken, expiresAt := utils.GenerateRefreshToken(utils.RefreshJWTPayload{UserID: user.UID}, days)
|
||||
data := ResponseData{
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: refreshToken,
|
||||
ExpiresAt: expiresAt,
|
||||
UID: user.UID,
|
||||
}
|
||||
|
||||
err = redis.Set(constant.UserLoginTokenRedisKey+*user.UID, data, days).Err()
|
||||
if err != nil {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
|
||||
return
|
||||
}
|
||||
gob.Register(ResponseData{})
|
||||
err = utils.SetSession(c, constant.SessionKey, data)
|
||||
if err != nil {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
|
||||
return
|
||||
}
|
||||
result.OkWithData(data, c)
|
||||
}
|
@@ -1,8 +1,10 @@
|
||||
package user_controller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
ginI18n "github.com/gin-contrib/i18n"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mssola/useragent"
|
||||
"github.com/yitter/idgenerator-go/idgen"
|
||||
"gorm.io/gorm"
|
||||
"reflect"
|
||||
@@ -141,7 +143,12 @@ func (UserController) AccountLogin(c *gin.Context) {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "PasswordError"), c)
|
||||
return
|
||||
}
|
||||
handelUserLogin(user, accountLoginRequest.AutoLogin, c)
|
||||
data, res := userService.HandelUserLogin(user, accountLoginRequest.AutoLogin, c)
|
||||
if !res {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
|
||||
return
|
||||
}
|
||||
result.OkWithData(data, c)
|
||||
}
|
||||
|
||||
// PhoneLogin 手机号登录/注册
|
||||
@@ -209,16 +216,21 @@ func (UserController) PhoneLogin(c *gin.Context) {
|
||||
|
||||
errChan := make(chan error)
|
||||
go func() {
|
||||
err := global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
addUser, err := userService.AddUserService(createUser)
|
||||
if err != nil {
|
||||
return err
|
||||
err = global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
addUser, w := userService.AddUserService(createUser)
|
||||
if w != nil {
|
||||
return w
|
||||
}
|
||||
_, err = global.Casbin.AddRoleForUser(uidStr, enum.User)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
handelUserLogin(*addUser, autoLogin, c)
|
||||
data, res := userService.HandelUserLogin(*addUser, autoLogin, c)
|
||||
if !res {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
|
||||
return errors.New("login failed")
|
||||
}
|
||||
result.OkWithData(data, c)
|
||||
return nil
|
||||
})
|
||||
errChan <- err
|
||||
@@ -249,7 +261,12 @@ func (UserController) PhoneLogin(c *gin.Context) {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "CaptchaError"), c)
|
||||
return
|
||||
}
|
||||
handelUserLogin(user, autoLogin, c)
|
||||
data, res := userService.HandelUserLogin(user, autoLogin, c)
|
||||
if !res {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
|
||||
return
|
||||
}
|
||||
result.OkWithData(data, c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -384,3 +401,65 @@ func (UserController) Logout(c *gin.Context) {
|
||||
}
|
||||
result.OkWithMessage(ginI18n.MustGetMessage(c, "LogoutSuccess"), c)
|
||||
}
|
||||
|
||||
// GetUserLoginDevice 获取用户登录设备
|
||||
func (UserController) GetUserLoginDevice(c *gin.Context) {
|
||||
userId := c.Query("user_id")
|
||||
if userId == "" {
|
||||
return
|
||||
}
|
||||
userAgent := c.GetHeader("User-Agent")
|
||||
if userAgent == "" {
|
||||
global.LOG.Errorln("user-agent is empty")
|
||||
return
|
||||
}
|
||||
ua := useragent.New(userAgent)
|
||||
|
||||
ip := utils.GetClientIP(c)
|
||||
location, err := global.IP2Location.SearchByStr(ip)
|
||||
location = utils.RemoveZeroAndAdjust(location)
|
||||
if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return
|
||||
}
|
||||
isBot := ua.Bot()
|
||||
browser, browserVersion := ua.Browser()
|
||||
os := ua.OS()
|
||||
mobile := ua.Mobile()
|
||||
mozilla := ua.Mozilla()
|
||||
platform := ua.Platform()
|
||||
engine, engineVersion := ua.Engine()
|
||||
device := model.ScaAuthUserDevice{
|
||||
UserID: &userId,
|
||||
IP: &ip,
|
||||
Location: &location,
|
||||
Agent: userAgent,
|
||||
Browser: &browser,
|
||||
BrowserVersion: &browserVersion,
|
||||
OperatingSystem: &os,
|
||||
Mobile: &mobile,
|
||||
Bot: &isBot,
|
||||
Mozilla: &mozilla,
|
||||
Platform: &platform,
|
||||
EngineName: &engine,
|
||||
EngineVersion: &engineVersion,
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
userDevice, err := userDeviceService.GetUserDeviceByUIDIPAgentService(userId, ip, userAgent)
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = userDeviceService.AddUserDeviceService(&device)
|
||||
if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
} else {
|
||||
err := userDeviceService.UpdateUserDeviceService(userDevice.ID, &device)
|
||||
if err != nil {
|
||||
global.LOG.Errorln(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user