Files
ai_site/platform/internal/license/redeem.go
2026-07-31 10:31:17 +08:00

54 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package license
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)
// redeemOnline 向你们的核销服务登记/校验延期包 id删光本地数据后仍能拦截旧包
func (m *Manager) redeemOnline(leaseID, action string) error {
base := strings.TrimRight(strings.TrimSpace(m.cfg.RedeemURL), "/")
if base == "" {
return nil
}
leaseID = strings.TrimSpace(leaseID)
if leaseID == "" {
return fmt.Errorf("lease id 为空")
}
body, _ := json.Marshal(map[string]string{
"id": leaseID,
"customer": strings.TrimSpace(m.cfg.Customer),
"action": action, // check | redeem
"secret": strings.TrimSpace(m.cfg.ControlSecret),
})
client := &http.Client{Timeout: 15 * time.Second}
req, err := http.NewRequest(http.MethodPost, base+"/v1/license/redeem", bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-License-Secret", strings.TrimSpace(m.cfg.ControlSecret))
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("核销服务不可达(已配置 RedeemURL导入需联网: %w", err)
}
defer resp.Body.Close()
var out struct {
OK bool `json:"ok"`
Error string `json:"error"`
}
_ = json.NewDecoder(resp.Body).Decode(&out)
if resp.StatusCode >= 300 || !out.OK {
msg := out.Error
if msg == "" {
msg = fmt.Sprintf("核销失败 HTTP %d", resp.StatusCode)
}
return fmt.Errorf("%s", msg)
}
return nil
}