package asUser import ( "context" "errors" "fmt" "reflect" "github.com/fonchain_enterprise/fonchain-main/api/account" "github.com/fonchain_enterprise/fonchain-main/api/artistInfoUser" "github.com/fonchain_enterprise/fonchain-main/api/artistinfoArtshow" "github.com/fonchain_enterprise/fonchain-main/pkg/e" "github.com/fonchain_enterprise/fonchain-main/pkg/middleware" "github.com/fonchain_enterprise/fonchain-main/pkg/service" "github.com/fonchain_enterprise/fonchain-main/pkg/utils/secret" "github.com/gin-gonic/gin" ) func ParseTokenFromHttpHeader(c *gin.Context) (accountInfo *account.DecryptJwtResponse, err error, code int) { accountInfo = &account.DecryptJwtResponse{} token := GetTokenFromHttpHeader(c) if token == "" { err = errors.New("登录状态失效") code = e.NotLogin } else { accountInfo, err = service.AccountProvider.DecryptJwt(context.Background(), &account.DecryptJwtRequest{Token: GetTokenFromHttpHeader(c)}) if err != nil || accountInfo.IsOffline { err = errors.New("登录状态失效") code = e.NotLogin } } return } func GetTokenFromHttpHeader(c *gin.Context) string { return c.GetHeader(middleware.Authorization) } func GetUserInfoWithToken(c *gin.Context) (userInfo *artistInfoUser.UserInfo, err error, code int) { userInfo = &artistInfoUser.UserInfo{} accountInfo, err := service.AccountProvider.DecryptJwt(context.Background(), &account.DecryptJwtRequest{Token: GetTokenFromHttpHeader(c)}) if err != nil || accountInfo.IsOffline { err = errors.New("登录状态失效") code = e.NotLogin return } userInfo, err = service.GrpcArtistInfoUserImpl.FindUser(c, &artistInfoUser.FindUserRequest{ MgmtAccId: int64(accountInfo.ID), }) return } // dataKey 为数据id或画作uid func UpdateOperationTime[T int64 | string](c *gin.Context, odType artistinfoArtshow.OdType, dataKey T) { jwt, _, _ := ParseTokenFromHttpHeader(c) var req = &artistinfoArtshow.UpdateOperationTimeRequest{ OpObject: artistinfoArtshow.OpObject_backend, OperationAccId: int64(jwt.ID), OdType: odType, } if reflect.TypeOf(dataKey).Kind() == reflect.String { req.ArtworkUid = reflect.ValueOf(dataKey).String() } else { req.DataId = reflect.ValueOf(dataKey).Int() } _, err := service.GrpcArtistInfoArtshowImpl.UpdateOperationTime(c, req) if err != nil { fmt.Println("更新审批操作时间失败!!", err.Error()) } } func GetAccInfoWithToken(ctx *gin.Context) (accInfo *account.AccountInfo, err error, code int) { authorization := ctx.GetHeader(e.Authorization) if authorization == "" { err = errors.New("登录状态失效") code = e.NotLogin return } jwt, err := secret.GetJwtFromStr(authorization) if err != nil { err = errors.New("登录状态失效") code = e.NotLogin return } //0 解密 info, err := service.AccountProvider.DecryptJwt(ctx, &account.DecryptJwtRequest{ Token: jwt, }) if err != nil { err = errors.New("登录状态失效") code = e.NotLogin return } //1 获取用户的账号信息 infoRes, err := service.AccountProvider.Info(ctx, &account.InfoRequest{ ID: info.ID, }) if err != nil { err = errors.New("登录状态失效") code = e.NotLogin return } accInfo = infoRes.Info return }