add redeem code function

This commit is contained in:
RockYang
2024-08-08 18:28:50 +08:00
parent e54e908fbc
commit 4b717109d2
22 changed files with 635 additions and 597 deletions

View File

@@ -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)
}
}