mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-10 03:03:43 +08:00
add redeem code function
This commit is contained in:
@@ -1,16 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"geekai/utils"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func main() {
|
||||
file := "http://nk.img.r9it.com/chatgpt-plus/1719389335351828.xlsx"
|
||||
content, err := utils.ReadFileContent(file, "http://172.22.11.69:9998")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
const (
|
||||
codeLength = 32 // 兑换码长度
|
||||
)
|
||||
|
||||
fmt.Println(content)
|
||||
var (
|
||||
codeMap = make(map[string]bool)
|
||||
mapMutex = &sync.Mutex{}
|
||||
)
|
||||
|
||||
// GenerateUniqueCode 生成唯一兑换码
|
||||
func GenerateUniqueCode() (string, error) {
|
||||
for {
|
||||
code, err := generateCode()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
mapMutex.Lock()
|
||||
if !codeMap[code] {
|
||||
codeMap[code] = true
|
||||
mapMutex.Unlock()
|
||||
return code, nil
|
||||
}
|
||||
mapMutex.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// generateCode 生成兑换码
|
||||
func generateCode() (string, error) {
|
||||
bytes := make([]byte, codeLength/2) // 因为 hex 编码会使长度翻倍
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
for i := 0; i < 10; i++ {
|
||||
code, err := GenerateUniqueCode()
|
||||
if err != nil {
|
||||
fmt.Println("Error generating code:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Generated code:", code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user