Initial commit (code only without large binaries)
This commit is contained in:
172
EdgeAdmin/internal/web/actions/default/users/setting/smsTest.go
Normal file
172
EdgeAdmin/internal/web/actions/default/users/setting/smsTest.go
Normal file
@@ -0,0 +1,172 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build plus
|
||||
|
||||
package setting
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type SmsTestAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *SmsTestAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *SmsTestAction) RunGet(params struct{}) {
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *SmsTestAction) RunPost(params struct {
|
||||
ConfigJSON []byte
|
||||
ToMobile string
|
||||
Body string
|
||||
Code string
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
if len(params.ConfigJSON) == 0 {
|
||||
this.Fail("读取不到短信配置")
|
||||
return
|
||||
}
|
||||
|
||||
var config = userconfigs.NewSMSSenderConfig()
|
||||
err := json.Unmarshal(params.ConfigJSON, config)
|
||||
if err != nil {
|
||||
this.Fail("解析短信配置出错:" + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var paramsJSON []byte
|
||||
switch config.Type {
|
||||
case userconfigs.SMSSenderWebHook:
|
||||
if config.WebHookParams == nil {
|
||||
this.Fail("请配置HTTP接口相关参数")
|
||||
return
|
||||
}
|
||||
if len(config.WebHookParams.URL) == 0 {
|
||||
this.Fail("请输入短信HTTP接口URL地址")
|
||||
return
|
||||
}
|
||||
if !regexp.MustCompile(`^(http|https)://`).MatchString(config.WebHookParams.URL) {
|
||||
this.Fail("短信WebHook URL地址必须以http://或https://开头")
|
||||
return
|
||||
}
|
||||
|
||||
paramsJSON, err = json.Marshal(config.WebHookParams)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
case userconfigs.SMSSenderAliyunSMS:
|
||||
if config.AliyunSMSParams == nil {
|
||||
this.Fail("请配置阿里云短信相关参数")
|
||||
return
|
||||
}
|
||||
if len(config.AliyunSMSParams.Sign) == 0 {
|
||||
this.Fail("请输入签名名称")
|
||||
return
|
||||
}
|
||||
if len(config.AliyunSMSParams.TemplateCode) == 0 {
|
||||
this.Fail("请输入模板CODE")
|
||||
return
|
||||
}
|
||||
if len(config.AliyunSMSParams.CodeVarName) == 0 {
|
||||
this.Fail("请输入模板中验证码变量名称")
|
||||
return
|
||||
}
|
||||
if len(config.AliyunSMSParams.AccessKeyId) == 0 {
|
||||
this.Fail("请输入AccessKey ID")
|
||||
return
|
||||
}
|
||||
if len(config.AliyunSMSParams.AccessKeySecret) == 0 {
|
||||
this.Fail("请输入AccessKey Secret")
|
||||
return
|
||||
}
|
||||
|
||||
paramsJSON, err = json.Marshal(config.AliyunSMSParams)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
case userconfigs.SMSSenderTencentSMS:
|
||||
if config.TencentSMSParams == nil {
|
||||
this.Fail("请配置腾讯云短信相关参数")
|
||||
return
|
||||
}
|
||||
|
||||
var digitsReg = regexp.MustCompile(`^\d+$`)
|
||||
|
||||
if len(config.TencentSMSParams.SDKAppId) == 0 {
|
||||
this.Fail("请输入SDK应用ID")
|
||||
return
|
||||
}
|
||||
if !digitsReg.MatchString(config.TencentSMSParams.SDKAppId) {
|
||||
this.Fail("SDK应用ID是一组数字,请重新输入")
|
||||
return
|
||||
}
|
||||
if len(config.TencentSMSParams.Sign) == 0 {
|
||||
this.Fail("请输入签名内容")
|
||||
return
|
||||
}
|
||||
if len(config.TencentSMSParams.TemplateId) == 0 {
|
||||
this.Fail("请输入模板ID")
|
||||
return
|
||||
}
|
||||
if !digitsReg.MatchString(config.TencentSMSParams.TemplateId) {
|
||||
this.Fail("正文模板ID是一组数字,请重新输入")
|
||||
return
|
||||
}
|
||||
if len(config.TencentSMSParams.AccessKeyId) == 0 {
|
||||
this.Fail("请输入密钥SecretId")
|
||||
return
|
||||
}
|
||||
if len(config.TencentSMSParams.AccessKeySecret) == 0 {
|
||||
this.Fail("请输入密钥SecretKey")
|
||||
return
|
||||
}
|
||||
|
||||
paramsJSON, err = json.Marshal(config.TencentSMSParams)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
default:
|
||||
this.Fail("不支持渠道'" + config.Type + "'")
|
||||
}
|
||||
|
||||
params.Must.
|
||||
Field("toMobile", params.ToMobile).
|
||||
Require("请输入收信人手机号").
|
||||
Mobile("请输入正确的收信人手机号").
|
||||
Field("body", params.Body).
|
||||
Require("请输入测试内容")
|
||||
|
||||
// 发送测试
|
||||
resp, err := this.RPC().SMSSenderRPC().SendSMS(this.AdminContext(), &pb.SendSMSRequest{
|
||||
Mobile: params.ToMobile,
|
||||
Body: params.Body,
|
||||
Code: params.Code,
|
||||
Type: config.Type,
|
||||
ParamsJSON: paramsJSON,
|
||||
})
|
||||
if err != nil {
|
||||
this.Fail("发送失败:" + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if !resp.IsOk {
|
||||
this.Fail("发送失败,结果:" + resp.Result)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user