im 管理后台
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
// Package sms
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
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 dao.SysSmsLog.Ctx(ctx)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Package sms
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
)
|
||||
|
||||
// Drive 短信驱动
|
||||
type Drive interface {
|
||||
SendCode(ctx context.Context, in *sysin.SendCodeInp) (err error)
|
||||
}
|
||||
|
||||
func New(name ...string) Drive {
|
||||
var (
|
||||
instanceName = consts.SmsDriveAliYun
|
||||
drive Drive
|
||||
)
|
||||
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
instanceName = name[0]
|
||||
}
|
||||
|
||||
switch instanceName {
|
||||
case consts.SmsDriveAliYun:
|
||||
drive = &AliYunDrive{}
|
||||
case consts.SmsDriveTencent:
|
||||
drive = &TencentDrive{}
|
||||
default:
|
||||
panic(fmt.Sprintf("暂不支持短信驱动:%v", instanceName))
|
||||
}
|
||||
return drive
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// Package sms
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
// Package sms
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||
sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111" // 引入sms
|
||||
)
|
||||
|
||||
type TencentDrive struct{}
|
||||
|
||||
// SendCode 发送验证码
|
||||
func (d *TencentDrive) SendCode(ctx context.Context, in *sysin.SendCodeInp) (err error) {
|
||||
/* 必要步骤:
|
||||
* 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
|
||||
* 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
|
||||
* 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
|
||||
* 以免泄露密钥对危及你的财产安全。
|
||||
* SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi */
|
||||
credential := common.NewCredential(
|
||||
config.TencentSecretId,
|
||||
config.TencentSecretKey,
|
||||
)
|
||||
|
||||
/* 非必要步骤:
|
||||
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
|
||||
cpf := profile.NewClientProfile()
|
||||
|
||||
/* SDK默认使用POST方法。
|
||||
* 如果你一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
|
||||
cpf.HttpProfile.ReqMethod = "POST"
|
||||
|
||||
/* SDK有默认的超时时间,非必要请不要进行调整
|
||||
* 如有需要请在代码中查阅以获取最新的默认值 */
|
||||
// cpf.HttpProfile.ReqTimeout = 5
|
||||
|
||||
/* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com */
|
||||
cpf.HttpProfile.Endpoint = config.TencentEndpoint
|
||||
|
||||
/* SDK默认用TC3-HMAC-SHA256进行签名,非必要请不要修改这个字段 */
|
||||
cpf.SignMethod = "HmacSHA1"
|
||||
|
||||
/* 实例化要请求产品(以sms为例)的client对象
|
||||
* 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8 */
|
||||
client, err := sms.NewClient(credential, config.TencentRegion, cpf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
/* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
|
||||
* 你可以直接查询SDK源码确定接口有哪些属性可以设置
|
||||
* 属性可能是基本类型,也可能引用了另一个数据结构
|
||||
* 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
|
||||
request := sms.NewSendSmsRequest()
|
||||
|
||||
/* 基本类型的设置:
|
||||
* SDK采用的是指针风格指定参数,即使对于基本类型你也需要用指针来对参数赋值。
|
||||
* SDK提供对基本类型的指针引用封装函数
|
||||
* 帮助链接:
|
||||
* 短信控制台: https://console.cloud.tencent.com/smsv2
|
||||
* 腾讯云短信小助手: https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81 */
|
||||
|
||||
/* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
|
||||
// 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
|
||||
request.SmsSdkAppId = common.StringPtr(config.TencentAppId)
|
||||
|
||||
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
|
||||
// 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
|
||||
request.SignName = common.StringPtr(config.TencentSign)
|
||||
|
||||
/* 模板 ID: 必须填写已审核通过的模板 ID */
|
||||
// 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
|
||||
request.TemplateId = common.StringPtr(in.Template)
|
||||
|
||||
/* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空*/
|
||||
request.TemplateParamSet = common.StringPtrs([]string{in.Code})
|
||||
|
||||
/* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
|
||||
* 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
|
||||
request.PhoneNumberSet = common.StringPtrs([]string{fmt.Sprintf("+86%v", in.Mobile)})
|
||||
|
||||
/* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
|
||||
// request.SessionContext = common.StringPtr("")
|
||||
|
||||
/* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
|
||||
// request.ExtendCode = common.StringPtr("")
|
||||
|
||||
/* 国际/港澳台短信 SenderId(无需要可忽略): 国内短信填空,默认未开通,如需开通请联系 [腾讯云短信小助手] */
|
||||
// request.SenderId = common.StringPtr("")
|
||||
|
||||
// 通过client对象调用想要访问的接口,需要传入请求对象
|
||||
response, err := client.SendSms(request)
|
||||
// 处理异常
|
||||
if _, ok := err.(*errors.TencentCloudSDKError); ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 非SDK异常,直接失败。实际代码中可以加入其他的处理。
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
b, err := json.Marshal(response.Response)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 打印返回的json字符串
|
||||
g.Log().Debugf(ctx, "tencent.sendCode response:%+v", string(b))
|
||||
|
||||
/* 当出现以下错误码时,快速解决方案参考
|
||||
* [FailedOperation.SignatureIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
|
||||
* [FailedOperation.TemplateIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.templateincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
|
||||
* [UnauthorizedOperation.SmsSdkAppIdVerifyFail](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunauthorizedoperation.smssdkappidverifyfail-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
|
||||
* [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunsupportedoperation.containdomesticandinternationalphonenumber-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
|
||||
* 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
|
||||
*/
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user