mirror of
https://github.com/yangjian102621/geekai.git
synced 2026-08-13 03:00:59 +00:00
feat(release): migrate GeekAI v4.3.0 to open source
- Sync backend and frontend from GeekAI Plus v4.3.0 - Remove commercial License flows and update open-source deployment defaults - Preserve Docker Compose deployment and bump image tags to v4.3.0 BREAKING CHANGE: commercial License configuration and related endpoints are removed
This commit is contained in:
+4
-10
@@ -8,14 +8,14 @@ package sms
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"geekai/core/types"
|
||||
"geekai/utils"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BaoSmsService struct {
|
||||
@@ -58,15 +58,9 @@ func (s *BaoSmsService) SendVerifyCode(mobile string, code int) error {
|
||||
params.Set("c", content)
|
||||
|
||||
apiURL := fmt.Sprintf("https://%s/sms?%s", s.domain, params.Encode())
|
||||
response, err := http.Get(apiURL)
|
||||
body, status, err := utils.FetchURLBytes(context.Background(), apiURL, "", 30*time.Second, 2, 2<<20)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("smsbao request failed: status=%d: %w", status, err)
|
||||
}
|
||||
result := string(body)
|
||||
logger.Debugf("send SmsBao result: %v", errMsg[result])
|
||||
|
||||
@@ -9,6 +9,7 @@ package sms
|
||||
|
||||
const Ali = "aliyun"
|
||||
const Bao = "bao"
|
||||
const Tencent = "tencent"
|
||||
|
||||
type Service interface {
|
||||
SendVerifyCode(mobile string, code int) error
|
||||
|
||||
@@ -9,23 +9,25 @@ package sms
|
||||
|
||||
import (
|
||||
"geekai/core/types"
|
||||
logger2 "geekai/logger"
|
||||
"geekai/log"
|
||||
)
|
||||
|
||||
type SmsManager struct {
|
||||
aliyun *AliYunSmsService
|
||||
bao *BaoSmsService
|
||||
active string
|
||||
aliyun *AliYunSmsService
|
||||
bao *BaoSmsService
|
||||
tencent *TencentSmsService
|
||||
active string
|
||||
}
|
||||
|
||||
var logger = logger2.GetLogger()
|
||||
var logger = log.GetLogger()
|
||||
|
||||
func NewSmsManager(sysConfig *types.SystemConfig, aliyun *AliYunSmsService, bao *BaoSmsService) (*SmsManager, error) {
|
||||
func NewSmsManager(sysConfig *types.SystemConfig, aliyun *AliYunSmsService, bao *BaoSmsService, tencent *TencentSmsService) (*SmsManager, error) {
|
||||
|
||||
return &SmsManager{
|
||||
active: sysConfig.SMS.Active,
|
||||
aliyun: aliyun,
|
||||
bao: bao,
|
||||
active: sysConfig.SMS.Active,
|
||||
aliyun: aliyun,
|
||||
bao: bao,
|
||||
tencent: tencent,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -35,6 +37,8 @@ func (m *SmsManager) GetService() Service {
|
||||
return m.aliyun
|
||||
case Bao:
|
||||
return m.bao
|
||||
case Tencent:
|
||||
return m.tencent
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -49,6 +53,8 @@ func (m *SmsManager) UpdateConfig(config types.SMSConfig) {
|
||||
m.aliyun.UpdateConfig(config.Ali)
|
||||
case Bao:
|
||||
m.bao.UpdateConfig(config.Bao)
|
||||
case Tencent:
|
||||
m.tencent.UpdateConfig(config.Tencent)
|
||||
}
|
||||
m.active = config.Active
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package sms
|
||||
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
|
||||
// * Use of this source code is governed by a Apache-2.0 license
|
||||
// * that can be found in the LICENSE file.
|
||||
// * @Author yangjian102621@163.com
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"geekai/core/types"
|
||||
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||
sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111"
|
||||
)
|
||||
|
||||
type TencentSmsService struct {
|
||||
config types.SmsConfigTencent
|
||||
client *sms.Client
|
||||
region string
|
||||
}
|
||||
|
||||
func NewTencentSmsService(sysConfig *types.SystemConfig) (*TencentSmsService, error) {
|
||||
config := sysConfig.SMS.Tencent
|
||||
region := config.Region
|
||||
if region == "" {
|
||||
region = "ap-guangzhou" // 默认使用广州地区
|
||||
}
|
||||
|
||||
s := TencentSmsService{
|
||||
config: config,
|
||||
region: region,
|
||||
}
|
||||
if sysConfig.SMS.Active == Tencent {
|
||||
err := s.UpdateConfig(config)
|
||||
if err != nil {
|
||||
logger.Errorf("腾讯云短信初始化失败: %v", err)
|
||||
}
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func (s *TencentSmsService) UpdateConfig(config types.SmsConfigTencent) error {
|
||||
if config.SecretId == "" || config.SecretKey == "" {
|
||||
// 配置不完整时不初始化客户端
|
||||
s.config = config
|
||||
if config.Region != "" {
|
||||
s.region = config.Region
|
||||
} else {
|
||||
s.region = "ap-guangzhou"
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
region := config.Region
|
||||
if region == "" {
|
||||
region = "ap-guangzhou"
|
||||
}
|
||||
|
||||
// 创建凭证
|
||||
credential := common.NewCredential(
|
||||
config.SecretId,
|
||||
config.SecretKey,
|
||||
)
|
||||
|
||||
// 创建客户端配置
|
||||
cpf := profile.NewClientProfile()
|
||||
cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
|
||||
|
||||
// 创建客户端
|
||||
client, err := sms.NewClient(credential, region, cpf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create client: %v", err)
|
||||
}
|
||||
|
||||
s.client = client
|
||||
s.config = config
|
||||
s.region = region
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendVerifyCode 发送验证码短信
|
||||
// 注意:腾讯云后台配置的短信模板内容应与配置中的 code_template 一致
|
||||
// 模板只需要1个参数:{1} 表示验证码,例如:{1}为您的验证码,请于5分钟内填写,如非本人操作,请忽略本短信。
|
||||
func (s *TencentSmsService) SendVerifyCode(mobile string, code int) error {
|
||||
if s.client == nil {
|
||||
return fmt.Errorf("腾讯云短信服务未初始化")
|
||||
}
|
||||
|
||||
// 创建发送短信请求
|
||||
request := sms.NewSendSmsRequest()
|
||||
request.SmsSdkAppId = common.StringPtr(s.config.SmsSdkAppId)
|
||||
request.SignName = common.StringPtr(s.config.Sign)
|
||||
request.TemplateId = common.StringPtr(s.config.CodeTempId)
|
||||
request.PhoneNumberSet = common.StringPtrs([]string{mobile})
|
||||
request.TemplateParamSet = common.StringPtrs([]string{fmt.Sprintf("%d", code), "5"})
|
||||
|
||||
// 发送短信
|
||||
response, err := s.client.SendSms(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send SMS: %v", err)
|
||||
}
|
||||
|
||||
// 检查响应
|
||||
if response.Response == nil {
|
||||
return fmt.Errorf("failed to send SMS: response is nil")
|
||||
}
|
||||
|
||||
if len(response.Response.SendStatusSet) == 0 {
|
||||
return fmt.Errorf("failed to send SMS: no send status")
|
||||
}
|
||||
|
||||
sendStatus := response.Response.SendStatusSet[0]
|
||||
if sendStatus.Code == nil || *sendStatus.Code != "Ok" {
|
||||
message := "unknown error"
|
||||
if sendStatus.Message != nil {
|
||||
message = *sendStatus.Message
|
||||
}
|
||||
return fmt.Errorf("failed to send SMS: %s", message)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ Service = &TencentSmsService{}
|
||||
Reference in New Issue
Block a user