🎨 remove ent orm & add xorm
This commit is contained in:
@@ -3,11 +3,10 @@ package comment
|
||||
import (
|
||||
"context"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/response"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetCommentListLogic struct {
|
||||
@@ -25,7 +24,12 @@ func NewGetCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
|
||||
}
|
||||
|
||||
func (l *GetCommentListLogic) GetCommentList(req *types.CommentListRequest) (resp *types.Response, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 获取用户ID
|
||||
// uid, ok := l.ctx.Value("user_id").(string)
|
||||
// if !ok {
|
||||
// return nil, errors.New("user_id not found in context")
|
||||
// }
|
||||
|
||||
return response.Success(), nil
|
||||
// 查询评论列表
|
||||
return
|
||||
}
|
||||
|
@@ -14,6 +14,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/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -57,11 +58,14 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
|
||||
browser, _ := ua.Browser()
|
||||
operatingSystem := ua.OS()
|
||||
isAuthor := 0
|
||||
session, wrong := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
|
||||
if wrong == nil {
|
||||
return nil, wrong
|
||||
session, err := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
|
||||
if err == nil {
|
||||
return nil, err
|
||||
}
|
||||
uid, ok := session.Values["uid"].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("uid not found in session")
|
||||
}
|
||||
uid := session.Values["uid"].(string)
|
||||
if uid == req.Author {
|
||||
isAuthor = 1
|
||||
}
|
||||
@@ -70,39 +74,36 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
|
||||
comment, err := l.svcCtx.MySQLClient.ScaCommentReply.Create().
|
||||
SetContent(commentContent).
|
||||
SetUserID(uid).
|
||||
SetTopicID(req.TopicId).
|
||||
SetCommentType(constant.CommentTopicType).
|
||||
SetCommentType(constant.COMMENT).
|
||||
SetAuthor(isAuthor).
|
||||
SetCommentIP(ip).
|
||||
SetLocation(location).
|
||||
SetBrowser(browser).
|
||||
SetOperatingSystem(operatingSystem).
|
||||
SetAgent(userAgent).Save(l.ctx)
|
||||
comment := model.ScaCommentReply{
|
||||
Content: commentContent,
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
TopicType: constant.CommentTopicType,
|
||||
CommentType: constant.COMMENT,
|
||||
Author: isAuthor,
|
||||
CommentIp: ip,
|
||||
Location: location,
|
||||
Browser: browser,
|
||||
OperatingSystem: operatingSystem,
|
||||
Agent: userAgent,
|
||||
}
|
||||
affected, err := l.svcCtx.DB.InsertOne(&comment)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if affected == 0 {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
|
||||
if len(req.Images) > 0 {
|
||||
imagesDataCh := make(chan [][]byte)
|
||||
go func() {
|
||||
imagesData, err := utils.ProcessImages(req.Images)
|
||||
if err != nil {
|
||||
imagesDataCh <- nil
|
||||
return
|
||||
}
|
||||
imagesDataCh <- imagesData
|
||||
}()
|
||||
imagesData := <-imagesDataCh
|
||||
if imagesData == nil {
|
||||
return nil, errors.New("process images failed")
|
||||
imagesData, err := utils.ProcessImages(req.Images)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commentImages := types.CommentImages{
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
CommentId: comment.ID,
|
||||
CommentId: comment.Id,
|
||||
Images: imagesData,
|
||||
CreatedAt: comment.CreatedAt.String(),
|
||||
}
|
||||
@@ -111,7 +112,7 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
|
||||
}
|
||||
}
|
||||
commentResponse := types.CommentResponse{
|
||||
Id: comment.ID,
|
||||
Id: comment.Id,
|
||||
Content: commentContent,
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
|
@@ -2,7 +2,9 @@ package comment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/mssola/useragent"
|
||||
|
||||
@@ -12,6 +14,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/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -53,20 +56,96 @@ func (l *SubmitReplyCommentLogic) SubmitReplyComment(r *http.Request, req *types
|
||||
|
||||
browser, _ := ua.Browser()
|
||||
operatingSystem := ua.OS()
|
||||
isAuthor := 0
|
||||
session, wrong := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
|
||||
if wrong == nil {
|
||||
return nil, wrong
|
||||
session, err := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uid := session.Values["uid"].(string)
|
||||
|
||||
uid, ok := session.Values["uid"].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("uid not found in session")
|
||||
}
|
||||
isAuthor := 0
|
||||
if uid == req.Author {
|
||||
isAuthor = 1
|
||||
}
|
||||
|
||||
xssFilterContent := utils.XssFilter(req.Content)
|
||||
if xssFilterContent == "" {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
|
||||
|
||||
return
|
||||
tx := l.svcCtx.DB.NewSession()
|
||||
defer tx.Close()
|
||||
if err = tx.Begin(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reply := model.ScaCommentReply{
|
||||
Content: commentContent,
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
TopicType: constant.CommentTopicType,
|
||||
CommentType: constant.COMMENT,
|
||||
Author: isAuthor,
|
||||
CommentIp: ip,
|
||||
Location: location,
|
||||
Browser: browser,
|
||||
OperatingSystem: operatingSystem,
|
||||
Agent: userAgent,
|
||||
ReplyId: req.ReplyId,
|
||||
ReplyUser: req.ReplyUser,
|
||||
}
|
||||
affected, err := tx.Insert(&reply)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if affected == 0 {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
update, err := tx.Table(model.ScaCommentReply{}).Where("id = ? and deleted = 0", req.ReplyId).Incr("reply_count", 1).Update(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if update == 0 {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
|
||||
if len(req.Images) > 0 {
|
||||
imagesData, err := utils.ProcessImages(req.Images)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
commentImages := types.CommentImages{
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
CommentId: reply.Id,
|
||||
Images: imagesData,
|
||||
CreatedAt: reply.CreatedAt.String(),
|
||||
}
|
||||
if _, err = l.svcCtx.MongoClient.Collection("comment_images").InsertOne(l.ctx, commentImages); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
commentResponse := types.CommentResponse{
|
||||
Id: reply.Id,
|
||||
Content: commentContent,
|
||||
UserId: uid,
|
||||
TopicId: reply.TopicId,
|
||||
Author: isAuthor,
|
||||
Location: location,
|
||||
Browser: browser,
|
||||
OperatingSystem: operatingSystem,
|
||||
CreatedTime: time.Now(),
|
||||
ReplyId: reply.ReplyId,
|
||||
ReplyUser: reply.ReplyUser,
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.SuccessWithData(commentResponse), nil
|
||||
}
|
||||
|
@@ -2,9 +2,19 @@ package comment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/mssola/useragent"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/captcha/verify"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/constant"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/response"
|
||||
"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/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -23,8 +33,133 @@ func NewSubmitReplyReplyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SubmitReplyReplyLogic) SubmitReplyReply(req *types.ReplyReplyRequest) (resp *types.Response, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
func (l *SubmitReplyReplyLogic) SubmitReplyReply(r *http.Request, req *types.ReplyReplyRequest) (resp *types.Response, err error) {
|
||||
// 验证验证码
|
||||
if !verify.VerifySlideCaptcha(l.ctx, l.svcCtx.RedisClient, req.Point, req.Key) {
|
||||
return response.ErrorWithI18n(l.ctx, "captcha.verificationFailure"), nil
|
||||
}
|
||||
|
||||
return
|
||||
// 检查图片数量
|
||||
if len(req.Images) > 3 {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.tooManyImages"), nil
|
||||
}
|
||||
|
||||
// 获取用户代理
|
||||
userAgent := r.Header.Get("User-Agent")
|
||||
if userAgent == "" {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
ua := useragent.New(userAgent)
|
||||
|
||||
// 获取客户端IP及位置信息
|
||||
ip := utils.GetClientIP(r)
|
||||
location, err := l.svcCtx.Ip2Region.SearchByStr(ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
location = utils.RemoveZeroAndAdjust(location)
|
||||
|
||||
// 获取浏览器与操作系统信息
|
||||
browser, _ := ua.Browser()
|
||||
operatingSystem := ua.OS()
|
||||
|
||||
// 获取用户会话信息
|
||||
session, err := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uid, ok := session.Values["uid"].(string)
|
||||
if !ok {
|
||||
return nil, errors.New("uid not found in session")
|
||||
}
|
||||
|
||||
// 判断作者身份
|
||||
isAuthor := 0
|
||||
if uid == req.Author {
|
||||
isAuthor = 1
|
||||
}
|
||||
|
||||
// XSS过滤
|
||||
xssFilterContent := utils.XssFilter(req.Content)
|
||||
if xssFilterContent == "" {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
|
||||
|
||||
tx := l.svcCtx.DB.NewSession()
|
||||
defer tx.Close()
|
||||
if err = tx.Begin(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
replyReply := model.ScaCommentReply{
|
||||
Content: commentContent,
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
TopicType: constant.CommentTopicType,
|
||||
CommentType: constant.COMMENT,
|
||||
Author: isAuthor,
|
||||
CommentIp: ip,
|
||||
Location: location,
|
||||
Browser: browser,
|
||||
OperatingSystem: operatingSystem,
|
||||
Agent: userAgent,
|
||||
ReplyId: req.ReplyId,
|
||||
ReplyUser: req.ReplyUser,
|
||||
ReplyTo: req.ReplyTo,
|
||||
}
|
||||
affected, err := tx.Insert(&replyReply)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if affected == 0 {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
update, err := tx.Table(model.ScaCommentReply{}).Where("id = ? and version = ? and deleted = 0", req.ReplyId, replyReply.Version).Incr("reply_count", 1).Update(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if update == 0 {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
|
||||
// 处理图片
|
||||
if len(req.Images) > 0 {
|
||||
imagesData, err := utils.ProcessImages(req.Images)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commentImages := types.CommentImages{
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
CommentId: replyReply.Id,
|
||||
Images: imagesData,
|
||||
CreatedAt: replyReply.CreatedAt.String(),
|
||||
}
|
||||
if _, err = l.svcCtx.MongoClient.Collection("comment_images").InsertOne(l.ctx, commentImages); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// 构建响应
|
||||
commentResponse := types.CommentResponse{
|
||||
Id: replyReply.Id,
|
||||
Content: commentContent,
|
||||
UserId: uid,
|
||||
TopicId: replyReply.TopicId,
|
||||
Author: isAuthor,
|
||||
Location: location,
|
||||
Browser: browser,
|
||||
OperatingSystem: operatingSystem,
|
||||
CreatedTime: time.Now(),
|
||||
ReplyId: replyReply.ReplyId,
|
||||
ReplyUser: replyReply.ReplyUser,
|
||||
ReplyTo: replyReply.ReplyTo,
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.SuccessWithData(commentResponse), nil
|
||||
}
|
||||
|
@@ -15,9 +15,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/logic/user"
|
||||
"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/ent/scaauthusersocial"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -102,81 +100,84 @@ func (l *GiteeCallbackLogic) GiteeCallback(w http.ResponseWriter, r *http.Reques
|
||||
if err = json.Unmarshal(marshal, &giteeUser); err != nil {
|
||||
return err
|
||||
}
|
||||
Id := strconv.Itoa(giteeUser.ID)
|
||||
|
||||
tx, err := l.svcCtx.MySQLClient.Tx(l.ctx)
|
||||
tx := l.svcCtx.DB.NewSession()
|
||||
defer tx.Close()
|
||||
if err = tx.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
userSocial := model.ScaAuthUserSocial{
|
||||
OpenId: Id,
|
||||
Source: constant.OAuthSourceGitee,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
has, err := tx.Get(&userSocial)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Id := strconv.Itoa(giteeUser.ID)
|
||||
|
||||
socialUser, err := l.svcCtx.MySQLClient.ScaAuthUserSocial.Query().
|
||||
Where(scaauthusersocial.OpenID(Id),
|
||||
scaauthusersocial.Source(constant.OAuthSourceGitee),
|
||||
scaauthusersocial.Deleted(constant.NotDeleted)).
|
||||
First(l.ctx)
|
||||
|
||||
if err != nil && !ent.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if ent.IsNotFound(err) {
|
||||
if !has {
|
||||
// 创建用户
|
||||
uid := idgen.NextId()
|
||||
uidStr := strconv.FormatInt(uid, 10)
|
||||
|
||||
addUser, fault := l.svcCtx.MySQLClient.ScaAuthUser.Create().
|
||||
SetUID(uidStr).
|
||||
SetAvatar(giteeUser.AvatarURL).
|
||||
SetUsername(giteeUser.Login).
|
||||
SetNickname(giteeUser.Name).
|
||||
SetBlog(giteeUser.Blog).
|
||||
SetEmail(giteeUser.Email).
|
||||
SetDeleted(constant.NotDeleted).
|
||||
SetGender(constant.Male).
|
||||
Save(l.ctx)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
addUser := model.ScaAuthUser{
|
||||
UID: uidStr,
|
||||
Avatar: giteeUser.AvatarURL,
|
||||
Username: giteeUser.Login,
|
||||
Nickname: giteeUser.Name,
|
||||
Blog: giteeUser.Blog,
|
||||
Email: giteeUser.Email,
|
||||
Deleted: constant.NotDeleted,
|
||||
Gender: constant.Male,
|
||||
}
|
||||
|
||||
if err = l.svcCtx.MySQLClient.ScaAuthUserSocial.Create().
|
||||
SetUserID(uidStr).
|
||||
SetOpenID(Id).
|
||||
SetSource(constant.OAuthSourceGitee).
|
||||
Exec(l.ctx); err != nil {
|
||||
return tx.Rollback()
|
||||
affected, err := tx.Insert(&addUser)
|
||||
if err != nil || affected == 0 {
|
||||
return err
|
||||
}
|
||||
socialUser := model.ScaAuthUserSocial{
|
||||
UserId: uidStr,
|
||||
OpenId: Id,
|
||||
Source: constant.OAuthSourceGitee,
|
||||
}
|
||||
insert, err := tx.Insert(&socialUser)
|
||||
if err != nil || insert == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
if res, err := l.svcCtx.CasbinEnforcer.AddRoleForUser(uidStr, constant.User); !res || err != nil {
|
||||
return tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
if result := HandleOauthLoginResponse(addUser, l.svcCtx, r, w, l.ctx); !result {
|
||||
return tx.Rollback()
|
||||
if err = HandleOauthLoginResponse(addUser, l.svcCtx, r, w, l.ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
sacAuthUser, fault := l.svcCtx.MySQLClient.ScaAuthUser.Query().
|
||||
Where(scaauthuser.UID(socialUser.UserID), scaauthuser.Deleted(constant.NotDeleted)).
|
||||
First(l.ctx)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
user := model.ScaAuthUser{
|
||||
UID: userSocial.UserId,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
have, err := tx.Get(&user)
|
||||
if err != nil || !have {
|
||||
return err
|
||||
}
|
||||
|
||||
if result := HandleOauthLoginResponse(sacAuthUser, l.svcCtx, r, w, l.ctx); !result {
|
||||
return tx.Rollback()
|
||||
if err = HandleOauthLoginResponse(user, l.svcCtx, r, w, l.ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return tx.Rollback()
|
||||
if err = tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandleOauthLoginResponse 处理登录响应
|
||||
func HandleOauthLoginResponse(scaAuthUser *ent.ScaAuthUser, svcCtx *svc.ServiceContext, r *http.Request, w http.ResponseWriter, ctx context.Context) bool {
|
||||
data, result := user.HandleUserLogin(scaAuthUser, svcCtx, true, r, w, ctx)
|
||||
if !result {
|
||||
return false
|
||||
func HandleOauthLoginResponse(scaAuthUser model.ScaAuthUser, svcCtx *svc.ServiceContext, r *http.Request, w http.ResponseWriter, ctx context.Context) error {
|
||||
data, err := user.HandleUserLogin(scaAuthUser, svcCtx, true, r, w, ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
responseData := response.SuccessWithData(data)
|
||||
formattedScript := fmt.Sprintf(Script, responseData, svcCtx.Config.Web.URL)
|
||||
@@ -187,9 +188,9 @@ func HandleOauthLoginResponse(scaAuthUser *ent.ScaAuthUser, svcCtx *svc.ServiceC
|
||||
|
||||
// 写入响应内容
|
||||
if _, writeErr := w.Write([]byte(formattedScript)); writeErr != nil {
|
||||
return false
|
||||
return writeErr
|
||||
}
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetGiteeTokenAuthUrl 获取Gitee token
|
||||
|
@@ -12,9 +12,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/constant"
|
||||
"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/ent/scaauthusersocial"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -102,70 +100,76 @@ func (l *GithubCallbackLogic) GithubCallback(w http.ResponseWriter, r *http.Requ
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx, err := l.svcCtx.MySQLClient.Tx(l.ctx)
|
||||
Id := strconv.Itoa(gitHubUser.ID)
|
||||
tx := l.svcCtx.DB.NewSession()
|
||||
defer tx.Close()
|
||||
if err = tx.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
userSocial := model.ScaAuthUserSocial{
|
||||
OpenId: Id,
|
||||
Source: constant.OAuthSourceGithub,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
has, err := tx.Get(&userSocial)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Id := strconv.Itoa(gitHubUser.ID)
|
||||
socialUser, err := l.svcCtx.MySQLClient.ScaAuthUserSocial.Query().
|
||||
Where(scaauthusersocial.OpenID(Id),
|
||||
scaauthusersocial.Source(constant.OAuthSourceGithub),
|
||||
scaauthusersocial.Deleted(constant.NotDeleted)).
|
||||
First(l.ctx)
|
||||
|
||||
if err != nil && !ent.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if ent.IsNotFound(err) {
|
||||
if !has {
|
||||
// 创建用户
|
||||
uid := idgen.NextId()
|
||||
uidStr := strconv.FormatInt(uid, 10)
|
||||
|
||||
addUser, fault := l.svcCtx.MySQLClient.ScaAuthUser.Create().
|
||||
SetUID(uidStr).
|
||||
SetAvatar(gitHubUser.AvatarURL).
|
||||
SetUsername(gitHubUser.Login).
|
||||
SetNickname(gitHubUser.Name).
|
||||
SetBlog(gitHubUser.Blog).
|
||||
SetEmail(gitHubUser.Email).
|
||||
SetDeleted(constant.NotDeleted).
|
||||
SetGender(constant.Male).
|
||||
Save(l.ctx)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
addUser := model.ScaAuthUser{
|
||||
UID: uidStr,
|
||||
Avatar: gitHubUser.AvatarURL,
|
||||
Username: gitHubUser.Login,
|
||||
Nickname: gitHubUser.Name,
|
||||
Blog: gitHubUser.Blog,
|
||||
Email: gitHubUser.Email,
|
||||
Deleted: constant.NotDeleted,
|
||||
Gender: constant.Male,
|
||||
}
|
||||
affected, err := tx.Insert(&addUser)
|
||||
if err != nil || affected == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = l.svcCtx.MySQLClient.ScaAuthUserSocial.Create().
|
||||
SetUserID(uidStr).
|
||||
SetOpenID(Id).
|
||||
SetSource(constant.OAuthSourceGithub).
|
||||
Exec(l.ctx); err != nil {
|
||||
return tx.Rollback()
|
||||
socialUser := model.ScaAuthUserSocial{
|
||||
UserId: uidStr,
|
||||
OpenId: Id,
|
||||
Source: constant.OAuthSourceGithub,
|
||||
}
|
||||
insert, err := tx.Insert(&socialUser)
|
||||
if err != nil || insert == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
if res, err := l.svcCtx.CasbinEnforcer.AddRoleForUser(uidStr, constant.User); !res || err != nil {
|
||||
return tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
if result := HandleOauthLoginResponse(addUser, l.svcCtx, r, w, l.ctx); !result {
|
||||
return tx.Rollback()
|
||||
if err = HandleOauthLoginResponse(addUser, l.svcCtx, r, w, l.ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
sacAuthUser, fault := l.svcCtx.MySQLClient.ScaAuthUser.Query().
|
||||
Where(scaauthuser.UID(socialUser.UserID), scaauthuser.Deleted(constant.NotDeleted)).
|
||||
First(l.ctx)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
user := model.ScaAuthUser{
|
||||
UID: userSocial.UserId,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
have, err := tx.Get(&user)
|
||||
if err != nil || !have {
|
||||
return err
|
||||
}
|
||||
|
||||
if result := HandleOauthLoginResponse(sacAuthUser, l.svcCtx, r, w, l.ctx); !result {
|
||||
return tx.Rollback()
|
||||
if err = HandleOauthLoginResponse(user, l.svcCtx, r, w, l.ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return tx.Rollback()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -12,9 +12,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/constant"
|
||||
"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/ent/scaauthusersocial"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -99,67 +97,74 @@ func (l *QqCallbackLogic) QqCallback(w http.ResponseWriter, r *http.Request, req
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx, err := l.svcCtx.MySQLClient.Tx(l.ctx)
|
||||
|
||||
tx := l.svcCtx.DB.NewSession()
|
||||
defer tx.Close()
|
||||
if err = tx.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
userSocial := model.ScaAuthUserSocial{
|
||||
OpenId: authQQme.OpenID,
|
||||
Source: constant.OAuthSourceQQ,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
has, err := tx.Get(&userSocial)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
socialUser, err := l.svcCtx.MySQLClient.ScaAuthUserSocial.Query().
|
||||
Where(scaauthusersocial.OpenID(authQQme.OpenID),
|
||||
scaauthusersocial.Source(constant.OAuthSourceQQ),
|
||||
scaauthusersocial.Deleted(constant.NotDeleted)).
|
||||
First(l.ctx)
|
||||
|
||||
if err != nil && !ent.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if ent.IsNotFound(err) {
|
||||
if !has {
|
||||
// 创建用户
|
||||
uid := idgen.NextId()
|
||||
uidStr := strconv.FormatInt(uid, 10)
|
||||
|
||||
addUser, fault := l.svcCtx.MySQLClient.ScaAuthUser.Create().
|
||||
SetUID(uidStr).
|
||||
SetAvatar(qqUserInfo.FigureurlQq1).
|
||||
SetUsername(authQQme.OpenID).
|
||||
SetNickname(qqUserInfo.Nickname).
|
||||
SetDeleted(constant.NotDeleted).
|
||||
SetGender(constant.Male).
|
||||
Save(l.ctx)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
addUser := model.ScaAuthUser{
|
||||
UID: uidStr,
|
||||
Avatar: qqUserInfo.FigureurlQq1,
|
||||
Username: authQQme.OpenID,
|
||||
Nickname: qqUserInfo.Nickname,
|
||||
Deleted: constant.NotDeleted,
|
||||
Gender: constant.Male,
|
||||
}
|
||||
affected, err := tx.Insert(&addUser)
|
||||
if err != nil || affected == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = l.svcCtx.MySQLClient.ScaAuthUserSocial.Create().
|
||||
SetUserID(uidStr).
|
||||
SetOpenID(authQQme.OpenID).
|
||||
SetSource(constant.OAuthSourceQQ).
|
||||
Exec(l.ctx); err != nil {
|
||||
return tx.Rollback()
|
||||
socialUser := model.ScaAuthUserSocial{
|
||||
UserId: uidStr,
|
||||
OpenId: authQQme.OpenID,
|
||||
Source: constant.OAuthSourceGithub,
|
||||
}
|
||||
insert, err := tx.Insert(&socialUser)
|
||||
if err != nil || insert == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
if res, err := l.svcCtx.CasbinEnforcer.AddRoleForUser(uidStr, constant.User); !res || err != nil {
|
||||
return tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
if result := HandleOauthLoginResponse(addUser, l.svcCtx, r, w, l.ctx); !result {
|
||||
return tx.Rollback()
|
||||
if err = HandleOauthLoginResponse(addUser, l.svcCtx, r, w, l.ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
sacAuthUser, fault := l.svcCtx.MySQLClient.ScaAuthUser.Query().
|
||||
Where(scaauthuser.UID(socialUser.UserID), scaauthuser.Deleted(constant.NotDeleted)).
|
||||
First(l.ctx)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
user := model.ScaAuthUser{
|
||||
UID: userSocial.UserId,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
have, err := tx.Get(&user)
|
||||
if err != nil || !have {
|
||||
return err
|
||||
}
|
||||
|
||||
if result := HandleOauthLoginResponse(sacAuthUser, l.svcCtx, r, w, l.ctx); !result {
|
||||
return tx.Rollback()
|
||||
if err = HandleOauthLoginResponse(user, l.svcCtx, r, w, l.ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return tx.Rollback()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -23,9 +23,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/logic/user"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/logic/websocket"
|
||||
"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/scaauthuser"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/ent/scaauthusersocial"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
)
|
||||
|
||||
type WechatCallbackLogic struct {
|
||||
@@ -115,86 +113,92 @@ func (l *WechatCallbackLogic) HandlerWechatLogin(openId string, clientId string,
|
||||
if openId == "" {
|
||||
return errors.New("openId is empty")
|
||||
}
|
||||
socialUser, err := l.svcCtx.MySQLClient.ScaAuthUserSocial.Query().
|
||||
Where(scaauthusersocial.OpenID(openId),
|
||||
scaauthusersocial.Source(constant.OAuthSourceWechat),
|
||||
scaauthusersocial.Deleted(constant.NotDeleted)).
|
||||
First(l.ctx)
|
||||
|
||||
if err != nil && !ent.IsNotFound(err) {
|
||||
tx := l.svcCtx.DB.NewSession()
|
||||
defer tx.Close()
|
||||
if err := tx.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
tx, err := l.svcCtx.MySQLClient.Tx(l.ctx)
|
||||
userSocial := model.ScaAuthUserSocial{
|
||||
OpenId: openId,
|
||||
Source: constant.OAuthSourceWechat,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
has, err := tx.Get(&userSocial)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ent.IsNotFound(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).
|
||||
SetAvatar(avatar).
|
||||
SetUsername(openId).
|
||||
SetNickname(name).
|
||||
SetDeleted(constant.NotDeleted).
|
||||
SetGender(constant.Male).
|
||||
Save(l.ctx)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
addUser := model.ScaAuthUser{
|
||||
UID: uidStr,
|
||||
Avatar: avatar,
|
||||
Username: openId,
|
||||
Nickname: name,
|
||||
Deleted: constant.NotDeleted,
|
||||
Gender: constant.Male,
|
||||
}
|
||||
affected, err := tx.Insert(&addUser)
|
||||
if err != nil || affected == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = l.svcCtx.MySQLClient.ScaAuthUserSocial.Create().
|
||||
SetUserID(uidStr).
|
||||
SetOpenID(openId).
|
||||
SetSource(constant.OAuthSourceWechat).
|
||||
Exec(l.ctx); err != nil {
|
||||
return tx.Rollback()
|
||||
socialUser := model.ScaAuthUserSocial{
|
||||
UserId: uidStr,
|
||||
OpenId: openId,
|
||||
Source: constant.OAuthSourceGithub,
|
||||
}
|
||||
insert, err := tx.Insert(&socialUser)
|
||||
if err != nil || insert == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
if res, err := l.svcCtx.CasbinEnforcer.AddRoleForUser(uidStr, constant.User); !res || err != nil {
|
||||
return tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
data, result := user.HandleUserLogin(addUser, l.svcCtx, true, r, w, l.ctx)
|
||||
if !result {
|
||||
return tx.Rollback()
|
||||
data, err := user.HandleUserLogin(addUser, l.svcCtx, true, r, w, l.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
marshal, fault := json.Marshal(data)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
marshal, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = websocket.QrcodeWebSocketHandler.SendMessageToClient(clientId, marshal)
|
||||
if err != nil {
|
||||
return tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
} else {
|
||||
sacAuthUser, fault := l.svcCtx.MySQLClient.ScaAuthUser.Query().
|
||||
Where(scaauthuser.UID(socialUser.UserID), scaauthuser.Deleted(constant.NotDeleted)).
|
||||
First(l.ctx)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
authUser := model.ScaAuthUser{
|
||||
UID: userSocial.UserId,
|
||||
Deleted: constant.NotDeleted,
|
||||
}
|
||||
have, err := tx.Get(&authUser)
|
||||
if err != nil || !have {
|
||||
return err
|
||||
}
|
||||
|
||||
data, result := user.HandleUserLogin(sacAuthUser, l.svcCtx, true, r, w, l.ctx)
|
||||
if !result {
|
||||
return tx.Rollback()
|
||||
data, err := user.HandleUserLogin(authUser, l.svcCtx, true, r, w, l.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
marshal, fault := json.Marshal(data)
|
||||
if fault != nil {
|
||||
return tx.Rollback()
|
||||
marshal, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = websocket.QrcodeWebSocketHandler.SendMessageToClient(clientId, marshal)
|
||||
if err != nil {
|
||||
return tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return tx.Rollback()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
|
@@ -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