package main

import (
	"sparrow/pkg/models"
	"time"

	jwt "github.com/dgrijalva/jwt-go"
)

const SignedString = "www.yehaoji.com"

type AppClaims struct {
	AppID     uint
	AppName   string
	AppKey    string
	SecretKey string
	VendorID  string
	jwt.StandardClaims
}

// TokenMaker 生成token
func TokenMaker(app *models.Application) (string, int64) {
	claims := AppClaims{
		AppID:     app.ID,
		AppName:   app.AppName,
		AppKey:    app.AppKey,
		SecretKey: app.SecretKey,
		VendorID:  app.VendorID,
	}
	expireAt := time.Now().Add(time.Hour * 24).Unix()
	claims.ExpiresAt = expireAt
	claims.IssuedAt = time.Now().Unix()
	claims.Issuer = "apiprovider"
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	ser, _ := token.SignedString([]byte(SignedString))
	return ser, expireAt
}
