Files
lckt-server/utils/sms/sms.go
2025-07-23 02:35:04 +08:00

143 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package sms
// SMS短信服务
import (
"crypto/tls"
"encoding/json"
"fmt"
"git.echol.cn/loser/lckt/global"
"github.com/alibabacloud-go/tea/tea"
"go.uber.org/zap"
"net/http"
"net/url"
"strings"
util "github.com/alibabacloud-go/tea-utils/v2/service"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v4/client"
)
type message struct {
Code string `json:"code"`
}
func SendSMS(phone string, code string) bool {
// 从配置中获取阿里云短信配置
aliyunConfig := global.GVA_CONFIG.SMS
clientConfig := &openapi.Config{
AccessKeyId: tea.String(aliyunConfig.AccessKeyID),
AccessKeySecret: tea.String(aliyunConfig.AccessKeySecret),
Endpoint: tea.String("dysmsapi.aliyuncs.com"),
RegionId: tea.String("cn-hangzhou"), // 添加区域配置
}
smsClient, err := dysmsapi.NewClient(clientConfig)
if err != nil {
global.GVA_LOG.Error("创建短信客户端失败", zap.Error(err))
return false
}
mes := message{Code: code}
templateParam, err := json.Marshal(mes)
if err != nil {
global.GVA_LOG.Error("模板参数序列化失败", zap.Error(err))
return false
}
sendSmsRequest := &dysmsapi.SendSmsRequest{
PhoneNumbers: tea.String(phone),
SignName: tea.String(aliyunConfig.SignName),
TemplateCode: tea.String(aliyunConfig.TemplateCode),
TemplateParam: tea.String(string(templateParam)),
}
// 添加更多的运行时选项
runtime := &util.RuntimeOptions{
ReadTimeout: tea.Int(5000),
ConnectTimeout: tea.Int(5000),
}
response, err := smsClient.SendSmsWithOptions(sendSmsRequest, runtime)
if err != nil {
global.GVA_LOG.Error("发送短信失败", zap.Error(err))
return false
}
if response.Body == nil || *response.Body.Code != "OK" {
if response.Body != nil {
global.GVA_LOG.Error("服务商返回错误", zap.Any("resp", *response.Body.Message))
} else {
global.GVA_LOG.Error("服务商返回错误", zap.String("resp", "response body is nil"))
}
return false
}
global.GVA_LOG.Info("短信[阿里云]", zap.String("发送成功", "手机号: "+phone))
return true
}
func SendSMSTest(phone, code string) bool {
endpoint := "https://dfsns.market.alicloudapi.com/data/send_sms"
templateID := "CST_ptdie100"
// 构造 POST 表单数据
form := url.Values{}
form.Set("content", fmt.Sprintf("code:%s", code))
form.Set("template_id", templateID)
form.Set("phone_number", phone)
// 创建 HTTP 请求
req, err := http.NewRequest("POST", endpoint, strings.NewReader(form.Encode()))
if err != nil {
return false
}
// 添加请求头
req.Header.Set("Authorization", "APPCODE "+"b8f46ced154b44c5a40a0a49a91e1634")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// 创建 HTTP 客户端(跳过证书校验,模拟 curl -k
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
// 发送请求
resp, err := client.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
if resp.Status != "ok" {
return false
}
return true
}
func DxbSendSMS(phone, code string) bool {
// 内容 通过urlEncode编码
content := "【海口龙华铁坚成电子商务商行】您的验证码是" + code + "。如非本人操作,请忽略本短信"
// urlencode编码内容
content = url.QueryEscape(content)
api := "https://api.smsbao.com/sms?u=lchz5599&p=7ea114c87a224cd38a0d616b9be3faed&g=海口龙华铁坚成电子商务商行&m=" + phone + "&c=" + content
// 发送GET请求
resp, err := http.Get(api)
if err != nil {
global.GVA_LOG.Error("发送短信请求失败:", zap.Error(err))
return false
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
global.GVA_LOG.Info("发送短信请求成功,手机号:" + phone + ",验证码:" + code)
return true
} else {
global.GVA_LOG.Error("发送短信请求失败:", zap.Error(err))
return false
}
}