fonchain-fiee/pkg/middleware/check_login.go

181 lines
4.5 KiB
Go
Raw Normal View History

2025-02-20 11:43:50 +00:00
package middleware
import (
2025-05-26 08:31:38 +00:00
"dubbo.apache.org/dubbo-go/v3/common/logger"
2025-02-20 11:43:50 +00:00
"errors"
"fmt"
2025-05-26 08:31:38 +00:00
"fonchain-fiee/api/account"
api "fonchain-fiee/api/accountFiee"
2025-02-20 11:43:50 +00:00
jwt2 "fonchain-fiee/pkg/common/jwt"
"fonchain-fiee/pkg/common/m"
"fonchain-fiee/pkg/e"
2025-05-26 08:31:38 +00:00
"fonchain-fiee/pkg/logic"
"fonchain-fiee/pkg/model"
2025-02-20 11:43:50 +00:00
"fonchain-fiee/pkg/model/login"
"fonchain-fiee/pkg/service"
2025-05-26 08:31:38 +00:00
"fonchain-fiee/pkg/utils/secret"
2025-02-20 11:43:50 +00:00
"github.com/gin-gonic/gin"
2025-05-26 08:31:38 +00:00
"time"
2025-02-20 11:43:50 +00:00
)
// CheckLogin 检测登陆
2025-05-26 08:31:38 +00:00
func CheckLogin(provider *api.AccountFieeClientImpl) gin.HandlerFunc {
2025-02-20 11:43:50 +00:00
return func(ctx *gin.Context) {
//如果没有登录
authorization := ctx.GetHeader(e.BoxAuthorization)
if authorization == "" {
2025-02-22 09:32:25 +00:00
service.NotLoginError(ctx, errors.New(e.ErrNotLogin))
2025-02-20 11:43:50 +00:00
return
}
jwt, err := jwt2.ParseToken(authorization, m.JWTSecret)
fmt.Println(jwt)
if err != nil {
2025-02-22 09:32:25 +00:00
service.NotLoginError(ctx, errors.New(e.ErrNotLogin))
2025-02-20 11:43:50 +00:00
return
}
//1 获取用户的账号信息
infoReq := &api.UserByTelRequest{
2025-02-21 05:27:45 +00:00
Tel: jwt.Phone,
Domain: jwt.Domain,
2025-02-20 11:43:50 +00:00
}
2025-05-26 08:31:38 +00:00
infoRes, err := service.AccountFieeProvider.UserByTel(ctx, infoReq)
2025-02-20 11:43:50 +00:00
if err != nil {
service.Error(ctx, err)
return
}
2025-02-21 09:49:13 +00:00
//info := infoRes.Info/
2025-02-20 11:43:50 +00:00
loginInfo := login.Info{
2025-02-21 09:49:13 +00:00
ID: infoRes.Id,
Status: infoRes.Status,
Name: infoRes.Name,
Sex: infoRes.Sex,
Nationality: infoRes.Nationality,
DocumentType: infoRes.DocumentType,
CertificatePicture: infoRes.CertificatePicture,
Validity: infoRes.Validity,
PlaceOfResidence: infoRes.PlaceOfResidence,
GroupPhoto: infoRes.GroupPhoto,
Attachment: infoRes.Attachment,
SubNum: infoRes.SubNum,
NotPassRemarks: infoRes.NotPassRemarks,
Domain: infoRes.Domain,
2025-03-12 08:03:24 +00:00
TelNum: jwt.Phone,
SubscriberNumber: infoRes.SubscriberNumber,
2025-02-20 11:43:50 +00:00
}
ctx.Set("jwtInfo", loginInfo)
ctx.Next()
}
}
2025-05-26 08:31:38 +00:00
func CheckWebLogin(provider *account.AccountClientImpl) gin.HandlerFunc {
return func(ctx *gin.Context) {
startTime := time.Now()
//如果没有登录
authorization := ctx.GetHeader(e.Authorization)
if authorization == "" {
service.NotLoginRes(ctx, logic.ConvertLoginMsg(ctx, e.ErrNotLogin))
return
}
jwt, err := secret.GetJwtFromStr(authorization)
logger.Info("---------end帐号转jwt:时间:", time.Now().Sub(startTime))
if err != nil {
service.NotLoginRes(ctx, err.Error())
return
}
//0 解密
req := account.DecryptJwtRequest{
Token: jwt,
}
info, err := provider.DecryptJwt(ctx, &req)
logger.Info("---------end帐号微服务解密:时间:", time.Now().Sub(startTime))
if err != nil {
service.NotLoginRes(ctx, err.Error())
return
}
//if info.OfflineCode == e.OfflineSqueeze {
// service.Error(ctx, e.NotLoginSqueeze, errors.New(e.ErrOfflineSqueeze))
// return
//}
if info.IsOffline == true {
service.ErrorWeb(ctx, e.NotLogin, errors.New(logic.ConvertOfflineMsg(ctx, e.ErrOffline)))
return
}
//1 获取用户的账号信息
infoReq := &account.InfoRequest{
ID: info.ID,
Scene: "base",
}
infoRes, err := service.AccountProvider.Info(ctx, infoReq)
logger.Info("---------end帐号info时间:", time.Now().Sub(startTime))
if err != nil {
service.ErrorWeb(ctx, e.Error, err)
return
}
//3 获取用户的岗位信息
//uReq := rule.RulesRequest{
// AccountID: info.ID,
//}
//
//qres, err1 := service.RuleProvider.UserInfo(ctx, &uReq)
//
//logger.Info("---------end帐号,rule的userInfo:时间:", time.Now().Sub(startTime))
//if err1 != nil {
// service.Error(ctx, e.Error, err)
// return
//}
loginInfo := login.Info{
Domain: info.Domain,
ID: info.ID,
//Account: info.Account,
//NickName: info.NickName,
//PositionUsers: qres.PositionUsers,
//Extend: infoRes.Info.Extend,
TelNum: infoRes.Info.TelNum,
//Avatar: infoRes.Info.Avatar,
//JumpTo: "",
//DepartmentName: "",
}
//if infoRes.Info.Extend != nil {
// loginInfo.JumpTo = infoRes.Info.Extend.JumpTo
//}
//
//if len(qres.PositionUsers) >= 1 {
// loginInfo.DepartmentName = qres.PositionUsers[0].DepartmentName
//}
ctx.Set("jwtInfo", loginInfo)
var mLoginInfo model.LoginInfo
mLoginInfo.ID = loginInfo.ID
mLoginInfo.NickName = loginInfo.Name
mLoginInfo.Phone = loginInfo.TelNum
//mLoginInfo.PositionUsers = qres.PositionUsers
ctx.Set("mLoginInfo", mLoginInfo)
logger.Info("---------check_login:总时间", time.Now().Sub(startTime))
ctx.Next()
}
}