init: init refactor
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
"sundynix-micro-go/common/utils/hash"
|
||||
"sundynix-micro-go/common/utils/uniqueid"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CreateUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
|
||||
return &CreateUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
func (l *CreateUserLogic) CreateUser(in *user.CreateUserReq) (*user.CreateUserResp, error) {
|
||||
// 如果有OpenID,先检查是否存在
|
||||
if in.OpenId != "" {
|
||||
var existing model.SundynixUser
|
||||
err := l.svcCtx.DB.Where("open_id = ?", in.OpenId).First(&existing).Error
|
||||
if err == nil {
|
||||
// 用户已存在,更新session_key
|
||||
if in.SessionKey != "" {
|
||||
l.svcCtx.DB.Model(&existing).UpdateColumn("session_key", in.SessionKey)
|
||||
}
|
||||
return &user.CreateUserResp{
|
||||
User: convertUserToProto(&existing),
|
||||
}, nil
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
l.Errorf("查询用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询用户失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 创建新用户
|
||||
name := in.Name
|
||||
if name == "" {
|
||||
name = uniqueid.GenerateName("用户")
|
||||
}
|
||||
|
||||
newUser := model.SundynixUser{
|
||||
Name: name,
|
||||
OpenID: in.OpenId,
|
||||
SessionKey: in.SessionKey,
|
||||
ClientID: in.ClientId,
|
||||
Phone: in.Phone,
|
||||
}
|
||||
|
||||
// 如果有密码则加密
|
||||
if in.Phone != "" {
|
||||
newUser.Password = hash.BcryptHash(in.Phone) // 默认密码为手机号
|
||||
}
|
||||
|
||||
if err := l.svcCtx.DB.Create(&newUser).Error; err != nil {
|
||||
l.Errorf("创建用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "创建用户失败")
|
||||
}
|
||||
|
||||
return &user.CreateUserResp{
|
||||
User: convertUserToProto(&newUser),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GetUserByIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetUserByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByIdLogic {
|
||||
return &GetUserByIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID获取用户信息
|
||||
func (l *GetUserByIdLogic) GetUserById(in *user.GetUserByIdReq) (*user.GetUserByIdResp, error) {
|
||||
var u model.SundynixUser
|
||||
err := l.svcCtx.DB.Where("id = ?", in.Id).First(&u).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, status.Error(codes.NotFound, "用户不存在")
|
||||
}
|
||||
l.Errorf("查询用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询用户失败")
|
||||
}
|
||||
|
||||
return &user.GetUserByIdResp{
|
||||
User: convertUserToProto(&u),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// convertUserToProto 将GORM模型转换为proto消息
|
||||
func convertUserToProto(u *model.SundynixUser) *user.UserInfo {
|
||||
info := &user.UserInfo{
|
||||
Id: u.ID,
|
||||
TenantId: u.TenantID,
|
||||
ClientId: u.ClientID,
|
||||
Name: u.Name,
|
||||
Account: u.Account,
|
||||
NickName: u.NickName,
|
||||
Phone: u.Phone,
|
||||
SessionKey: u.SessionKey,
|
||||
UnionId: u.UnionID,
|
||||
OpenId: u.OpenID,
|
||||
SaOpenId: u.SaOpenID,
|
||||
AvatarId: u.AvatarID,
|
||||
Gender: int32(u.Gender),
|
||||
CreatedAt: u.CreatedAt.Unix(),
|
||||
UpdatedAt: u.UpdatedAt.Unix(),
|
||||
}
|
||||
if u.LastLoginAt != nil {
|
||||
info.LastLoginAt = u.LastLoginAt.Unix()
|
||||
}
|
||||
return info
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GetUserByOpenIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetUserByOpenIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByOpenIdLogic {
|
||||
return &GetUserByOpenIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 根据OpenId获取用户信息
|
||||
func (l *GetUserByOpenIdLogic) GetUserByOpenId(in *user.GetUserByOpenIdReq) (*user.GetUserByOpenIdResp, error) {
|
||||
var u model.SundynixUser
|
||||
err := l.svcCtx.DB.Where("open_id = ?", in.OpenId).First(&u).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, status.Error(codes.NotFound, "用户不存在")
|
||||
}
|
||||
l.Errorf("查询用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询用户失败")
|
||||
}
|
||||
|
||||
return &user.GetUserByOpenIdResp{
|
||||
User: convertUserToProto(&u),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type UpdateUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
|
||||
return &UpdateUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
func (l *UpdateUserLogic) UpdateUser(in *user.UpdateUserReq) (*user.CommonResp, error) {
|
||||
var u model.SundynixUser
|
||||
if err := l.svcCtx.DB.Where("id = ?", in.Id).First(&u).Error; err != nil {
|
||||
return nil, status.Error(codes.NotFound, "用户不存在")
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{}
|
||||
if in.Name != "" {
|
||||
updates["name"] = in.Name
|
||||
}
|
||||
if in.Account != "" {
|
||||
updates["account"] = in.Account
|
||||
}
|
||||
if in.Phone != "" {
|
||||
updates["phone"] = in.Phone
|
||||
}
|
||||
if in.AvatarId != "" {
|
||||
updates["avatar_id"] = in.AvatarId
|
||||
}
|
||||
if in.NickName != "" {
|
||||
updates["nick_name"] = in.NickName
|
||||
}
|
||||
|
||||
if len(updates) > 0 {
|
||||
if err := l.svcCtx.DB.Model(&u).Updates(updates).Error; err != nil {
|
||||
l.Errorf("更新用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "更新用户失败")
|
||||
}
|
||||
}
|
||||
|
||||
return &user.CommonResp{
|
||||
Code: 200,
|
||||
Msg: "更新成功",
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type VerifyTokenLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewVerifyTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VerifyTokenLogic {
|
||||
return &VerifyTokenLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 验证Token有效性
|
||||
func (l *VerifyTokenLogic) VerifyToken(in *user.VerifyTokenReq) (*user.VerifyTokenResp, error) {
|
||||
claims, err := l.svcCtx.JWT.ParseToken(in.Token)
|
||||
if err != nil {
|
||||
return &user.VerifyTokenResp{
|
||||
Valid: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 检查Redis黑名单
|
||||
if l.svcCtx.Redis != nil {
|
||||
blacklistKey := "jwt:blacklist:" + claims.BaseClaims.ID
|
||||
val, _ := l.svcCtx.Redis.Get(l.ctx, blacklistKey).Result()
|
||||
if val == in.Token {
|
||||
return &user.VerifyTokenResp{
|
||||
Valid: false,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return &user.VerifyTokenResp{
|
||||
Valid: true,
|
||||
UserId: claims.BaseClaims.ID,
|
||||
Account: claims.BaseClaims.Account,
|
||||
ExpiresAt: claims.ExpiresAt.Unix(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user