78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"server/models"
|
|
)
|
|
|
|
// ListSystemEmails 返回全部邮箱配置(按 id 升序,通常仅一条)
|
|
func ListSystemEmails() ([]models.SystemEmail, error) {
|
|
var rows []models.SystemEmail
|
|
_, err := models.Orm.QueryTable(new(models.SystemEmail)).OrderBy("id").All(&rows)
|
|
return rows, err
|
|
}
|
|
|
|
// UpsertFirstSystemEmail 若已有记录则更新第一条,否则插入
|
|
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
|
|
}
|
|
if status == 0 {
|
|
status = 1
|
|
}
|
|
fromAddress = strings.TrimSpace(fromAddress)
|
|
host = strings.TrimSpace(host)
|
|
|
|
cnt, err := models.Orm.QueryTable(new(models.SystemEmail)).Count()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if cnt == 0 {
|
|
if strings.TrimSpace(password) == "" {
|
|
return fmt.Errorf("首次保存必须填写授权码/密码")
|
|
}
|
|
row := &models.SystemEmail{
|
|
FromAddress: fromAddress,
|
|
FromName: fromName,
|
|
Host: host,
|
|
Port: port,
|
|
Password: strings.TrimSpace(password),
|
|
Encryption: encryption,
|
|
Timeout: timeout,
|
|
Status: status,
|
|
Remark: remark,
|
|
}
|
|
_, err = models.Orm.Insert(row)
|
|
return err
|
|
}
|
|
|
|
var first models.SystemEmail
|
|
if err := models.Orm.QueryTable(new(models.SystemEmail)).OrderBy("id").Limit(1).One(&first); err != nil {
|
|
return err
|
|
}
|
|
|
|
up := map[string]interface{}{
|
|
"from_address": fromAddress,
|
|
"from_name": fromName,
|
|
"host": host,
|
|
"port": port,
|
|
"encryption": encryption,
|
|
"timeout": timeout,
|
|
"status": status,
|
|
"remark": remark,
|
|
}
|
|
if strings.TrimSpace(password) != "" {
|
|
up["password"] = strings.TrimSpace(password)
|
|
}
|
|
_, err = models.Orm.QueryTable(new(models.SystemEmail)).Filter("id", first.ID).Update(up)
|
|
return err
|
|
}
|