123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package chat
- import (
- "context"
- "time"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- myjwt "github.com/golang-jwt/jwt/v4"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type GetAuthLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewGetAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAuthLogic {
- return &GetAuthLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *GetAuthLogic) GetAuth() (resp *types.BaseMsgResp, err error) {
- // todo: add your logic here and delete this line
- now := time.Now().Unix()
- accessExpire := l.svcCtx.Config.Auth.AccessExpire
- accessSecret := l.svcCtx.Config.Auth.AccessSecret
- Id := now
- accessToken, err := getToken(accessSecret, now, accessExpire, Id)
- if err != nil {
- accessToken = ""
- }
- return &types.BaseMsgResp{
- Code: 100,
- Msg: accessToken,
- }, err
- }
- func getToken(secretKey string, iat, seconds, uid int64) (string, error) {
- claims := make(myjwt.MapClaims)
- claims["exp"] = iat + seconds
- claims["iat"] = iat
- claims["uid"] = uid
- token := myjwt.New(myjwt.SigningMethodHS256)
- token.Claims = claims
- return token.SignedString([]byte(secretKey))
- }
|