Initial commit (code only without large binaries)

This commit is contained in:
robin
2026-02-15 18:58:44 +08:00
commit 35df75498f
9442 changed files with 1495866 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build plus
package setting
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/iwind/TeaGo/actions"
)
type EmailAction struct {
actionutils.ParentAction
}
func (this *EmailAction) Init() {
this.Nav("", "", "email")
}
func (this *EmailAction) RunGet(params struct{}) {
config, err := configloaders.LoadUserSenderConfig()
if err != nil {
this.ErrorPage(err)
return
}
if config == nil {
this.ErrorPage(errors.New("'config' should not be nil"))
return
}
// 验证邮件
if config.VerifyEmail == nil {
config.VerifyEmail = userconfigs.NewEmailSenderConfig()
}
// 通知邮件
if config.NotifyEmail == nil {
config.NotifyEmail = userconfigs.NewEmailSenderConfig()
}
this.Data["config"] = config
this.Show()
}
func (this *EmailAction) RunPost(params struct {
VerifyEmailJSON []byte
NotifyEmailJSON []byte
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo(codes.User_LogUpdateUserEmailSettings)
config, err := configloaders.LoadUserSenderConfig()
if err != nil {
this.ErrorPage(err)
return
}
var verifyEmailConfig = userconfigs.NewEmailSenderConfig()
err = json.Unmarshal(params.VerifyEmailJSON, verifyEmailConfig)
if err != nil {
this.ErrorPage(err)
return
}
config.VerifyEmail = verifyEmailConfig
if verifyEmailConfig.IsOn {
params.Must.
Field("verifyEmailJSONSmtpHost", verifyEmailConfig.SMTPHost).
Require("请输入SMTP地址").
Field("verifyEmailJSONSmtpPort", verifyEmailConfig.SMTPPort).
Gt(0, "请输入正确的SMTP端口").
Field("verifyEmailJSONUsername", verifyEmailConfig.Username).
Require("请输入用户名").
Field("verifyEmailJSONPassword", verifyEmailConfig.Password).
Require("请输入密码").
Field("verifyEmailJSONFromEmail", verifyEmailConfig.FromEmail).
Require("请输入发件人Email").
Email("发件人Email格式不正确")
}
var notifyEmailConfig = userconfigs.NewEmailSenderConfig()
err = json.Unmarshal(params.NotifyEmailJSON, notifyEmailConfig)
if err != nil {
this.ErrorPage(err)
return
}
config.NotifyEmail = notifyEmailConfig
if notifyEmailConfig.IsOn {
params.Must.
Field("notifyEmailJSONSmtpHost", notifyEmailConfig.SMTPHost).
Require("请输入SMTP地址").
Field("notifyEmailJSONSmtpPort", notifyEmailConfig.SMTPPort).
Gt(0, "请输入正确的SMTP端口").
Field("notifyEmailJSONUsername", notifyEmailConfig.Username).
Require("请输入用户名").
Field("notifyEmailJSONPassword", notifyEmailConfig.Password).
Require("请输入密码").
Field("notifyEmailJSONFromEmail", notifyEmailConfig.FromEmail).
Require("请输入发件人Email").
Email("发件人Email格式不正确")
}
configJSON, err := json.Marshal(config)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: systemconfigs.SettingCodeUserSenderConfig,
ValueJSON: configJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,101 @@
// Copyright 2022 Liuxiangchao iwind.liu@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"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
type EmailTestAction struct {
actionutils.ParentAction
}
func (this *EmailTestAction) Init() {
this.Nav("", "", "")
}
func (this *EmailTestAction) RunGet(params struct{}) {
this.Show()
}
func (this *EmailTestAction) RunPost(params struct {
ConfigJSON []byte
ToEmail string
Subject string
Body string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
if len(params.ConfigJSON) == 0 {
this.Fail("读取不到邮件配置")
return
}
var config = userconfigs.NewEmailSenderConfig()
err := json.Unmarshal(params.ConfigJSON, config)
if err != nil {
this.Fail("解析邮件配置出错:" + err.Error())
return
}
if len(config.SMTPHost) == 0 {
this.Fail("缺少SMTP地址")
return
}
if config.SMTPPort <= 0 {
this.Fail("缺少SMTP端口")
return
}
if len(config.Username) == 0 {
this.Fail("缺少用户名")
return
}
if len(config.Password) == 0 {
this.Fail("缺少密码")
return
}
if len(config.FromEmail) == 0 {
this.Fail("缺少发件人Email")
return
}
params.Must.Field("fromEmail", config.FromEmail).
Email("发件人Email格式不正确")
params.Must.
Field("toEmail", params.ToEmail).
Require("请输入收件人Email").
Email("请输入正确的收件人Email").
Field("subject", params.Subject).
Require("请输入测试标题").
Field("body", params.Body).
Require("请输入测试内容")
// 发送测试
_, err = this.RPC().MessageMediaRPC().SendMediaMessage(this.AdminContext(), &pb.SendMediaMessageRequest{
MediaType: "email",
OptionsJSON: maps.Map{
"smtp": config.SMTPHost + ":" + types.String(config.SMTPPort),
"username": config.Username,
"password": config.Password,
"from": config.FromEmail,
"fromName": config.FromName,
}.AsJSON(),
User: params.ToEmail,
Subject: params.Subject,
Body: params.Body,
})
if err != nil {
this.Fail("发送失败:" + err.Error())
return
}
this.Success()
}

View File

@@ -0,0 +1,172 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
//go:build plus
package setting
import (
"encoding/json"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
"github.com/TeaOSLab/EdgeAdmin/internal/plus"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "index")
}
func (this *IndexAction) RunGet(params struct{}) {
if !teaconst.IsPlus {
this.RedirectURL("/users")
return
}
// 当前配置
resp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeUserRegisterConfig})
if err != nil {
this.ErrorPage(err)
return
}
var config = userconfigs.DefaultUserRegisterConfig()
if len(resp.ValueJSON) > 0 {
err = json.Unmarshal(resp.ValueJSON, config)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["config"] = config
// 功能列表
var featureMaps = []maps.Map{}
for _, feature := range userconfigs.FindAllUserFeatures() {
featureMaps = append(featureMaps, maps.Map{
"name": feature.Name,
"description": feature.Description,
"code": feature.Code,
"isChecked": lists.ContainsString(config.Features, feature.Code),
})
}
this.Data["features"] = featureMaps
// 当前高防
this.Data["adIsVisible"] = plus.AllowComponent(plus.ComponentCodeAntiDDoS)
// 当前默认的智能DNS设置
this.Data["nsIsVisible"] = plus.AllowComponent(plus.ComponentCodeNS)
this.Show()
}
func (this *IndexAction) RunPost(params struct {
IsOn bool
ComplexPassword bool
RequireVerification bool
RequireIdentity bool
CheckClientRegion bool
EmailVerificationIsOn bool
EmailVerificationShowNotice bool
EmailVerificationCanLogin bool
EmailVerificationSubject string
EmailVerificationBody string
EmailResetPasswordIsOn bool
EmailResetPasswordSubject string
EmailResetPasswordBody string
MobileVerificationIsOn bool
MobileVerificationShowNotice bool
MobileVerificationCanLogin bool
MobileVerificationForce bool
MobileVerificationBody string
CdnIsOn bool
ClusterId int64
Features []string
FeatureOp string
AdIsOn bool
NsIsOn bool
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo(codes.User_LogUpdateUserGlobalSettings)
params.Must.
Field("clusterId", params.ClusterId).
Gt(0, "请选择一个集群")
var config = userconfigs.DefaultUserRegisterConfig()
config.IsOn = params.IsOn
config.ComplexPassword = params.ComplexPassword
config.RequireVerification = params.RequireVerification
config.RequireIdentity = params.RequireIdentity
config.CheckClientRegion = params.CheckClientRegion
config.EmailVerification.IsOn = params.EmailVerificationIsOn
config.EmailVerification.CanLogin = params.EmailVerificationCanLogin
config.EmailVerification.Subject = params.EmailVerificationSubject
config.EmailVerification.Body = params.EmailVerificationBody
config.EmailVerification.ShowNotice = params.EmailVerificationShowNotice
config.MobileVerification.IsOn = params.MobileVerificationIsOn
config.MobileVerification.CanLogin = params.MobileVerificationCanLogin
config.MobileVerification.Force = params.MobileVerificationForce
config.MobileVerification.Body = params.MobileVerificationBody
config.MobileVerification.ShowNotice = params.MobileVerificationShowNotice
config.EmailResetPassword.IsOn = params.EmailResetPasswordIsOn
config.EmailResetPassword.Subject = params.EmailResetPasswordSubject
config.EmailResetPassword.Body = params.EmailResetPasswordBody
config.CDNIsOn = params.CdnIsOn
config.ClusterId = params.ClusterId
config.Features = params.Features
config.ADIsOn = params.AdIsOn
config.NSIsOn = params.NsIsOn
configJSON, err := json.Marshal(config)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: systemconfigs.SettingCodeUserRegisterConfig,
ValueJSON: configJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
if params.FeatureOp != "keep" {
_, err = this.RPC().UserRPC().UpdateAllUsersFeatures(this.AdminContext(), &pb.UpdateAllUsersFeaturesRequest{
FeatureCodes: params.Features,
Overwrite: params.FeatureOp == "overwrite",
})
if err != nil {
this.ErrorPage(err)
return
}
}
this.Success()
}

View File

@@ -0,0 +1,110 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
//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/systemconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type ServerAction struct {
actionutils.ParentAction
}
func (this *ServerAction) Init() {
this.Nav("", "", "server")
}
func (this *ServerAction) RunGet(params struct{}) {
// 当前配置
resp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeUserServerConfig})
if err != nil {
this.ErrorPage(err)
return
}
var config = userconfigs.DefaultUserServerConfig()
if len(resp.ValueJSON) > 0 {
err = json.Unmarshal(resp.ValueJSON, config)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["config"] = config
// 分组
groupsResp, err := this.RPC().ServerGroupRPC().FindAllEnabledServerGroups(this.AdminContext(), &pb.FindAllEnabledServerGroupsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var groupMaps = []maps.Map{}
for _, group := range groupsResp.ServerGroups {
groupMaps = append(groupMaps, maps.Map{
"id": group.Id,
"name": group.Name,
})
}
this.Data["groups"] = groupMaps
// 缓存
this.Data["defaultMaxCacheKeysPerTask"] = userconfigs.MaxCacheKeysPerTask
this.Data["defaultMaxCacheKeysPerDay"] = userconfigs.MaxCacheKeysPerDay
this.Show()
}
func (this *ServerAction) RunPost(params struct {
GroupId int64
RequirePlan bool
EnableStat bool
// 缓存相关
PurgeMaxKeysPerTask int32
PurgeMaxKeysPerDay int32
FetchMaxKeysPerTask int32
FetchMaxKeysPerDay int32
Must *actions.Must
CSRF *actionutils.CSRF
}) {
var config = userconfigs.DefaultUserServerConfig()
config.GroupId = params.GroupId
config.RequirePlan = params.RequirePlan
config.EnableStat = params.EnableStat
if config.HTTPCacheTaskPurgeConfig != nil {
config.HTTPCacheTaskPurgeConfig.MaxKeysPerTask = params.PurgeMaxKeysPerTask
config.HTTPCacheTaskPurgeConfig.MaxKeysPerDay = params.PurgeMaxKeysPerDay
}
if config.HTTPCacheTaskFetchConfig != nil {
config.HTTPCacheTaskFetchConfig.MaxKeysPerTask = params.FetchMaxKeysPerTask
config.HTTPCacheTaskFetchConfig.MaxKeysPerDay = params.FetchMaxKeysPerDay
}
configJSON, err := json.Marshal(config)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: systemconfigs.SettingCodeUserServerConfig,
ValueJSON: configJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,259 @@
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build plus
package setting
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/iwind/TeaGo/actions"
"regexp"
)
type SmsAction struct {
actionutils.ParentAction
}
func (this *SmsAction) Init() {
this.Nav("", "", "sms")
}
func (this *SmsAction) RunGet(params struct{}) {
config, err := configloaders.LoadUserSenderConfig()
if err != nil {
this.ErrorPage(err)
return
}
if config == nil {
this.ErrorPage(errors.New("'config' should not be nil"))
return
}
// 验证短信
if config.VerifySMS == nil {
config.VerifySMS = userconfigs.NewSMSSenderConfig()
}
// 通知短信
if config.NotifySMS == nil {
config.NotifySMS = userconfigs.NewSMSSenderConfig()
}
this.Data["config"] = config
this.Show()
}
func (this *SmsAction) RunPost(params struct {
VerifySMSJSON []byte
NotifySMSJSON []byte
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo(codes.User_LogUpdateUserSmsSettings)
config, err := configloaders.LoadUserSenderConfig()
if err != nil {
this.ErrorPage(err)
return
}
var verifySMSConfig = userconfigs.NewSMSSenderConfig()
err = json.Unmarshal(params.VerifySMSJSON, verifySMSConfig)
if err != nil {
this.ErrorPage(err)
return
}
config.VerifySMS = verifySMSConfig
if verifySMSConfig.IsOn {
switch verifySMSConfig.Type {
case userconfigs.SMSSenderWebHook:
if verifySMSConfig.WebHookParams == nil {
this.Fail("请配置HTTP接口相关参数")
return
}
if len(verifySMSConfig.WebHookParams.URL) == 0 {
this.Fail("请输入激活短信HTTP接口URL地址")
return
}
if !regexp.MustCompile(`^(http|https)://`).MatchString(verifySMSConfig.WebHookParams.URL) {
this.Fail("激活短信HTTP接口URL地址必须以http://或https://开头")
return
}
case userconfigs.SMSSenderAliyunSMS:
if verifySMSConfig.AliyunSMSParams == nil {
this.Fail("请配置阿里云短信相关参数")
return
}
if len(verifySMSConfig.AliyunSMSParams.Sign) == 0 {
this.Fail("请输入签名名称")
return
}
if len(verifySMSConfig.AliyunSMSParams.TemplateCode) == 0 {
this.Fail("请输入模板CODE")
return
}
if len(verifySMSConfig.AliyunSMSParams.CodeVarName) == 0 {
this.Fail("请输入模板中验证码变量名称")
return
}
if len(verifySMSConfig.AliyunSMSParams.AccessKeyId) == 0 {
this.Fail("请输入AccessKey ID")
return
}
if len(verifySMSConfig.AliyunSMSParams.AccessKeySecret) == 0 {
this.Fail("请输入AccessKey Secret")
return
}
case userconfigs.SMSSenderTencentSMS:
if verifySMSConfig.TencentSMSParams == nil {
this.Fail("请配置腾讯云短信相关参数")
return
}
var digitsReg = regexp.MustCompile(`^\d+$`)
if len(verifySMSConfig.TencentSMSParams.SDKAppId) == 0 {
this.Fail("请输入SDK应用ID")
return
}
if !digitsReg.MatchString(verifySMSConfig.TencentSMSParams.SDKAppId) {
this.Fail("SDK应用ID是一组数字请重新输入")
return
}
if len(verifySMSConfig.TencentSMSParams.Sign) == 0 {
this.Fail("请输入签名内容")
return
}
if len(verifySMSConfig.TencentSMSParams.TemplateId) == 0 {
this.Fail("请输入模板ID")
return
}
if !digitsReg.MatchString(verifySMSConfig.TencentSMSParams.TemplateId) {
this.Fail("正文模板ID是一组数字请重新输入")
return
}
if len(verifySMSConfig.TencentSMSParams.AccessKeyId) == 0 {
this.Fail("请输入密钥SecretId")
return
}
if len(verifySMSConfig.TencentSMSParams.AccessKeySecret) == 0 {
this.Fail("请输入密钥SecretKey")
return
}
}
}
var notifySMSConfig = userconfigs.NewSMSSenderConfig()
err = json.Unmarshal(params.NotifySMSJSON, notifySMSConfig)
if err != nil {
this.ErrorPage(err)
return
}
config.NotifySMS = notifySMSConfig
if notifySMSConfig.IsOn {
switch notifySMSConfig.Type {
case userconfigs.SMSSenderWebHook:
if notifySMSConfig.WebHookParams == nil {
this.Fail("请配置HTTP接口相关参数")
return
}
if len(notifySMSConfig.WebHookParams.URL) == 0 {
this.Fail("请输入通知短信HTTP接口URL地址")
return
}
if !regexp.MustCompile(`^(http|https)://`).MatchString(notifySMSConfig.WebHookParams.URL) {
this.Fail("通知短信HTTP接口URL地址必须以http://或https://开头")
return
}
case userconfigs.SMSSenderAliyunSMS:
if notifySMSConfig.AliyunSMSParams == nil {
this.Fail("请配置阿里云短信相关参数")
return
}
if len(notifySMSConfig.AliyunSMSParams.Sign) == 0 {
this.Fail("请输入签名名称")
return
}
if len(notifySMSConfig.AliyunSMSParams.TemplateCode) == 0 {
this.Fail("请输入模板CODE")
return
}
if len(notifySMSConfig.AliyunSMSParams.CodeVarName) == 0 {
this.Fail("请输入模板中验证码变量名称")
return
}
if len(notifySMSConfig.AliyunSMSParams.AccessKeyId) == 0 {
this.Fail("请输入AccessKey ID")
return
}
if len(notifySMSConfig.AliyunSMSParams.AccessKeySecret) == 0 {
this.Fail("请输入AccessKey Secret")
return
}
case userconfigs.SMSSenderTencentSMS:
if notifySMSConfig.TencentSMSParams == nil {
this.Fail("请配置腾讯云短信相关参数")
return
}
var digitsReg = regexp.MustCompile(`^\d+$`)
if len(notifySMSConfig.TencentSMSParams.SDKAppId) == 0 {
this.Fail("请输入SDK应用ID")
return
}
if !digitsReg.MatchString(notifySMSConfig.TencentSMSParams.SDKAppId) {
this.Fail("SDK应用ID是一组数字请重新输入")
return
}
if len(notifySMSConfig.TencentSMSParams.Sign) == 0 {
this.Fail("请输入签名内容")
return
}
if len(notifySMSConfig.TencentSMSParams.TemplateId) == 0 {
this.Fail("请输入模板ID")
return
}
if !digitsReg.MatchString(notifySMSConfig.TencentSMSParams.TemplateId) {
this.Fail("正文模板ID是一组数字请重新输入")
return
}
if len(notifySMSConfig.TencentSMSParams.AccessKeyId) == 0 {
this.Fail("请输入密钥SecretId")
return
}
if len(notifySMSConfig.TencentSMSParams.AccessKeySecret) == 0 {
this.Fail("请输入密钥SecretKey")
return
}
}
}
configJSON, err := json.Marshal(config)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: systemconfigs.SettingCodeUserSenderConfig,
ValueJSON: configJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View 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()
}