chore: opt build bin size
This commit is contained in:
@@ -30,6 +30,7 @@ func initSentences() {
|
||||
var offset int64 = 0
|
||||
for scanner.Scan() {
|
||||
lineOffsets = append(lineOffsets, offset)
|
||||
// scanner.Bytes() returns the line without newline
|
||||
offset += int64(len(scanner.Bytes())) + 1 // +1 for newline
|
||||
lineCount++
|
||||
}
|
||||
@@ -43,22 +44,33 @@ func GetRandomSentence() string {
|
||||
return "欢迎使用白虎面板"
|
||||
}
|
||||
|
||||
targetLine := rand.Intn(lineCount)
|
||||
targetIndex := rand.Intn(lineCount)
|
||||
start := lineOffsets[targetIndex]
|
||||
|
||||
scanner := bufio.NewScanner(bytes.NewReader(sentenceData))
|
||||
currentLine := 0
|
||||
for scanner.Scan() {
|
||||
if currentLine == targetLine {
|
||||
var s Sentence
|
||||
if err := json.Unmarshal(scanner.Bytes(), &s); err == nil && s.Name != "" {
|
||||
if s.From != "" {
|
||||
return "\"" + s.Name + "\"—— " + s.From
|
||||
}
|
||||
return s.Name
|
||||
}
|
||||
break
|
||||
// 找到行结束位置(通过下一个偏移量或文件末尾)
|
||||
var end int64
|
||||
if targetIndex < lineCount-1 {
|
||||
end = lineOffsets[targetIndex+1] - 1 // -1 移除换行符
|
||||
} else {
|
||||
end = int64(len(sentenceData))
|
||||
}
|
||||
|
||||
// 确保不越界并移除末尾可能的回车符 (Windows \r\n)
|
||||
line := sentenceData[start:end]
|
||||
line = bytes.TrimRight(line, "\r\n")
|
||||
|
||||
var sData []string
|
||||
if err := json.Unmarshal(line, &sData); err == nil && len(sData) >= 1 {
|
||||
name := sData[0]
|
||||
from := ""
|
||||
if len(sData) >= 2 {
|
||||
from = sData[1]
|
||||
}
|
||||
currentLine++
|
||||
|
||||
if from != "" {
|
||||
return "\"" + name + "\"—— " + from
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
return "欢迎使用白虎面板"
|
||||
|
||||
+10000
-10000
File diff suppressed because it is too large
Load Diff
@@ -5,7 +5,9 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v4/client"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
)
|
||||
|
||||
// replaceBodyPlaceholder 替换自定义 webhook body 中的 TEXT 占位符
|
||||
@@ -15,12 +17,19 @@ func replaceBodyPlaceholder(body string, content string) string {
|
||||
return strings.Replace(body, "TEXT", dataStr, -1)
|
||||
}
|
||||
|
||||
// createAliyunSMSClient 创建阿里云短信客户端
|
||||
// createAliyunSMSClient 创建阿里云短信客户端 (V2.0)
|
||||
func createAliyunSMSClient(accessKeyId, accessKeySecret, regionId string) (*dysmsapi.Client, error) {
|
||||
return dysmsapi.NewClientWithAccessKey(regionId, accessKeyId, accessKeySecret)
|
||||
config := &openapi.Config{
|
||||
AccessKeyId: tea.String(accessKeyId),
|
||||
AccessKeySecret: tea.String(accessKeySecret),
|
||||
RegionId: tea.String(regionId),
|
||||
}
|
||||
// 设置端点,通常为 dysmsapi.aliyuncs.com
|
||||
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
||||
return dysmsapi.NewClient(config)
|
||||
}
|
||||
|
||||
// sendAliyunSMS 发送短信
|
||||
// sendAliyunSMS 发送短信 (V2.0)
|
||||
func sendAliyunSMS(client *dysmsapi.Client, phoneNumber, signName, templateCode, content string, extra map[string]any) (string, error) {
|
||||
templateParam := map[string]interface{}{
|
||||
"content": content,
|
||||
@@ -32,21 +41,27 @@ func sendAliyunSMS(client *dysmsapi.Client, phoneNumber, signName, templateCode,
|
||||
}
|
||||
templateParamJSON, _ := json.Marshal(templateParam)
|
||||
|
||||
request := dysmsapi.CreateSendSmsRequest()
|
||||
request.Scheme = "https"
|
||||
request.PhoneNumbers = phoneNumber
|
||||
request.SignName = signName
|
||||
request.TemplateCode = templateCode
|
||||
request.TemplateParam = string(templateParamJSON)
|
||||
request := &dysmsapi.SendSmsRequest{
|
||||
PhoneNumbers: tea.String(phoneNumber),
|
||||
SignName: tea.String(signName),
|
||||
TemplateCode: tea.String(templateCode),
|
||||
TemplateParam: tea.String(string(templateParamJSON)),
|
||||
}
|
||||
|
||||
response, err := client.SendSms(request)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("发送短信失败: %s", err.Error())
|
||||
}
|
||||
|
||||
if response.Code != "OK" {
|
||||
return "", fmt.Errorf("发送失败: %s - %s", response.Code, response.Message)
|
||||
if response.Body == nil || tea.StringValue(response.Body.Code) != "OK" {
|
||||
msg := "Unknown Error"
|
||||
code := "Unknown Code"
|
||||
if response.Body != nil {
|
||||
msg = tea.StringValue(response.Body.Message)
|
||||
code = tea.StringValue(response.Body.Code)
|
||||
}
|
||||
return "", fmt.Errorf("发送失败: %s - %s", code, msg)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("RequestId: %s, BizId: %s", response.RequestId, response.BizId), nil
|
||||
return fmt.Sprintf("RequestId: %s, BizId: %s", tea.StringValue(response.Body.RequestId), tea.StringValue(response.Body.BizId)), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user