136 lines
3.8 KiB
Go
136 lines
3.8 KiB
Go
package services
|
||
|
||
import (
|
||
"fmt"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"server/models"
|
||
)
|
||
|
||
// ListSystemEmails 返回从 yz_platform_normal_setting 组装的邮箱配置(切片,通常仅一条)
|
||
func ListSystemEmails() ([]models.SystemEmail, error) {
|
||
enabledStr := models.GetPlatformSettingValue("email_enabled", "0")
|
||
fromAddress := models.GetPlatformSettingValue("email_from_address", "")
|
||
fromName := models.GetPlatformSettingValue("email_from_name", "")
|
||
host := models.GetPlatformSettingValue("email_host", "")
|
||
portStr := models.GetPlatformSettingValue("email_port", "465")
|
||
password := models.GetPlatformSettingValue("email_password", "")
|
||
encryption := models.GetPlatformSettingValue("email_encryption", "ssl")
|
||
timeoutStr := models.GetPlatformSettingValue("email_timeout", "30")
|
||
|
||
status := int8(0)
|
||
if enabledStr == "1" {
|
||
status = 1
|
||
}
|
||
portVal, _ := strconv.ParseUint(portStr, 10, 32)
|
||
timeoutVal, _ := strconv.ParseUint(timeoutStr, 10, 32)
|
||
|
||
row := models.SystemEmail{
|
||
ID: 1,
|
||
FromAddress: fromAddress,
|
||
Host: host,
|
||
Port: uint(portVal),
|
||
Password: password,
|
||
Encryption: encryption,
|
||
Timeout: uint(timeoutVal),
|
||
Status: status,
|
||
CreateTime: time.Now(),
|
||
UpdateTime: time.Now(),
|
||
}
|
||
if fromName != "" {
|
||
row.FromName = &fromName
|
||
}
|
||
|
||
return []models.SystemEmail{row}, nil
|
||
}
|
||
|
||
// UpsertFirstSystemEmail 将邮箱配置保存到 yz_platform_normal_setting 表中
|
||
func UpsertFirstSystemEmail(fromAddress string, fromName *string, host string, port uint, password string, encryption string, timeout uint, status int8, remark *string) error {
|
||
if encryption == "" {
|
||
encryption = "ssl"
|
||
}
|
||
if port == 0 {
|
||
port = 465
|
||
}
|
||
if timeout == 0 {
|
||
timeout = 30
|
||
}
|
||
fromAddress = strings.TrimSpace(fromAddress)
|
||
host = strings.TrimSpace(host)
|
||
|
||
fn := ""
|
||
if fromName != nil {
|
||
fn = *fromName
|
||
}
|
||
|
||
statusStr := "0"
|
||
if status == 1 {
|
||
statusStr = "1"
|
||
}
|
||
|
||
settings := []struct {
|
||
code string
|
||
name string
|
||
value string
|
||
remark string
|
||
}{
|
||
{"email_enabled", "邮件服务启用状态", statusStr, "0为关闭,1为开启"},
|
||
{"email_from_address", "发件人邮箱", fromAddress, ""},
|
||
{"email_from_name", "发件人名称", fn, ""},
|
||
{"email_host", "SMTP 服务器地址", host, ""},
|
||
{"email_port", "SMTP 端口", strconv.FormatUint(uint64(port), 10), ""},
|
||
{"email_encryption", "邮件加密方式", encryption, "支持 ssl/tls/none"},
|
||
{"email_timeout", "邮件发送超时时间", strconv.FormatUint(uint64(timeout), 10), ""},
|
||
}
|
||
|
||
// 如果传入了新密码,或者目前还没有保存过密码,才更新密码
|
||
if strings.TrimSpace(password) != "" {
|
||
settings = append(settings, struct {
|
||
code string
|
||
name string
|
||
value string
|
||
remark string
|
||
}{"email_password", "邮件授权码/密码", strings.TrimSpace(password), ""})
|
||
} else {
|
||
// 校验:如果完全没有配置过密码,必须填写密码
|
||
existingPass := models.GetPlatformSettingValue("email_password", "")
|
||
if existingPass == "" {
|
||
return fmt.Errorf("首次保存必须填写授权码/密码")
|
||
}
|
||
}
|
||
|
||
for _, item := range settings {
|
||
var setting models.PlatformNormalSetting
|
||
err := models.Orm.QueryTable(new(models.PlatformNormalSetting)).
|
||
Filter("code", item.code).
|
||
Filter("delete_time__isnull", true).
|
||
One(&setting)
|
||
if err == nil {
|
||
setting.Value = item.value
|
||
setting.Name = item.name
|
||
setting.Remark = item.remark
|
||
now := time.Now()
|
||
setting.UpdateTime = &now
|
||
_, err = models.Orm.Update(&setting, "Value", "Name", "Remark", "UpdateTime")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
} else {
|
||
newSetting := models.PlatformNormalSetting{
|
||
Name: item.name,
|
||
Code: item.code,
|
||
Value: item.value,
|
||
Remark: item.remark,
|
||
CreateTime: time.Now(),
|
||
}
|
||
_, err = models.Orm.Insert(&newSetting)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
return nil
|
||
}
|