初始化提交
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
// Package aliyun 短信
|
||||
package aliyun
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
|
||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
"hospital-open-api/api/dao"
|
||||
"hospital-open-api/api/model"
|
||||
"hospital-open-api/config"
|
||||
)
|
||||
|
||||
func createClient() (_result *dysmsapi20170525.Client, _err error) {
|
||||
accessKeyId := config.C.Dysms.DysmsAccessKey
|
||||
accessKeySecret := config.C.Dysms.DysmsAccessSecret
|
||||
|
||||
openapiConfig := &openapi.Config{
|
||||
// 必填,您的 AccessKey ID
|
||||
AccessKeyId: &accessKeyId,
|
||||
// 必填,您的 AccessKey Secret
|
||||
AccessKeySecret: &accessKeySecret,
|
||||
}
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
|
||||
openapiConfig.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
||||
_result = &dysmsapi20170525.Client{}
|
||||
_result, _err = dysmsapi20170525.NewClient(openapiConfig)
|
||||
return _result, _err
|
||||
}
|
||||
|
||||
// SendSms 发送短信
|
||||
func SendSms(phoneNumber, templateCode, sceneDesc string, templateParam map[string]interface{}) error {
|
||||
client, err := createClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params, err := json.Marshal(templateParam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
||||
PhoneNumbers: tea.String(phoneNumber),
|
||||
SignName: tea.String("肝胆相照"),
|
||||
TemplateCode: tea.String(templateCode),
|
||||
TemplateParam: tea.String(string(params)),
|
||||
}
|
||||
|
||||
tryErr := func() (e error) {
|
||||
defer func() {
|
||||
if r := tea.Recover(recover()); r != nil {
|
||||
e = r
|
||||
}
|
||||
}()
|
||||
|
||||
// 初始化运行时配置。
|
||||
runtime := &util.RuntimeOptions{}
|
||||
// 读取超时
|
||||
runtime.SetReadTimeout(10000)
|
||||
// 连接超时
|
||||
runtime.SetConnectTimeout(5000)
|
||||
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
response, err := client.SendSms(sendSmsRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if response.Body == nil {
|
||||
return errors.New("短信发送失败")
|
||||
}
|
||||
|
||||
if response.Body.Code != nil && *response.Body.Code != "OK" {
|
||||
if response.Body.Message != nil {
|
||||
return errors.New(*response.Body.Message)
|
||||
}
|
||||
}
|
||||
|
||||
// 检测唯一值返回
|
||||
if response.Body.RequestId == nil {
|
||||
if response.Body.Message != nil {
|
||||
return errors.New(*response.Body.Message)
|
||||
}
|
||||
}
|
||||
|
||||
// 记录log
|
||||
logSms := &model.LogSms{
|
||||
Type: 1,
|
||||
Status: 1,
|
||||
Phone: phoneNumber,
|
||||
TemplateCode: templateCode,
|
||||
ThirdCode: *response.Body.RequestId,
|
||||
SceneDesc: sceneDesc,
|
||||
Remarks: string(params),
|
||||
}
|
||||
|
||||
logSmsDao := dao.LogSms{}
|
||||
_, _ = logSmsDao.AddLogSmsUnTransaction(logSms)
|
||||
|
||||
return nil
|
||||
}()
|
||||
|
||||
if tryErr != nil {
|
||||
var sdkError = &tea.SDKError{}
|
||||
if t, ok := tryErr.(*tea.SDKError); ok {
|
||||
sdkError = t
|
||||
} else {
|
||||
sdkError.Message = tea.String(tryErr.Error())
|
||||
}
|
||||
// 如有需要,请打印 error
|
||||
_, err = util.AssertAsString(sdkError.Message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package aliyun
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"hospital-open-api/config"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetOssSignResponse 获取oss签名返回值
|
||||
type GetOssSignResponse struct {
|
||||
AccessId string `json:"access_id"` // 主键id
|
||||
Host string `json:"host"`
|
||||
Policy string `json:"policy"`
|
||||
Signature string `json:"signature"`
|
||||
Expire int64 `json:"expire"`
|
||||
Callback string `json:"callback"`
|
||||
Dir string `json:"dir"`
|
||||
}
|
||||
|
||||
// GetOssSign 获取oss签名
|
||||
func GetOssSign(dir string) (*GetOssSignResponse, error) {
|
||||
// Endpoint := config.C.Oss.OssEndpoint
|
||||
// accessKey := config.C.Oss.OssAccessKey
|
||||
// accessSecret := config.C.Oss.OssAccessKeySecret
|
||||
// client, err := oss.New(Endpoint, accessKey, accessSecret)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
//
|
||||
// bucket, err := client.Bucket(viper.GetString("aliyun.Bucket"))
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
|
||||
now := time.Now()
|
||||
expire := 30 // 设置该policy超时时间是30s,即这个policy过了这个有效时间,将不能访问。
|
||||
end := now.Add(time.Second * time.Duration(expire))
|
||||
expiration := strings.Replace(end.Format("2006-01-02T15:04:05.000Z"), "+00:00", ".000Z", 1)
|
||||
|
||||
start := []interface{}{"starts-with", "$key", dir}
|
||||
conditions := [][]interface{}{start}
|
||||
|
||||
arr := map[string]interface{}{
|
||||
"expiration": expiration,
|
||||
"conditions": conditions,
|
||||
}
|
||||
policy, _ := json.Marshal(arr)
|
||||
base64Policy := base64.StdEncoding.EncodeToString(policy)
|
||||
stringToSign := base64Policy
|
||||
h := hmac.New(sha1.New, []byte(config.C.Oss.OssAccessKeySecret))
|
||||
h.Write([]byte(stringToSign))
|
||||
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
|
||||
|
||||
response := &GetOssSignResponse{
|
||||
AccessId: config.C.Oss.OssAccessKey,
|
||||
Host: config.C.Oss.OssCustomDomainName,
|
||||
Policy: base64Policy,
|
||||
Signature: signature,
|
||||
Expire: end.Unix(),
|
||||
Callback: "",
|
||||
Dir: dir,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
Reference in New Issue
Block a user