♻️ update code structure

This commit is contained in:
landaiqing
2024-10-01 17:31:05 +08:00
parent 4ec1ea40bb
commit fcbbbbabff
16 changed files with 491 additions and 322 deletions

View File

@@ -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
}
}