aliyun-communicate包高版本下异常
当前go版本:go version go1.23.0 windows/amd64
在使用github.com/KenmyZhang/aliyun-communicate
时编译失败,编译信息如下:
panic: encoding alphabet includes duplicate symbols
goroutine 1 [running]:
encoding/base32.NewEncoding(...)
******/Go/src/encoding/base32/base32.go:79
github.com/KenmyZhang/aliyun-communicate/sms-lib.init()
******/pkg/mod/github.com/!kenmy!zhang/aliyun-communicate@v0.0.0-20180308134849-7997edc57454/sms-lib/sms-lib.go:45 +0xf4
以前用go 1.20正常没问题,所以直接利用阿里云自己提供的sdk,重写了pkg/sms/driver_aliyun.go
文件,代码如下:
/*
Package sms
@Author : Uyynot
@Email : uyynot@qq.com
@Time : 2024/9/6 16:53
@File : driver_aliyun.go
@Project : go_hub
@Desc :
*/
package sms
import (
"encoding/json"
"learn_for_go_hub/pkg/logger"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
)
// Aliyun 实现sms.Driver接口
type Aliyun struct{}
// Send
//
// @Description: 发送邮件
// @receiver a
// @param phone
// @param message
// @param config
// @return bool
func (a *Aliyun) Send(phone string, message Message, config map[string]string) bool {
templateParam, err := json.Marshal(message.Data)
if err != nil {
logger.ErrorString("短信【阿里云】", "解析绑定错误", err.Error())
return false
}
openapiConfig := &openapi.Config{}
openapiConfig.AccessKeyId = tea.String(config["access_key_id"])
openapiConfig.AccessKeySecret = tea.String(config["access_key_secret"])
smsClient, err := dysmsapi.NewClient(openapiConfig)
if err != nil {
logger.ErrorString("短信【阿里云】", "NewClient失败", err.Error())
return false
}
// 1.发送短信
sendReq := &dysmsapi.SendSmsRequest{
PhoneNumbers: tea.String(phone),
SignName: tea.String(config["sign_name"]),
TemplateCode: tea.String(message.Template),
TemplateParam: tea.String(string(templateParam)),
}
sendResp, _err := smsClient.SendSms(sendReq)
logger.DebugJSON("短信【阿里云】", "接口内容", sendReq)
logger.DebugJSON("短信【阿里云】", "接口响应", sendResp)
if _err != nil {
logger.ErrorString("短信【阿里云】", "发信失败", err.Error())
return false
}
resultJSON, err := json.Marshal(sendResp)
if err != nil {
logger.ErrorString("短信【阿里云】", "解析相应JSON错误", err.Error())
return false
}
code := sendResp.Body.Code
if !tea.BoolValue(util.EqualString(code, tea.String("OK"))) {
logger.ErrorString("短信【阿里云】", "服务商返回错误", string(resultJSON))
return false
}
logger.DebugString("短信【阿里云】", "发信成功", "")
return true
}
推荐文章: