package utils import ( "crypto/rand" "encoding/hex" "fmt" "strings" "time" ) // GenerateLicenseKey 生成 License 密钥(格式: LIC-YYYY-XXXX-XXXX-XXXX-XXXX) // Logan.Li func GenerateLicenseKey() string { bytes := make([]byte, 16) _, _ = rand.Read(bytes) year := time.Now().Year() part1 := hex.EncodeToString(bytes[0:4]) part2 := hex.EncodeToString(bytes[4:8]) part3 := hex.EncodeToString(bytes[8:12]) part4 := hex.EncodeToString(bytes[12:16]) return fmt.Sprintf("LIC-%d-%s-%s-%s-%s", year, strings.ToUpper(part1[:4]), strings.ToUpper(part2[:4]), strings.ToUpper(part3[:4]), strings.ToUpper(part4[:4])) } // GenerateOrderNo 生成唯一订单号(格式: ORD-YYYYMMDD-XXXXXXXX) // Logan.Li func GenerateOrderNo() string { bytes := make([]byte, 4) _, _ = rand.Read(bytes) return fmt.Sprintf("ORD-%s-%s", time.Now().Format("20060102"), strings.ToUpper(hex.EncodeToString(bytes))) } // CalculateDaysLeft 计算剩余天数(向下取整,最小为 0) // Logan.Li func CalculateDaysLeft(endDate time.Time) int { diff := endDate.Sub(time.Now()) days := int(diff.Hours() / 24) if days < 0 { return 0 } return days } // IsLicenseExpired 检查 License 是否过期 // Logan.Li func IsLicenseExpired(endDate time.Time) bool { return time.Now().After(endDate) } // IsExpiringSoon 检查 License 是否即将到期 // Logan.Li func IsExpiringSoon(endDate time.Time, warningDays int) bool { daysLeft := CalculateDaysLeft(endDate) return daysLeft > 0 && daysLeft <= warningDays } // ParseFeatures 解析产品功能列表 JSON 为字符串切片 // Logan.Li func ParseFeatures(featuresJSON string) []string { if featuresJSON == "" { return []string{} } trimmed := strings.Trim(featuresJSON, "[]") if trimmed == "" { return []string{} } // 简单解析:去引号后按逗号分割 // Logan.Li trimmed = strings.ReplaceAll(trimmed, `"`, "") parts := strings.Split(trimmed, ",") result := make([]string, 0, len(parts)) for _, p := range parts { if s := strings.TrimSpace(p); s != "" { result = append(result, s) } } return result } // FormatFeatures 将功能列表格式化为 JSON 字符串 // Logan.Li func FormatFeatures(features []string) string { if len(features) == 0 { return "[]" } quoted := make([]string, len(features)) for i, f := range features { quoted[i] = fmt.Sprintf(`"%s"`, f) } return fmt.Sprintf("[%s]", strings.Join(quoted, ",")) }