first commit

This commit is contained in:
Yangtao
2025-11-18 17:48:20 +08:00
commit 6e56cab848
196 changed files with 65809 additions and 0 deletions

311
pkg/client/sms_client.go Normal file
View File

@ -0,0 +1,311 @@
package client
import (
"errors"
"fmt"
"servicebase/pkg/helper"
"servicebase/pkg/log"
"sync"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"github.com/spf13/viper"
)
// CreateAliSmsClient
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
func CreateAliSmsClient(accessKeyId *string, accessKeySecret *string) (_result *dysmsapi20170525.Client, _err error) {
config := &openapi.Config{
// 必填,您的 AccessKey ID
AccessKeyId: accessKeyId,
// 必填,您的 AccessKey Secret
AccessKeySecret: accessKeySecret,
}
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
_result = &dysmsapi20170525.Client{}
_result, _err = dysmsapi20170525.NewClient(config)
return _result, _err
}
var (
smsSignList = []string{"淮南东东网络科技"}
tempCodeList = []string{"SMS_320216048"}
smsSignCodeLock sync.Mutex
smsSignCodeIndex int64
)
func getSignAndCode() (sign, code string) {
smsSignCodeLock.Lock()
defer smsSignCodeLock.Unlock()
i := smsSignCodeIndex % int64(len(smsSignList))
sign = smsSignList[int(i)]
code = tempCodeList[int(i)]
smsSignCodeIndex++
return
}
func SendVerifyCode01(mobile, content string, fn func(_result *dysmsapi20170525.SendSmsResponse, success bool)) (_result *dysmsapi20170525.SendSmsResponse, e error) {
if !helper.IsMobile(mobile) {
return nil, errors.New("手机号有误!")
}
sign, code := getSignAndCode()
log.InfoF("get sms sign and code: %s %s", sign, code)
_result, e = SendMobileMsg01(mobile, sign, code, content)
if fn != nil {
fn(_result, e == nil)
}
return
}
func SendMobileMsg01(mobile, signName, tempCode, content string) (_result *dysmsapi20170525.SendSmsResponse, _err error) {
var (
ID = viper.GetString("aliyun.sms.accessKeyId")
Secret = viper.GetString("aliyun.sms.accessKeySecret")
)
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式更多鉴权访问方式请参见https://help.aliyun.com/document_detail/378661.html
client, _err := CreateAliSmsClient(tea.String(ID), tea.String(Secret))
if _err != nil {
return nil, _err
}
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: tea.String(mobile),
SignName: tea.String(signName),
TemplateCode: tea.String(tempCode),
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%s\"}", content)),
}
res, tryErr := func() (_result *dysmsapi20170525.SendSmsResponse, _e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// 复制代码运行请自行打印 API 的返回值
_result, _err = client.SendSmsWithOptions(sendSmsRequest, &util.RuntimeOptions{})
if _err != nil {
return nil, _err
}
return
}()
if tryErr != nil {
var e = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
e = _t
} else {
e.Message = tea.String(tryErr.Error())
}
// 如有需要,请打印 error
_, _err = util.AssertAsString(e.Message)
if _err != nil {
return nil, _err
}
}
return res, _err
}
func SendVerifyCodeUsaAndCanada(mobile, content string, fn func(_result *dysmsapi20170525.SendSmsResponse, success bool)) (_result *dysmsapi20170525.SendSmsResponse, e error) {
// if !helper.IsMobile(mobile) {
// return nil, errors.New("手机号有误!")
// }
sign, code := getSignAndCode()
log.InfoF("SendVerifyCodeUsaAndCanada get sms sign and code: %s %s", sign, code)
_result, e = SendMobileMsgUsaAndCanada(mobile, sign, code, content)
if fn != nil {
fn(_result, e == nil)
}
return
}
func SendVerifyCodeGlobalOther(mobile, content string, fn func(_result *dysmsapi20170525.SendSmsResponse, success bool)) (_result *dysmsapi20170525.SendSmsResponse, e error) {
// if !helper.IsMobile(mobile) {
// return nil, errors.New("手机号有误!")
// }
sign, code := getSignAndCode()
log.InfoF("SendVerifyCodeUsaAndCanada get sms sign and code: %s %s", sign, code)
_result, e = SendMobileMsgGlobalOther(mobile, sign, code, content)
if fn != nil {
fn(_result, e == nil)
}
return
}
// SendMobileMsgUsaAndCanada 美国/加拿大地区短信发送
func SendMobileMsgUsaAndCanada(mobile, signName, tempCode, content string) (_result *dysmsapi20170525.SendSmsResponse, _err error) {
var (
ID = viper.GetString("aliyun.sms.accessKeyId")
Secret = viper.GetString("aliyun.sms.accessKeySecret")
)
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式更多鉴权访问方式请参见https://help.aliyun.com/document_detail/378661.html
client, _err := CreateAliSmsClient(tea.String(ID), tea.String(Secret))
if _err != nil {
return nil, _err
}
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: tea.String(mobile),
SignName: tea.String(signName),
TemplateCode: tea.String(tempCode),
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%s\"}", content)),
}
res, tryErr := func() (_result *dysmsapi20170525.SendSmsResponse, _e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// 复制代码运行请自行打印 API 的返回值
_result, _err = client.SendSmsWithOptions(sendSmsRequest, &util.RuntimeOptions{})
if _err != nil {
return nil, _err
}
return
}()
if tryErr != nil {
var e = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
e = _t
} else {
e.Message = tea.String(tryErr.Error())
}
// 如有需要,请打印 error
_, _err = util.AssertAsString(e.Message)
if _err != nil {
return nil, _err
}
}
return res, _err
}
// SendMobileMsgGlobalOther 全球其他国家
func SendMobileMsgGlobalOther(mobile, signName, tempCode, content string) (_result *dysmsapi20170525.SendSmsResponse, _err error) {
var (
// ID AccessKey ID
// LTAI5tRfkJnr6Y6NU2T6jWoB
//
// AccessKey Secret
// BcaMWgXkwdxdJXFnCwRQbEl3Q3Xtxl
// TODO: 替换为您的AccessKey ID和AccessKey Secret
ID = viper.GetString("aliyun.sms.accessKeyId")
Secret = viper.GetString("aliyun.sms.accessKeySecret")
)
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式更多鉴权访问方式请参见https://help.aliyun.com/document_detail/378661.html
client, _err := CreateAliSmsClient(tea.String(ID), tea.String(Secret))
if _err != nil {
return nil, _err
}
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: tea.String(mobile),
SignName: tea.String(signName),
TemplateCode: tea.String(tempCode),
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%s\"}", content)),
}
res, tryErr := func() (_result *dysmsapi20170525.SendSmsResponse, _e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// 复制代码运行请自行打印 API 的返回值
_result, _err = client.SendSmsWithOptions(sendSmsRequest, &util.RuntimeOptions{})
if _err != nil {
return nil, _err
}
return
}()
if tryErr != nil {
var e = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
e = _t
} else {
e.Message = tea.String(tryErr.Error())
}
// 如有需要,请打印 error
_, _err = util.AssertAsString(e.Message)
if _err != nil {
return nil, _err
}
}
return res, _err
}
func SendVerifyHKMacauAndTW(mobile, content string, fn func(_result *dysmsapi20170525.SendSmsResponse, success bool)) (_result *dysmsapi20170525.SendSmsResponse, e error) {
if !helper.IsMobile(mobile) {
return nil, errors.New("手机号有误!")
}
sign, code := getSignAndCode()
log.InfoF("get sms sign and code: %s %s", sign, code)
_result, e = SendMobileMsgHKMacauAndTW(mobile, sign, code, content)
if fn != nil {
fn(_result, e == nil)
}
return
}
// SendMobileMsgHKMacauAndTW 港澳台
func SendMobileMsgHKMacauAndTW(mobile, signName, tempCode, content string) (_result *dysmsapi20170525.SendSmsResponse, _err error) {
var (
ID = viper.GetString("aliyun.sms.accessKeyId")
Secret = viper.GetString("aliyun.sms.accessKeySecret")
)
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式更多鉴权访问方式请参见https://help.aliyun.com/document_detail/378661.html
client, _err := CreateAliSmsClient(tea.String(ID), tea.String(Secret))
if _err != nil {
return nil, _err
}
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: tea.String(mobile),
SignName: tea.String(signName),
TemplateCode: tea.String(tempCode),
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%s\"}", content)),
}
res, tryErr := func() (_result *dysmsapi20170525.SendSmsResponse, _e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// 复制代码运行请自行打印 API 的返回值
_result, _err = client.SendSmsWithOptions(sendSmsRequest, &util.RuntimeOptions{})
if _err != nil {
return nil, _err
}
return
}()
if tryErr != nil {
var e = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
e = _t
} else {
e.Message = tea.String(tryErr.Error())
}
// 如有需要,请打印 error
_, _err = util.AssertAsString(e.Message)
if _err != nil {
return nil, _err
}
}
return res, _err
}