🎨 remove ent orm & add xorm
This commit is contained in:
@@ -5,8 +5,8 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logc"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"xorm.io/xorm"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/captcha/verify"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/constant"
|
||||
@@ -15,8 +15,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent/scaauthuser"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
)
|
||||
|
||||
type AccountLoginLogic struct {
|
||||
@@ -38,44 +37,43 @@ func (l *AccountLoginLogic) AccountLogin(w http.ResponseWriter, r *http.Request,
|
||||
if !verifyResult {
|
||||
return response.ErrorWithI18n(l.ctx, "captcha.verificationFailure"), nil
|
||||
}
|
||||
var user *ent.ScaAuthUser
|
||||
var query *ent.ScaAuthUserQuery
|
||||
var user model.ScaAuthUser
|
||||
var query *xorm.Session
|
||||
|
||||
switch {
|
||||
case utils.IsPhone(req.Account):
|
||||
query = l.svcCtx.MySQLClient.ScaAuthUser.Query().Where(scaauthuser.PhoneEQ(req.Account), scaauthuser.DeletedEQ(0))
|
||||
query = l.svcCtx.DB.Where("phone = ? AND deleted = ?", req.Account, 0)
|
||||
case utils.IsEmail(req.Account):
|
||||
query = l.svcCtx.MySQLClient.ScaAuthUser.Query().Where(scaauthuser.EmailEQ(req.Account), scaauthuser.DeletedEQ(0))
|
||||
query = l.svcCtx.DB.Where("email = ? AND deleted = ?", req.Account, 0)
|
||||
case utils.IsUsername(req.Account):
|
||||
query = l.svcCtx.MySQLClient.ScaAuthUser.Query().Where(scaauthuser.UsernameEQ(req.Account), scaauthuser.DeletedEQ(0))
|
||||
query = l.svcCtx.DB.Where("username = ? AND deleted = ?", req.Account, 0)
|
||||
default:
|
||||
return response.ErrorWithI18n(l.ctx, "login.invalidAccount"), nil
|
||||
}
|
||||
|
||||
user, err = query.First(l.ctx)
|
||||
has, err := query.Get(&user)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return response.ErrorWithI18n(l.ctx, "login.userNotRegistered"), nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !has {
|
||||
return response.ErrorWithI18n(l.ctx, "login.userNotRegistered"), nil
|
||||
}
|
||||
|
||||
if !utils.Verify(user.Password, req.Password) {
|
||||
return response.ErrorWithI18n(l.ctx, "login.invalidPassword"), nil
|
||||
}
|
||||
data, result := HandleUserLogin(user, l.svcCtx, req.AutoLogin, r, w, l.ctx)
|
||||
if !result {
|
||||
return response.ErrorWithI18n(l.ctx, "login.loginFailed"), nil
|
||||
data, err := HandleUserLogin(user, l.svcCtx, req.AutoLogin, r, w, l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 记录用户登录设备
|
||||
if !GetUserLoginDevice(user.UID, r, l.svcCtx.Ip2Region, l.svcCtx.MySQLClient, l.ctx) {
|
||||
return response.ErrorWithI18n(l.ctx, "login.loginFailed"), nil
|
||||
if err = GetUserLoginDevice(user.UID, r, l.svcCtx.Ip2Region, l.svcCtx.DB, l.ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.SuccessWithData(data), nil
|
||||
}
|
||||
|
||||
// HandleUserLogin 处理用户登录
|
||||
func HandleUserLogin(user *ent.ScaAuthUser, svcCtx *svc.ServiceContext, autoLogin bool, r *http.Request, w http.ResponseWriter, ctx context.Context) (*types.LoginResponse, bool) {
|
||||
func HandleUserLogin(user model.ScaAuthUser, svcCtx *svc.ServiceContext, autoLogin bool, r *http.Request, w http.ResponseWriter, ctx context.Context) (*types.LoginResponse, error) {
|
||||
// 生成jwt token
|
||||
accessToken := jwt.GenerateAccessToken(svcCtx.Config.Auth.AccessSecret, jwt.AccessJWTPayload{
|
||||
UserID: user.UID,
|
||||
@@ -105,20 +103,18 @@ func HandleUserLogin(user *ent.ScaAuthUser, svcCtx *svc.ServiceContext, autoLogi
|
||||
}
|
||||
err := svcCtx.RedisClient.Set(ctx, constant.UserTokenPrefix+user.UID, redisToken, days).Err()
|
||||
if err != nil {
|
||||
logc.Error(ctx, err)
|
||||
return nil, false
|
||||
return nil, err
|
||||
}
|
||||
session, err := svcCtx.Session.Get(r, constant.SESSION_KEY)
|
||||
if err != nil {
|
||||
logc.Error(ctx, err)
|
||||
return nil, false
|
||||
return nil, err
|
||||
}
|
||||
session.Values["refresh_token"] = refreshToken
|
||||
session.Values["uid"] = user.UID
|
||||
err = session.Save(r, w)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
return nil, err
|
||||
}
|
||||
return &data, true
|
||||
return &data, nil
|
||||
|
||||
}
|
||||
|
@@ -8,12 +8,12 @@ import (
|
||||
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
||||
"github.com/mssola/useragent"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"xorm.io/xorm"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/constant"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent/scaauthuserdevice"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
)
|
||||
|
||||
type GetUserDeviceLogic struct {
|
||||
@@ -40,23 +40,22 @@ func (l *GetUserDeviceLogic) GetUserDevice(r *http.Request) error {
|
||||
return errors.New("user session not found")
|
||||
}
|
||||
|
||||
res := GetUserLoginDevice(uid, r, l.svcCtx.Ip2Region, l.svcCtx.MySQLClient, l.ctx)
|
||||
if !res {
|
||||
return errors.New("user device not found")
|
||||
if err = GetUserLoginDevice(uid, r, l.svcCtx.Ip2Region, l.svcCtx.DB, l.ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserLoginDevice 获取用户登录设备
|
||||
func GetUserLoginDevice(userId string, r *http.Request, ip2location *xdb.Searcher, entClient *ent.Client, ctx context.Context) bool {
|
||||
func GetUserLoginDevice(userId string, r *http.Request, ip2location *xdb.Searcher, db *xorm.Engine, ctx context.Context) error {
|
||||
userAgent := r.Header.Get("User-Agent")
|
||||
if userAgent == "" {
|
||||
return false
|
||||
return errors.New("user agent not found")
|
||||
}
|
||||
ip := utils.GetClientIP(r)
|
||||
location, err := ip2location.SearchByStr(ip)
|
||||
if err != nil {
|
||||
return false
|
||||
return err
|
||||
}
|
||||
location = utils.RemoveZeroAndAdjust(location)
|
||||
|
||||
@@ -69,57 +68,54 @@ func GetUserLoginDevice(userId string, r *http.Request, ip2location *xdb.Searche
|
||||
platform := ua.Platform()
|
||||
engine, engineVersion := ua.Engine()
|
||||
|
||||
device, err := entClient.ScaAuthUserDevice.Query().
|
||||
Where(scaauthuserdevice.UserID(userId), scaauthuserdevice.IP(ip), scaauthuserdevice.Agent(userAgent)).
|
||||
Only(ctx)
|
||||
var device model.ScaAuthUserDevice
|
||||
has, err := db.Where("user_id = ? AND ip = ? AND agent = ?", userId, ip, userAgent).Get(&device)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 如果有错误,表示设备不存在,执行插入
|
||||
if ent.IsNotFound(err) {
|
||||
if !has {
|
||||
// 创建新的设备记录
|
||||
err = entClient.ScaAuthUserDevice.Create().
|
||||
SetUserID(userId).
|
||||
SetBot(isBot).
|
||||
SetAgent(userAgent).
|
||||
SetBrowser(browser).
|
||||
SetBrowserVersion(browserVersion).
|
||||
SetEngineName(engine).
|
||||
SetEngineVersion(engineVersion).
|
||||
SetIP(ip).
|
||||
SetLocation(location).
|
||||
SetOperatingSystem(os).
|
||||
SetMobile(mobile).
|
||||
SetMozilla(mozilla).
|
||||
SetPlatform(platform).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return false
|
||||
newDevice := &model.ScaAuthUserDevice{
|
||||
UserId: userId,
|
||||
Bot: isBot,
|
||||
Agent: userAgent,
|
||||
Browser: browser,
|
||||
BrowserVersion: browserVersion,
|
||||
EngineName: engine,
|
||||
EngineVersion: engineVersion,
|
||||
Ip: ip,
|
||||
Location: location,
|
||||
OperatingSystem: os,
|
||||
Mobile: mobile,
|
||||
Mozilla: mozilla,
|
||||
Platform: platform,
|
||||
}
|
||||
return true
|
||||
} else if err == nil {
|
||||
// 如果设备存在,执行更新
|
||||
err = device.Update().
|
||||
SetUserID(userId).
|
||||
SetBot(isBot).
|
||||
SetAgent(userAgent).
|
||||
SetBrowser(browser).
|
||||
SetBrowserVersion(browserVersion).
|
||||
SetEngineName(engine).
|
||||
SetEngineVersion(engineVersion).
|
||||
SetIP(ip).
|
||||
SetLocation(location).
|
||||
SetOperatingSystem(os).
|
||||
SetMobile(mobile).
|
||||
SetMozilla(mozilla).
|
||||
SetPlatform(platform).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return false
|
||||
|
||||
affected, err := db.Insert(newDevice)
|
||||
if err != nil || affected == 0 {
|
||||
return errors.New("create user device failed")
|
||||
}
|
||||
return true
|
||||
return nil
|
||||
} else {
|
||||
logx.Error(err)
|
||||
return false
|
||||
// 如果设备存在,执行更新
|
||||
device.Bot = isBot
|
||||
device.Agent = userAgent
|
||||
device.Browser = browser
|
||||
device.BrowserVersion = browserVersion
|
||||
device.EngineName = engine
|
||||
device.EngineVersion = engineVersion
|
||||
device.Ip = ip
|
||||
device.Location = location
|
||||
device.OperatingSystem = os
|
||||
device.Mobile = mobile
|
||||
device.Mozilla = mozilla
|
||||
device.Platform = platform
|
||||
|
||||
affected, err := db.ID(device.Id).Update(&device)
|
||||
if err != nil || affected == 0 {
|
||||
return errors.New("update user device failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -14,8 +15,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent/scaauthuser"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
)
|
||||
|
||||
type PhoneLoginLogic struct {
|
||||
@@ -43,64 +43,68 @@ func (l *PhoneLoginLogic) PhoneLogin(r *http.Request, w http.ResponseWriter, req
|
||||
if req.Captcha != code {
|
||||
return response.ErrorWithI18n(l.ctx, "login.captchaError"), nil
|
||||
}
|
||||
user, err := l.svcCtx.MySQLClient.ScaAuthUser.Query().Where(scaauthuser.Phone(req.Phone), scaauthuser.Deleted(0)).First(l.ctx)
|
||||
tx, wrong := l.svcCtx.MySQLClient.Tx(l.ctx)
|
||||
if wrong != nil {
|
||||
authUser := model.ScaAuthUser{
|
||||
Phone: req.Phone,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
has, err := l.svcCtx.DB.Get(&authUser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ent.IsNotFound(err) {
|
||||
tx := l.svcCtx.DB.NewSession()
|
||||
defer tx.Close()
|
||||
if err = tx.Begin(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !has {
|
||||
uid := idgen.NextId()
|
||||
uidStr := strconv.FormatInt(uid, 10)
|
||||
avatar := utils.GenerateAvatar(uidStr)
|
||||
name := randomname.GenerateName()
|
||||
|
||||
addUser, fault := l.svcCtx.MySQLClient.ScaAuthUser.Create().
|
||||
SetUID(uidStr).
|
||||
SetPhone(req.Phone).
|
||||
SetAvatar(avatar).
|
||||
SetNickname(name).
|
||||
SetDeleted(constant.NotDeleted).
|
||||
SetGender(constant.Male).
|
||||
Save(l.ctx)
|
||||
if fault != nil {
|
||||
err = tx.Rollback()
|
||||
return nil, err
|
||||
user := model.ScaAuthUser{
|
||||
UID: uidStr,
|
||||
Phone: req.Phone,
|
||||
Avatar: avatar,
|
||||
Nickname: name,
|
||||
Deleted: constant.NotDeleted,
|
||||
Gender: constant.Male,
|
||||
}
|
||||
insert, err := tx.Insert(&user)
|
||||
if err != nil || insert == 0 {
|
||||
return nil, errors.New("register failed")
|
||||
}
|
||||
_, err = l.svcCtx.CasbinEnforcer.AddRoleForUser(uidStr, constant.User)
|
||||
if err != nil {
|
||||
err = tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
data, result := HandleUserLogin(addUser, l.svcCtx, req.AutoLogin, r, w, l.ctx)
|
||||
if !result {
|
||||
err = tx.Rollback()
|
||||
return response.ErrorWithI18n(l.ctx, "login.registerError"), err
|
||||
data, err := HandleUserLogin(user, l.svcCtx, req.AutoLogin, r, w, l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 记录用户登录设备
|
||||
if !GetUserLoginDevice(addUser.UID, r, l.svcCtx.Ip2Region, l.svcCtx.MySQLClient, l.ctx) {
|
||||
return response.ErrorWithI18n(l.ctx, "login.registerError"), nil
|
||||
if err = GetUserLoginDevice(user.UID, r, l.svcCtx.Ip2Region, l.svcCtx.DB, l.ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
return response.SuccessWithData(data), nil
|
||||
} else if err == nil {
|
||||
data, result := HandleUserLogin(user, l.svcCtx, req.AutoLogin, r, w, l.ctx)
|
||||
if !result {
|
||||
err = tx.Rollback()
|
||||
return response.ErrorWithI18n(l.ctx, "login.loginFailed"), err
|
||||
}
|
||||
// 记录用户登录设备
|
||||
if !GetUserLoginDevice(user.UID, r, l.svcCtx.Ip2Region, l.svcCtx.MySQLClient, l.ctx) {
|
||||
return response.ErrorWithI18n(l.ctx, "login.loginFailed"), nil
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
return response.SuccessWithData(data), nil
|
||||
} else {
|
||||
return response.ErrorWithI18n(l.ctx, "login.loginFailed"), nil
|
||||
data, err := HandleUserLogin(authUser, l.svcCtx, req.AutoLogin, r, w, l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 记录用户登录设备
|
||||
if err = GetUserLoginDevice(authUser.UID, r, l.svcCtx.Ip2Region, l.svcCtx.DB, l.ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.SuccessWithData(data), nil
|
||||
}
|
||||
}
|
||||
|
@@ -8,8 +8,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent/scaauthuser"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -49,17 +48,28 @@ func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordRequest) (res
|
||||
if err = l.svcCtx.RedisClient.Del(l.ctx, constant.UserSmsRedisPrefix+req.Phone).Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user, err := l.svcCtx.MySQLClient.ScaAuthUser.Query().Where(scaauthuser.Phone(req.Phone), scaauthuser.Deleted(constant.NotDeleted)).First(l.ctx)
|
||||
if err != nil && ent.IsNotFound(err) {
|
||||
authUser := model.ScaAuthUser{
|
||||
Phone: req.Phone,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
has, err := l.svcCtx.DB.Get(&authUser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !has {
|
||||
return response.ErrorWithI18n(l.ctx, "login.userNotRegistered"), nil
|
||||
}
|
||||
encrypt, err := utils.Encrypt(req.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = user.Update().SetPassword(encrypt).Exec(l.ctx)
|
||||
|
||||
affected, err := l.svcCtx.DB.ID(authUser.Id).Cols("password").Update(&model.ScaAuthUser{Password: encrypt})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if affected == 0 {
|
||||
return response.ErrorWithI18n(l.ctx, "login.resetPasswordError"), nil
|
||||
}
|
||||
return response.Success(), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user