Files
servicebase/pkg/client/sms_client.go
2025-11-18 18:33:22 +08:00

276 lines
7.7 KiB
Go

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"
)
func CreateAliSmsClient(accessKeyId *string, accessKeySecret *string) (_result *dysmsapi20170525.Client, _err error) {
config := &openapi.Config{
AccessKeyId: accessKeyId,
AccessKeySecret: accessKeySecret,
}
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")
)
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
}
}()
_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) {
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) {
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
}
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")
)
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
}
}()
_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 SendMobileMsgGlobalOther(mobile, signName, tempCode, content string) (_result *dysmsapi20170525.SendSmsResponse, _err error) {
var (
ID = viper.GetString("aliyun.sms.accessKeyId")
Secret = viper.GetString("aliyun.sms.accessKeySecret")
)
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())
}
_, _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")
)
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
}