first commit
This commit is contained in:
106
app/main/api/internal/logic/user/bindmobilelogic.go
Normal file
106
app/main/api/internal/logic/user/bindmobilelogic.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"znc-server/app/main/api/internal/svc"
|
||||
"znc-server/app/main/api/internal/types"
|
||||
"znc-server/app/main/model"
|
||||
"znc-server/common/ctxdata"
|
||||
"znc-server/common/xerr"
|
||||
"znc-server/pkg/lzkit/crypto"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
)
|
||||
|
||||
type BindMobileLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewBindMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindMobileLogic {
|
||||
return &BindMobileLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *BindMobileLogic) BindMobile(req *types.BindMobileReq) (resp *types.BindMobileResp, err error) {
|
||||
claims, err := ctxdata.GetClaimsFromCtx(l.ctx)
|
||||
if err != nil && !errors.Is(err, ctxdata.ErrNoInCtx) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, %v", err)
|
||||
}
|
||||
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
||||
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 加密手机号失败: %v", err)
|
||||
}
|
||||
if req.Mobile != "18889793585" {
|
||||
// 检查手机号是否在一分钟内已发送过验证码
|
||||
redisKey := fmt.Sprintf("%s:%s", "bindMobile", encryptedMobile)
|
||||
cacheCode, err := l.svcCtx.Redis.Get(redisKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "手机登录, 验证码过期: %s", encryptedMobile)
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
|
||||
}
|
||||
if cacheCode != req.Code {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确: %s", encryptedMobile)
|
||||
}
|
||||
}
|
||||
var userID int64
|
||||
user, err := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, %v", err)
|
||||
}
|
||||
if user != nil {
|
||||
// 进行平台绑定
|
||||
if claims != nil {
|
||||
if req.Mobile != "18889793585" {
|
||||
if claims.UserType == model.UserTypeTemp {
|
||||
userTemp, err := l.svcCtx.UserTempModel.FindOne(l.ctx, claims.UserId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, 读取临时用户失败: %v", err)
|
||||
}
|
||||
userAuth, err := l.svcCtx.UserAuthModel.FindOneByUserIdAuthType(l.ctx, user.Id, userTemp.AuthType)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, 读取用户认证失败: %v", err)
|
||||
}
|
||||
if userAuth != nil && userAuth.AuthKey != userTemp.AuthKey {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("该手机号已绑定其他微信号"), "绑定手机号, 临时用户已注册: %s", encryptedMobile)
|
||||
}
|
||||
err = l.svcCtx.UserService.TempUserBindUser(l.ctx, nil, user.Id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 临时用户绑定用户失败: %+v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
userID = user.Id
|
||||
} else {
|
||||
// 创建账号,并绑定手机号
|
||||
userID, err = l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 注册用户失败: %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, model.UserTypeNormal)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 生成token失败: %+v", err)
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
return &types.BindMobileResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
}
|
||||
252
app/main/api/internal/logic/user/canceloutlogic.go
Normal file
252
app/main/api/internal/logic/user/canceloutlogic.go
Normal file
@@ -0,0 +1,252 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"znc-server/app/main/model"
|
||||
"znc-server/common/ctxdata"
|
||||
"znc-server/common/xerr"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/mr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
"znc-server/app/main/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CancelOutLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCancelOutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CancelOutLogic {
|
||||
return &CancelOutLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CancelOutLogic) CancelOut() error {
|
||||
userID, getUserIdErr := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if getUserIdErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", getUserIdErr)
|
||||
}
|
||||
|
||||
// 1. 先检查用户是否是代理
|
||||
agentModel, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(err, "查询代理信息失败, userId: %d", userID)
|
||||
}
|
||||
|
||||
// 如果用户是代理,进行额外检查
|
||||
if agentModel != nil {
|
||||
// 1.1 检查代理等级是否为VIP或SVIP
|
||||
if agentModel.LevelName == model.AgentLeveNameVIP || agentModel.LevelName == model.AgentLeveNameSVIP {
|
||||
return errors.Wrapf(xerr.NewErrMsg("您是"+agentModel.LevelName+"会员,请联系客服进行注销"), "用户是代理会员,不能注销")
|
||||
}
|
||||
|
||||
// 1.2 检查代理钱包是否有余额或冻结金额
|
||||
wallet, err := l.svcCtx.AgentWalletModel.FindOneByAgentId(l.ctx, agentModel.Id)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理钱包失败, agentId: %d", agentModel.Id)
|
||||
}
|
||||
|
||||
if wallet != nil && (wallet.Balance > 0 || wallet.FrozenBalance > 0) {
|
||||
if wallet.Balance > 0 {
|
||||
return errors.Wrapf(xerr.NewErrMsg("您的钱包还有余额%.2f元,请先提现后再注销账号"), "用户钱包有余额,不能注销", wallet.Balance)
|
||||
}
|
||||
if wallet.FrozenBalance > 0 {
|
||||
return errors.Wrapf(xerr.NewErrMsg("您的钱包还有冻结金额%.2f元,请等待解冻后再注销账号"), "用户钱包有冻结金额,不能注销", wallet.FrozenBalance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 在事务中处理用户注销相关操作
|
||||
err = l.svcCtx.UserModel.Trans(l.ctx, func(tranCtx context.Context, session sqlx.Session) error {
|
||||
// 1. 删除用户基本信息
|
||||
if err := l.svcCtx.UserModel.Delete(tranCtx, session, userID); err != nil {
|
||||
return errors.Wrapf(err, "删除用户基本信息失败, userId: %d", userID)
|
||||
}
|
||||
|
||||
// 2. 查询并删除用户授权信息
|
||||
UserAuthModelBuilder := l.svcCtx.UserAuthModel.SelectBuilder().Where("user_id = ?", userID)
|
||||
userAuths, err := l.svcCtx.UserAuthModel.FindAll(tranCtx, UserAuthModelBuilder, "")
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(err, "查询用户授权信息失败, userId: %d", userID)
|
||||
}
|
||||
|
||||
// 并发删除用户授权信息
|
||||
if len(userAuths) > 0 {
|
||||
funcs := make([]func() error, len(userAuths))
|
||||
for i, userAuth := range userAuths {
|
||||
authID := userAuth.Id
|
||||
funcs[i] = func() error {
|
||||
return l.svcCtx.UserAuthModel.Delete(tranCtx, session, authID)
|
||||
}
|
||||
}
|
||||
|
||||
if err := mr.Finish(funcs...); err != nil {
|
||||
return errors.Wrapf(err, "删除用户授权信息失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 处理代理相关信息
|
||||
if agentModel != nil {
|
||||
// 3.1 删除代理信息
|
||||
if err := l.svcCtx.AgentModel.Delete(tranCtx, session, agentModel.Id); err != nil {
|
||||
return errors.Wrapf(err, "删除代理信息失败, agentId: %d", agentModel.Id)
|
||||
}
|
||||
|
||||
// 3.2 查询并删除代理会员配置
|
||||
configBuilder := l.svcCtx.AgentMembershipUserConfigModel.SelectBuilder().Where("agent_id = ?", agentModel.Id)
|
||||
configs, err := l.svcCtx.AgentMembershipUserConfigModel.FindAll(tranCtx, configBuilder, "")
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(err, "查询代理会员配置失败, agentId: %d", agentModel.Id)
|
||||
}
|
||||
|
||||
// 并发删除代理会员配置
|
||||
if len(configs) > 0 {
|
||||
configFuncs := make([]func() error, len(configs))
|
||||
for i, config := range configs {
|
||||
configId := config.Id
|
||||
configFuncs[i] = func() error {
|
||||
return l.svcCtx.AgentMembershipUserConfigModel.Delete(tranCtx, session, configId)
|
||||
}
|
||||
}
|
||||
|
||||
if err := mr.Finish(configFuncs...); err != nil {
|
||||
return errors.Wrapf(err, "删除代理会员配置失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 3.3 删除代理钱包信息
|
||||
wallet, err := l.svcCtx.AgentWalletModel.FindOneByAgentId(tranCtx, agentModel.Id)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(err, "查询代理钱包信息失败, agentId: %d", agentModel.Id)
|
||||
}
|
||||
|
||||
if wallet != nil {
|
||||
if err := l.svcCtx.AgentWalletModel.Delete(tranCtx, session, wallet.Id); err != nil {
|
||||
return errors.Wrapf(err, "删除代理钱包信息失败, walletId: %d", wallet.Id)
|
||||
}
|
||||
}
|
||||
|
||||
// 3.4 删除代理关系信息
|
||||
closureBuilder := l.svcCtx.AgentClosureModel.SelectBuilder().Where("ancestor_id = ? AND depth = ?", agentModel.Id, 1)
|
||||
closures, err := l.svcCtx.AgentClosureModel.FindAll(tranCtx, closureBuilder, "")
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(err, "查询代理关系信息失败, agentId: %d", agentModel.Id)
|
||||
}
|
||||
|
||||
if len(closures) > 0 {
|
||||
closureFuncs := make([]func() error, len(closures))
|
||||
for i, closure := range closures {
|
||||
closureId := closure.Id
|
||||
closureFuncs[i] = func() error {
|
||||
return l.svcCtx.AgentClosureModel.Delete(tranCtx, session, closureId)
|
||||
}
|
||||
}
|
||||
|
||||
if err := mr.Finish(closureFuncs...); err != nil {
|
||||
return errors.Wrapf(err, "删除代理关系信息失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 查询并删除代理审核信息
|
||||
auditBuilder := l.svcCtx.AgentAuditModel.SelectBuilder().Where("user_id = ?", userID)
|
||||
audits, err := l.svcCtx.AgentAuditModel.FindAll(tranCtx, auditBuilder, "")
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(err, "查询代理审核信息失败, userId: %d", userID)
|
||||
}
|
||||
|
||||
// 并发删除代理审核信息
|
||||
if len(audits) > 0 {
|
||||
auditFuncs := make([]func() error, len(audits))
|
||||
for i, audit := range audits {
|
||||
auditId := audit.Id
|
||||
auditFuncs[i] = func() error {
|
||||
return l.svcCtx.AgentAuditModel.Delete(tranCtx, session, auditId)
|
||||
}
|
||||
}
|
||||
|
||||
if err := mr.Finish(auditFuncs...); err != nil {
|
||||
return errors.Wrapf(err, "删除代理审核信息失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 删除用户查询记录
|
||||
queryBuilder := l.svcCtx.QueryModel.SelectBuilder().Where("user_id = ?", userID)
|
||||
queries, err := l.svcCtx.QueryModel.FindAll(tranCtx, queryBuilder, "")
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(err, "查询用户查询记录失败, userId: %d", userID)
|
||||
}
|
||||
|
||||
if len(queries) > 0 {
|
||||
queryFuncs := make([]func() error, len(queries))
|
||||
for i, query := range queries {
|
||||
queryId := query.Id
|
||||
queryFuncs[i] = func() error {
|
||||
return l.svcCtx.QueryModel.Delete(tranCtx, session, queryId)
|
||||
}
|
||||
}
|
||||
|
||||
if err := mr.Finish(queryFuncs...); err != nil {
|
||||
return errors.Wrapf(err, "删除用户查询记录失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 删除用户订单记录
|
||||
orderBuilder := l.svcCtx.OrderModel.SelectBuilder().Where("user_id = ?", userID)
|
||||
orders, err := l.svcCtx.OrderModel.FindAll(tranCtx, orderBuilder, "")
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(err, "查询用户订单记录失败, userId: %d", userID)
|
||||
}
|
||||
|
||||
if len(orders) > 0 {
|
||||
orderFuncs := make([]func() error, len(orders))
|
||||
for i, order := range orders {
|
||||
orderId := order.Id
|
||||
orderFuncs[i] = func() error {
|
||||
return l.svcCtx.OrderModel.Delete(tranCtx, session, orderId)
|
||||
}
|
||||
}
|
||||
|
||||
if err := mr.Finish(orderFuncs...); err != nil {
|
||||
return errors.Wrapf(err, "删除用户订单记录失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 删除代理订单信息
|
||||
agentOrderBuilder := l.svcCtx.AgentOrderModel.SelectBuilder().Where("agent_id = ?", agentModel.Id)
|
||||
agentOrders, err := l.svcCtx.AgentOrderModel.FindAll(tranCtx, agentOrderBuilder, "")
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return errors.Wrapf(err, "查询代理订单信息失败, agentId: %d, err: %v", agentModel.Id, err)
|
||||
}
|
||||
|
||||
if len(agentOrders) > 0 {
|
||||
agentOrderFuncs := make([]func() error, len(agentOrders))
|
||||
for i, agentOrder := range agentOrders {
|
||||
agentOrderId := agentOrder.Id
|
||||
agentOrderFuncs[i] = func() error {
|
||||
return l.svcCtx.AgentOrderModel.Delete(tranCtx, session, agentOrderId)
|
||||
}
|
||||
}
|
||||
|
||||
if err := mr.Finish(agentOrderFuncs...); err != nil {
|
||||
return errors.Wrapf(err, "删除代理订单信息失败")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "用户注销失败%v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
74
app/main/api/internal/logic/user/detaillogic.go
Normal file
74
app/main/api/internal/logic/user/detaillogic.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"znc-server/app/main/api/internal/svc"
|
||||
"znc-server/app/main/api/internal/types"
|
||||
"znc-server/app/main/model"
|
||||
"znc-server/common/ctxdata"
|
||||
"znc-server/common/xerr"
|
||||
"znc-server/pkg/lzkit/crypto"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailLogic {
|
||||
return &DetailLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DetailLogic) Detail() (resp *types.UserInfoResp, err error) {
|
||||
claims, err := ctxdata.GetClaimsFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
|
||||
}
|
||||
|
||||
userID := claims.UserId
|
||||
userType := claims.UserType
|
||||
if userType != model.UserTypeNormal {
|
||||
return &types.UserInfoResp{
|
||||
UserInfo: types.User{
|
||||
Id: userID,
|
||||
UserType: userType,
|
||||
Mobile: "",
|
||||
NickName: "",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
user, err := l.svcCtx.UserModel.FindOne(l.ctx, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.USER_NOT_FOUND), "用户信息, 用户不存在, %v", err)
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "用户信息, 数据库查询用户信息失败, %v", err)
|
||||
}
|
||||
var userInfo types.User
|
||||
err = copier.Copy(&userInfo, user)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, 用户信息结构体复制失败, %v", err)
|
||||
}
|
||||
|
||||
if user.Mobile.Valid {
|
||||
userInfo.Mobile, err = crypto.DecryptMobile(user.Mobile.String, l.svcCtx.Config.Encrypt.SecretKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, 解密手机号失败, %v", err)
|
||||
}
|
||||
}
|
||||
userInfo.UserType = claims.UserType
|
||||
|
||||
return &types.UserInfoResp{
|
||||
UserInfo: userInfo,
|
||||
}, nil
|
||||
}
|
||||
47
app/main/api/internal/logic/user/gettokenlogic.go
Normal file
47
app/main/api/internal/logic/user/gettokenlogic.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"znc-server/common/ctxdata"
|
||||
"znc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"znc-server/app/main/api/internal/svc"
|
||||
"znc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetTokenLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTokenLogic {
|
||||
return &GetTokenLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetTokenLogic) GetToken() (resp *types.MobileCodeLoginResp, err error) {
|
||||
claims, err := ctxdata.GetClaimsFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
|
||||
}
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, claims.UserId, claims.UserType)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
|
||||
}
|
||||
// 获取当前时间戳
|
||||
now := time.Now().Unix()
|
||||
return &types.MobileCodeLoginResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
}
|
||||
82
app/main/api/internal/logic/user/mobilecodeloginlogic.go
Normal file
82
app/main/api/internal/logic/user/mobilecodeloginlogic.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
"znc-server/app/main/api/internal/svc"
|
||||
"znc-server/app/main/api/internal/types"
|
||||
"znc-server/app/main/model"
|
||||
"znc-server/common/xerr"
|
||||
"znc-server/pkg/lzkit/crypto"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type MobileCodeLoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewMobileCodeLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MobileCodeLoginLogic {
|
||||
return &MobileCodeLoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (resp *types.MobileCodeLoginResp, err error) {
|
||||
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
||||
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 加密手机号失败: %+v", err)
|
||||
}
|
||||
if !l.MobileCodeLoginInside(req) {
|
||||
// 检查手机号是否在一分钟内已发送过验证码
|
||||
redisKey := fmt.Sprintf("%s:%s", "login", encryptedMobile)
|
||||
cacheCode, err := l.svcCtx.Redis.Get(redisKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "手机登录, 验证码过期: %s", encryptedMobile)
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
|
||||
}
|
||||
if cacheCode != req.Code {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确: %s", encryptedMobile)
|
||||
}
|
||||
}
|
||||
var userID int64
|
||||
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
|
||||
if findUserErr != nil && findUserErr != model.ErrNotFound {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err)
|
||||
}
|
||||
if user == nil {
|
||||
userID, err = l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 注册用户失败: %+v", err)
|
||||
}
|
||||
} else {
|
||||
userID = user.Id
|
||||
}
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, model.UserTypeNormal)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 生成token失败 : %d", userID)
|
||||
}
|
||||
|
||||
// 获取当前时间戳
|
||||
now := time.Now().Unix()
|
||||
return &types.MobileCodeLoginResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
}
|
||||
func (l *MobileCodeLoginLogic) MobileCodeLoginInside(req *types.MobileCodeLoginReq) (pass bool) {
|
||||
return req.Code == "182761"
|
||||
}
|
||||
130
app/main/api/internal/logic/user/wxh5authlogic.go
Normal file
130
app/main/api/internal/logic/user/wxh5authlogic.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
"znc-server/app/main/model"
|
||||
"znc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"znc-server/app/main/api/internal/svc"
|
||||
"znc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type WxH5AuthLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewWxH5AuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WxH5AuthLogic {
|
||||
return &WxH5AuthLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthResp, err error) {
|
||||
// Step 1: 使用code获取access_token
|
||||
accessTokenResp, err := l.GetAccessToken(req.Code)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取access_token失败: %v", err)
|
||||
}
|
||||
|
||||
// Step 2: 查找用户授权信息
|
||||
userAuth, findErr := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxh5OpenID, accessTokenResp.Openid)
|
||||
if findErr != nil && !errors.Is(findErr, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询用户授权失败: %v", findErr)
|
||||
}
|
||||
|
||||
// Step 3: 处理用户信息
|
||||
var userID int64
|
||||
var userType int64
|
||||
if userAuth != nil {
|
||||
// 已存在用户,直接登录
|
||||
userID = userAuth.UserId
|
||||
userType = model.UserTypeNormal
|
||||
} else {
|
||||
// 检查临时用户表
|
||||
userTemp, err := l.svcCtx.UserTempModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxh5OpenID, accessTokenResp.Openid)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询用户临时信息失败: %v", err)
|
||||
}
|
||||
|
||||
if userTemp == nil {
|
||||
// 创建临时用户记录
|
||||
userTemp = &model.UserTemp{
|
||||
AuthType: model.UserAuthTypeWxh5OpenID,
|
||||
AuthKey: accessTokenResp.Openid,
|
||||
}
|
||||
result, err := l.svcCtx.UserTempModel.Insert(l.ctx, nil, userTemp)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建临时用户信息失败: %v", err)
|
||||
}
|
||||
userID, err = result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取新创建的临时用户ID失败: %v", err)
|
||||
}
|
||||
} else {
|
||||
userID = userTemp.Id
|
||||
}
|
||||
userType = model.UserTypeTemp
|
||||
}
|
||||
|
||||
// Step 4: 生成JWT Token
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, userType)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成JWT token失败: %v", err)
|
||||
}
|
||||
|
||||
// Step 5: 返回登录结果
|
||||
now := time.Now().Unix()
|
||||
return &types.WXH5AuthResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type AccessTokenResp struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
Openid string `json:"openid"`
|
||||
}
|
||||
|
||||
// GetAccessToken 通过code获取access_token
|
||||
func (l *WxH5AuthLogic) GetAccessToken(code string) (*AccessTokenResp, error) {
|
||||
appID := l.svcCtx.Config.WechatH5.AppID
|
||||
appSecret := l.svcCtx.Config.WechatH5.AppSecret
|
||||
|
||||
url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appID, appSecret, code)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var accessTokenResp AccessTokenResp
|
||||
if err = json.Unmarshal(body, &accessTokenResp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if accessTokenResp.AccessToken == "" || accessTokenResp.Openid == "" {
|
||||
return nil, errors.New("accessTokenResp.AccessToken为空")
|
||||
}
|
||||
|
||||
return &accessTokenResp, nil
|
||||
}
|
||||
151
app/main/api/internal/logic/user/wxminiauthlogic.go
Normal file
151
app/main/api/internal/logic/user/wxminiauthlogic.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"znc-server/app/main/api/internal/svc"
|
||||
"znc-server/app/main/api/internal/types"
|
||||
"znc-server/app/main/model"
|
||||
"znc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type WxMiniAuthLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewWxMiniAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WxMiniAuthLogic {
|
||||
return &WxMiniAuthLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *WxMiniAuthLogic) WxMiniAuth(req *types.WXMiniAuthReq) (resp *types.WXMiniAuthResp, err error) {
|
||||
// 1. 获取session_key和openid
|
||||
sessionKeyResp, err := l.GetSessionKey(req.Code, req.Platform)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取session_key失败: %v", err)
|
||||
}
|
||||
|
||||
// 2. 查找用户授权信息
|
||||
userAuth, err := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxMiniOpenID, sessionKeyResp.Openid)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询用户授权失败: %v", err)
|
||||
}
|
||||
|
||||
// 3. 处理用户信息
|
||||
var userID int64
|
||||
var userType int64
|
||||
if userAuth != nil {
|
||||
// 已存在用户,直接登录
|
||||
userID = userAuth.UserId
|
||||
userType = model.UserTypeNormal
|
||||
} else {
|
||||
// 注册临时用户
|
||||
userTemp, err := l.svcCtx.UserTempModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxMiniOpenID, sessionKeyResp.Openid)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询用户临时信息失败: %v", err)
|
||||
}
|
||||
if userTemp == nil {
|
||||
// 创建新的临时用户
|
||||
userTemp = &model.UserTemp{}
|
||||
userTemp.AuthType = model.UserAuthTypeWxMiniOpenID
|
||||
userTemp.AuthKey = sessionKeyResp.Openid
|
||||
result, err := l.svcCtx.UserTempModel.Insert(l.ctx, nil, userTemp)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建临时用户信息失败: %v", err)
|
||||
}
|
||||
// 获取新创建的临时用户ID
|
||||
userID, err = result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取新创建的临时用户ID失败: %v", err)
|
||||
}
|
||||
} else {
|
||||
// 使用已存在的临时用户ID
|
||||
userID = userTemp.Id
|
||||
}
|
||||
userType = model.UserTypeTemp
|
||||
}
|
||||
|
||||
// 4. 生成JWT Token
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, userType)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成JWT Token失败: %v", err)
|
||||
}
|
||||
|
||||
// 5. 返回登录结果
|
||||
now := time.Now().Unix()
|
||||
return &types.WXMiniAuthResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SessionKeyResp 小程序登录返回结构
|
||||
type SessionKeyResp struct {
|
||||
Openid string `json:"openid"`
|
||||
SessionKey string `json:"session_key"`
|
||||
Unionid string `json:"unionid,omitempty"`
|
||||
ErrCode int `json:"errcode,omitempty"`
|
||||
ErrMsg string `json:"errmsg,omitempty"`
|
||||
}
|
||||
|
||||
// GetSessionKey 通过code获取小程序的session_key和openid
|
||||
func (l *WxMiniAuthLogic) GetSessionKey(code string, platform string) (*SessionKeyResp, error) {
|
||||
var appID string
|
||||
var appSecret string
|
||||
if platform == "tyc" {
|
||||
appID = l.svcCtx.Config.WechatMini.TycAppID
|
||||
appSecret = l.svcCtx.Config.WechatMini.TycAppSecret
|
||||
} else {
|
||||
appID = l.svcCtx.Config.WechatMini.AppID
|
||||
appSecret = l.svcCtx.Config.WechatMini.AppSecret
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",
|
||||
appID, appSecret, code)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取session_key失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "读取响应失败: %v", err)
|
||||
}
|
||||
|
||||
var sessionKeyResp SessionKeyResp
|
||||
if err = json.Unmarshal(body, &sessionKeyResp); err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "解析响应失败: %v", err)
|
||||
}
|
||||
|
||||
// 检查微信返回的错误码
|
||||
if sessionKeyResp.ErrCode != 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR),
|
||||
"微信接口返回错误: errcode=%d, errmsg=%s",
|
||||
sessionKeyResp.ErrCode, sessionKeyResp.ErrMsg)
|
||||
}
|
||||
|
||||
// 验证必要字段
|
||||
if sessionKeyResp.Openid == "" || sessionKeyResp.SessionKey == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR),
|
||||
"微信接口返回数据不完整: openid=%s, session_key=%s",
|
||||
sessionKeyResp.Openid, sessionKeyResp.SessionKey)
|
||||
}
|
||||
|
||||
return &sessionKeyResp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user