123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package sms
- import (
- "encoding/json"
- 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"
- )
- type Aliyun struct {
- SignName string
- AppId string
- AppKey string
- TemplateId string
- }
- /**
- * 使用AK&SK初始化账号Client
- * @param accessKeyId
- * @param accessKeySecret
- * @return Client
- * @throws Exception
- */
- func (c *Aliyun) CreateClient() (_result *dysmsapi20170525.Client, _err error) {
- config := &openapi.Config{
- // 您的 AccessKey ID
- AccessKeyId: tea.String(c.AppId),
- // 您的 AccessKey Secret
- AccessKeySecret: tea.String(c.AppKey),
- }
- // 访问的域名
- config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
- _result = &dysmsapi20170525.Client{}
- _result, _err = dysmsapi20170525.NewClient(config)
- return _result, _err
- }
- func (c *Aliyun) Send(datatype, dataparams, phones interface{}) interface{} {
- client, _err := c.CreateClient()
- if _err != nil {
- return _err
- }
- dataparamsMap := dataparams.(map[string]interface{})
- dataparamsMapStr, _ := json.Marshal(dataparamsMap)
- phonesStr, _ := json.Marshal(phones.([]string))
- sendSmsRequest := &dysmsapi20170525.SendBatchSmsRequest{
- SignNameJson: tea.String(c.SignName),
- TemplateCode: tea.String(c.TemplateId),
- PhoneNumberJson: tea.String(string(phonesStr)),
- TemplateParamJson: tea.String(string(dataparamsMapStr)),
- }
- runtime := &util.RuntimeOptions{}
- tryErr := func() (_e error) {
- defer func() {
- if r := tea.Recover(recover()); r != nil {
- _e = r
- }
- }()
- // 复制代码运行请自行打印 API 的返回值
- _, _err = client.SendBatchSmsWithOptions(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
- }
|