You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
3.6 KiB
142 lines
3.6 KiB
2 months ago
|
package service
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"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"
|
||
|
credential "github.com/aliyun/credentials-go/credentials"
|
||
|
"github.com/gogf/gf/v2/encoding/gjson"
|
||
|
"log"
|
||
|
"strings"
|
||
|
"tyj_admin/internal/consts"
|
||
|
)
|
||
|
|
||
|
type IAliYunSms interface {
|
||
|
CreateClient() (_result *dysmsapi20170525.Client, _err error)
|
||
|
Main(args []string) (_err error)
|
||
|
}
|
||
|
|
||
|
type aliYunSmsServiceTmpl struct {
|
||
|
}
|
||
|
|
||
|
var aliYunSmsService = aliYunSmsServiceTmpl{}
|
||
|
|
||
|
func AliYunSms() IAliYunSms {
|
||
|
return &aliYunSmsService
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
Description:
|
||
|
|
||
|
* 使用凭据初始化账号Client
|
||
|
* @return Client
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
func (a *aliYunSmsServiceTmpl) CreateClient() (_result *dysmsapi20170525.Client, _err error) {
|
||
|
// 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378661.html。
|
||
|
credential, _err := credential.NewCredential(nil)
|
||
|
if _err != nil {
|
||
|
return _result, _err
|
||
|
}
|
||
|
|
||
|
config := &openapi.Config{
|
||
|
Credential: credential,
|
||
|
// 您的AccessKey ID
|
||
|
AccessKeyId: tea.String(consts.CODE_ACCESS_KEY_ID),
|
||
|
// 您的AccessKey Secret
|
||
|
AccessKeySecret: tea.String(consts.CODE_ACCESS_SECRET),
|
||
|
RegionId: tea.String(consts.CODE_REGION_ID),
|
||
|
}
|
||
|
// 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
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
Description:
|
||
|
|
||
|
* 使用凭据初始化账号Client
|
||
|
* @return Client
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
|
||
|
func (a *aliYunSmsServiceTmpl) Main(args []string) (_err error) {
|
||
|
client, _err := a.CreateClient()
|
||
|
if _err != nil {
|
||
|
return _err
|
||
|
}
|
||
|
|
||
|
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
||
|
PhoneNumbers: tea.String(args[0]),
|
||
|
SignName: tea.String(consts.CODE_SIGN_NAME),
|
||
|
TemplateCode: tea.String(consts.TEMPLATE_CODE_PASSWORD),
|
||
|
TemplateParam: tea.String(gjson.MustEncodeString(map[string]string{"code": args[1]})),
|
||
|
}
|
||
|
|
||
|
sendSmsRes := &dysmsapi20170525.SendSmsResponse{}
|
||
|
tryErr := func() (_e error) {
|
||
|
defer func() {
|
||
|
if r := tea.Recover(recover()); r != nil {
|
||
|
_e = r
|
||
|
}
|
||
|
}()
|
||
|
// 复制代码运行请自行打印 API 的返回值
|
||
|
sendSmsRes, _err = client.SendSmsWithOptions(sendSmsRequest, &util.RuntimeOptions{})
|
||
|
log.Print("sendSmsRes: ", gjson.MustEncodeString(sendSmsRes))
|
||
|
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())
|
||
|
}
|
||
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||
|
// 错误 message
|
||
|
fmt.Println(tea.StringValue(error.Message))
|
||
|
// 诊断地址
|
||
|
var data interface{}
|
||
|
d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
|
||
|
d.Decode(&data)
|
||
|
if m, ok := data.(map[string]interface{}); ok {
|
||
|
recommend, _ := m["Recommend"]
|
||
|
fmt.Println(recommend)
|
||
|
}
|
||
|
//_, _err = util.AssertAsString(error.Message)
|
||
|
if error.Message != nil {
|
||
|
return errors.New(*error.Message)
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
fmt.Println("sendSmsRes: Body ", sendSmsRes.Body.GoString())
|
||
|
body := sendSmsRes.Body.GoString()
|
||
|
jsonBody := map[string]interface{}{}
|
||
|
_err = json.Unmarshal([]byte(body), &jsonBody)
|
||
|
if _err != nil {
|
||
|
return _err
|
||
|
}
|
||
|
|
||
|
return errors.New(fmt.Sprint(jsonBody["Message"]))
|
||
|
}
|
||
|
return _err
|
||
|
}
|
||
|
|
||
|
//func main() {
|
||
|
// err := _main(tea.StringSlice(os.Args[1:]))
|
||
|
// if err != nil {
|
||
|
// panic(err)
|
||
|
// }
|
||
|
//}
|