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.
172 lines
4.4 KiB
172 lines
4.4 KiB
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" |
|
"github.com/gogf/gf/v2/frame/g" |
|
"github.com/gogf/gf/v2/os/gctx" |
|
"log" |
|
"strings" |
|
) |
|
|
|
type IAliYunSms interface { |
|
ConfigInit() |
|
CreateClient() (_result *dysmsapi20170525.Client, _err error) |
|
Main(args []string) (_err error) |
|
} |
|
|
|
type aliYunSmsServiceTmpl struct { |
|
} |
|
|
|
var aliYunSmsService = aliYunSmsServiceTmpl{} |
|
|
|
func AliYunSms() IAliYunSms { |
|
return &aliYunSmsService |
|
} |
|
|
|
type AliyunSms struct { |
|
AccessKeyId string |
|
AccessSecret string |
|
RegionId string |
|
CodeSignName string |
|
TemplateCode string |
|
} |
|
|
|
var ( |
|
smsConfig AliyunSms |
|
) |
|
|
|
func init() { |
|
aliYunSmsService.ConfigInit() |
|
} |
|
|
|
func (a *aliYunSmsServiceTmpl) ConfigInit() { |
|
//加载游戏相关配置 |
|
ctx := gctx.New() |
|
|
|
smsConfig.AccessKeyId = g.Cfg().MustGet(ctx, "game.sms.accessKeyId").String() |
|
smsConfig.AccessSecret = g.Cfg().MustGet(ctx, "game.sms.accessSecret").String() |
|
smsConfig.RegionId = g.Cfg().MustGet(ctx, "game.sms.regionId").String() |
|
smsConfig.CodeSignName = g.Cfg().MustGet(ctx, "game.sms.codeSignName").String() |
|
smsConfig.TemplateCode = g.Cfg().MustGet(ctx, "game.sms.templateCode").String() |
|
|
|
log.Println("aliyunsms init: ", gjson.MustEncodeString(smsConfig)) |
|
} |
|
|
|
/* |
|
Description: |
|
|
|
* 使用凭据初始化账号Client |
|
* @return Client |
|
* @throws Exception |
|
*/ |
|
func (a *aliYunSmsServiceTmpl) CreateClient() (_result *dysmsapi20170525.Client, _err error) { |
|
// 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378661.html。 |
|
newCredential, _err := credential.NewCredential(nil) |
|
if _err != nil { |
|
return _result, _err |
|
} |
|
|
|
config := &openapi.Config{ |
|
Credential: newCredential, |
|
// 您的AccessKey ID |
|
AccessKeyId: tea.String(smsConfig.AccessKeyId), |
|
// 您的AccessKey Secret |
|
AccessKeySecret: tea.String(smsConfig.AccessSecret), |
|
RegionId: tea.String(smsConfig.RegionId), |
|
} |
|
// 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(smsConfig.CodeSignName), |
|
TemplateCode: tea.String(smsConfig.TemplateCode), |
|
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) |
|
// } |
|
//}
|
|
|