kefu/lib/alisms.go

75 lines
2.0 KiB
Go
Raw Normal View History

2024-12-10 02:50:12 +00:00
package lib
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
"kefu/models"
)
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *dysmsapi20170525.Client, _err error) {
config := &openapi.Config{
// 您的 AccessKey ID
AccessKeyId: accessKeyId,
// 您的 AccessKey Secret
AccessKeySecret: accessKeySecret,
}
// 访问的域名
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
_result = &dysmsapi20170525.Client{}
_result, _err = dysmsapi20170525.NewClient(config)
return _result, _err
}
func SendSms(phone, code string) (_err error) {
accessKeyId := models.FindConfig("OSS_ACCESS_KEY_ID")
accessKeySecret := models.FindConfig("OSS_ACCESS_KEY_SECRET")
client, _err := CreateClient(&accessKeyId, &accessKeySecret)
if _err != nil {
return _err
}
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: tea.String(phone),
SignName: tea.String(models.FindConfig("ALI_SMS_SIGN")),
TemplateCode: tea.String(models.FindConfig("ALI_SMS_CODE")),
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%s\"}", code)),
}
runtime := &util.RuntimeOptions{}
tryErr := func() (_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// 复制代码运行请自行打印 API 的返回值
_, _err = client.SendSmsWithOptions(sendSmsRequest, runtime)
if _err != nil {
return _err
}
return nil
}()
if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
}
// 如有需要,请打印 error
util.AssertAsString(error.Message)
}
return _err
}