mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-11 03:33:53 +08:00
发布v2.8.4版本,更新内容请查看:https://github.com/bufanyun/hotgo/tree/v2.0/docs/guide-zh-CN/addon-version-upgrade.md
This commit is contained in:
@@ -1,120 +0,0 @@
|
||||
package aliyun
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
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"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/internal/model"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
)
|
||||
|
||||
var (
|
||||
Handle = aliYun{}
|
||||
)
|
||||
|
||||
type aliYun struct{}
|
||||
|
||||
// SendCode 发送验证码
|
||||
func (d *aliYun) SendCode(ctx context.Context, in sysin.SendCodeInp, config *model.SmsConfig) (err error) {
|
||||
if config == nil {
|
||||
config, err = service.SysConfig().GetSms(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
client, err := CreateClient(tea.String(config.AliYunAccessKeyID), tea.String(config.AliYunAccessKeySecret))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
||||
PhoneNumbers: tea.String(in.Mobile),
|
||||
SignName: tea.String(config.AliYunSign),
|
||||
TemplateCode: tea.String(in.Template),
|
||||
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%v\"}", in.Code)),
|
||||
}
|
||||
|
||||
tryErr := func() (_e error) {
|
||||
defer func() {
|
||||
if r := tea.Recover(recover()); r != nil {
|
||||
_e = r
|
||||
}
|
||||
}()
|
||||
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
response, err := client.SendSmsWithOptions(sendSmsRequest, &util.RuntimeOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.Log().Debugf(ctx, "aliyun.sendCode response:%+v", response.GoString())
|
||||
|
||||
return nil
|
||||
}()
|
||||
return tryErr
|
||||
}
|
||||
|
||||
// CreateClient 使用AK&SK初始化账号Client
|
||||
func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *dysmsapi20170525.Client, _err error) {
|
||||
config := &openapi.Config{
|
||||
// 必填,您的 AccessKey ID
|
||||
AccessKeyId: accessKeyId,
|
||||
// 必填,您的 AccessKey Secret
|
||||
AccessKeySecret: accessKeySecret,
|
||||
}
|
||||
// 访问的域名
|
||||
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
||||
_result, _err = dysmsapi20170525.NewClient(config)
|
||||
return _result, _err
|
||||
}
|
||||
|
||||
func TestSend(accessKeyId string, accessKeySecret string) (_err error) {
|
||||
// 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html
|
||||
client, _err := CreateClient(tea.String(accessKeyId), tea.String(accessKeySecret))
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
|
||||
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
||||
PhoneNumbers: tea.String("15303830571"),
|
||||
SignName: tea.String("布帆云"),
|
||||
TemplateCode: tea.String("SMS_198921686"),
|
||||
TemplateParam: tea.String("{\"code\":\"1234\"}"),
|
||||
}
|
||||
runtime := &util.RuntimeOptions{}
|
||||
tryErr := func() (_e error) {
|
||||
defer func() {
|
||||
if r := tea.Recover(recover()); r != nil {
|
||||
_e = r
|
||||
}
|
||||
}()
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
_, _err = client.SendSmsWithOptions(sendSmsRequest, runtime)
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
|
||||
return nil
|
||||
}()
|
||||
|
||||
if tryErr != nil {
|
||||
var err = &tea.SDKError{}
|
||||
if _t, ok := tryErr.(*tea.SDKError); ok {
|
||||
err = _t
|
||||
} else {
|
||||
err.Message = tea.String(tryErr.Error())
|
||||
}
|
||||
// 如有需要,请打印 error
|
||||
_, _err = util.AssertAsString(err.Message)
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
}
|
||||
return _err
|
||||
}
|
||||
22
server/internal/library/sms/config.go
Normal file
22
server/internal/library/sms/config.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/internal/model"
|
||||
)
|
||||
|
||||
var config *model.SmsConfig
|
||||
|
||||
func SetConfig(c *model.SmsConfig) {
|
||||
config = c
|
||||
}
|
||||
|
||||
func GetConfig() *model.SmsConfig {
|
||||
return config
|
||||
}
|
||||
|
||||
func GetModel(ctx context.Context) *gdb.Model {
|
||||
return g.Model("sys_sms_log").Ctx(ctx)
|
||||
}
|
||||
@@ -4,15 +4,12 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/sms/aliyun"
|
||||
"hotgo/internal/library/sms/tencent"
|
||||
"hotgo/internal/model"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
)
|
||||
|
||||
// Drive 短信驱动
|
||||
type Drive interface {
|
||||
SendCode(ctx context.Context, in sysin.SendCodeInp, config *model.SmsConfig) (err error)
|
||||
SendCode(ctx context.Context, in *sysin.SendCodeInp) (err error)
|
||||
}
|
||||
|
||||
func New(name ...string) Drive {
|
||||
@@ -27,9 +24,9 @@ func New(name ...string) Drive {
|
||||
|
||||
switch instanceName {
|
||||
case consts.SmsDriveAliYun:
|
||||
drive = &aliyun.Handle
|
||||
drive = &AliYunDrive{}
|
||||
case consts.SmsDriveTencent:
|
||||
drive = &tencent.Handle
|
||||
drive = &TencentDrive{}
|
||||
default:
|
||||
panic(fmt.Sprintf("暂不支持短信驱动:%v", instanceName))
|
||||
}
|
||||
|
||||
60
server/internal/library/sms/sms_aliyun.go
Normal file
60
server/internal/library/sms/sms_aliyun.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
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"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
)
|
||||
|
||||
type AliYunDrive struct{}
|
||||
|
||||
// SendCode 发送验证码
|
||||
func (d *AliYunDrive) SendCode(ctx context.Context, in *sysin.SendCodeInp) (err error) {
|
||||
client, err := d.CreateClient(tea.String(config.AliYunAccessKeyID), tea.String(config.AliYunAccessKeySecret))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
||||
PhoneNumbers: tea.String(in.Mobile),
|
||||
SignName: tea.String(config.AliYunSign),
|
||||
TemplateCode: tea.String(in.Template),
|
||||
TemplateParam: tea.String(fmt.Sprintf("{\"code\":\"%v\"}", in.Code)),
|
||||
}
|
||||
|
||||
return func() (_e error) {
|
||||
defer func() {
|
||||
if r := tea.Recover(recover()); r != nil {
|
||||
_e = r
|
||||
}
|
||||
}()
|
||||
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
response, err := client.SendSmsWithOptions(sendSmsRequest, &util.RuntimeOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.Log().Debugf(ctx, "aliyun.sendCode response:%+v", response.GoString())
|
||||
|
||||
return nil
|
||||
}()
|
||||
}
|
||||
|
||||
// CreateClient 使用AK&SK初始化账号Client
|
||||
func (d *AliYunDrive) CreateClient(accessKeyId *string, accessKeySecret *string) (result *dysmsapi20170525.Client, err error) {
|
||||
conf := &openapi.Config{
|
||||
// 必填,您的 AccessKey ID
|
||||
AccessKeyId: accessKeyId,
|
||||
// 必填,您的 AccessKey Secret
|
||||
AccessKeySecret: accessKeySecret,
|
||||
}
|
||||
// 访问的域名
|
||||
conf.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
||||
return dysmsapi20170525.NewClient(conf)
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
package tencent
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/internal/model"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
@@ -14,14 +13,10 @@ import (
|
||||
sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111" // 引入sms
|
||||
)
|
||||
|
||||
var (
|
||||
Handle = tencent{}
|
||||
)
|
||||
|
||||
type tencent struct{}
|
||||
type TencentDrive struct{}
|
||||
|
||||
// SendCode 发送验证码
|
||||
func (d *tencent) SendCode(ctx context.Context, in sysin.SendCodeInp, config *model.SmsConfig) (err error) {
|
||||
func (d *TencentDrive) SendCode(ctx context.Context, in *sysin.SendCodeInp) (err error) {
|
||||
/* 必要步骤:
|
||||
* 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
|
||||
* 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
|
||||
Reference in New Issue
Block a user