添加APP_ID生成

This commit is contained in:
junleea 2025-08-01 20:41:22 +08:00
parent a9a9ecc3bd
commit 68e0ee27f2
2 changed files with 17 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"gorm.io/gorm" "gorm.io/gorm"
"log" "log"
mrand "math/rand"
"os" "os"
"sync" "sync"
) )
@ -92,6 +93,7 @@ type ConfigStruct struct {
USER_SYNC_TIME int `json:"user_sync_time"` // 用户数据同步时间,单位秒 USER_SYNC_TIME int `json:"user_sync_time"` // 用户数据同步时间,单位秒
SERVER_NAME string `json:"server_name"` // 服务器名称,用于区分不同服务器 SERVER_NAME string `json:"server_name"` // 服务器名称,用于区分不同服务器
MONITOR_SERVER_TOKEN string `json:"monitor_server_token"` // 监控服务器token,用于状态监控及邮件通知 MONITOR_SERVER_TOKEN string `json:"monitor_server_token"` // 监控服务器token,用于状态监控及邮件通知
APP_ID string `json:"app_id"` // 应用ID,用于标识不同应用
} }
func WriteConfigToFile() { func WriteConfigToFile() {
@ -158,8 +160,22 @@ func ReadConfig(path string) error {
} }
} }
SigningKey = []byte(Config.TOKEN_SECRET) SigningKey = []byte(Config.TOKEN_SECRET)
if Config.APP_ID == "" {
Config.APP_ID = GetRandomString(8)
log.Println("ReadConfig generated APP_ID:", Config.APP_ID)
go WriteConfigToFile()
}
return err return err
} }
func GetRandomString(l int) string {
str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
var result []byte
for i := 0; i < l; i++ {
result = append(result, bytes[mrand.Intn(len(bytes))])
}
return string(result)
}
// 默认配置 // 默认配置
func DefaultConfig() { func DefaultConfig() {

View File

@ -280,7 +280,7 @@ func GetTokenSecretFromUserCenter() (*proto.SecretSyncSettings, error) {
proto.SigningKeyRWLock.Lock() proto.SigningKeyRWLock.Lock()
defer proto.SigningKeyRWLock.Unlock() defer proto.SigningKeyRWLock.Unlock()
req.SecretKeyMd5 = worker.GenerateMD5(string(proto.SigningKey)) req.SecretKeyMd5 = worker.GenerateMD5(string(proto.SigningKey))
req.DeviceApp = "StuAcaWorksAI" req.DeviceApp = proto.Config.APP_ID
req.Timestamp = worker.GetCurrentTimestamp() req.Timestamp = worker.GetCurrentTimestamp()
req.Sign = worker.GenerateMD5(req.SecretKeyMd5 + req.DeviceApp + strconv.FormatInt(req.Timestamp, 10)) req.Sign = worker.GenerateMD5(req.SecretKeyMd5 + req.DeviceApp + strconv.FormatInt(req.Timestamp, 10))
reqBytes, err2 := json.Marshal(req) reqBytes, err2 := json.Marshal(req)