增加bark配置
This commit is contained in:
@@ -66,6 +66,15 @@ func (c *ApiGetCardController) GetCard() {
|
||||
return
|
||||
}
|
||||
|
||||
// 读取机器码/MAC
|
||||
machineCode := strings.TrimSpace(c.GetString("machine_code"))
|
||||
if machineCode == "" {
|
||||
machineCode = strings.TrimSpace(c.GetString("machineCode"))
|
||||
}
|
||||
if machineCode == "" {
|
||||
machineCode = strings.TrimSpace(c.GetString("mac"))
|
||||
}
|
||||
|
||||
// 参数校验
|
||||
if platform == "" {
|
||||
c.cardErr(400, 400, "缺少参数 type(来源平台)")
|
||||
@@ -92,7 +101,7 @@ func (c *ApiGetCardController) GetCard() {
|
||||
|
||||
switch module {
|
||||
case "cursor":
|
||||
c.extractCursor(platform, dataType, startID, now)
|
||||
c.extractCursor(platform, dataType, startID, now, machineCode)
|
||||
case "windsurf":
|
||||
c.extractWindsurf(platform, dataType, startID, now)
|
||||
case "krio":
|
||||
@@ -121,8 +130,25 @@ func (c *ApiGetCardController) readOptionalStartID() (uint64, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (c *ApiGetCardController) extractCursor(platform, dataType string, startID uint64, now time.Time) {
|
||||
c.extractWithProbe("cursor", platform, dataType, now, func() (uint64, *string, *string, string, string, *int8, error) {
|
||||
func (c *ApiGetCardController) extractCursor(platform, dataType string, startID uint64, now time.Time, machineCode string) {
|
||||
// 优先查询该机器码是否已经绑定过未删除的卡密
|
||||
if machineCode != "" {
|
||||
var existing models.PlatformAccountPoolCursor
|
||||
err := models.Orm.QueryTable(new(models.PlatformAccountPoolCursor)).
|
||||
Filter("machine_code", machineCode).
|
||||
Filter("delete_time__isnull", true).
|
||||
Exclude("is_used", 0).
|
||||
OrderBy("-id").
|
||||
Limit(1).
|
||||
One(&existing)
|
||||
if err == nil {
|
||||
// 直接返回已绑定的卡密信息
|
||||
c.cardOK(buildCardResult(&existing.Account, &existing.Password, existing.Token, existing.DataType))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.extractWithProbe("cursor", platform, dataType, now, machineCode, func() (uint64, *string, *string, string, string, *int8, error) {
|
||||
var row models.PlatformAccountPoolCursor
|
||||
qs := models.Orm.QueryTable(new(models.PlatformAccountPoolCursor)).
|
||||
Filter("is_extracted", 0).
|
||||
@@ -141,7 +167,7 @@ func (c *ApiGetCardController) extractCursor(platform, dataType string, startID
|
||||
}
|
||||
|
||||
func (c *ApiGetCardController) extractWindsurf(platform, dataType string, startID uint64, now time.Time) {
|
||||
c.extractWithProbe("windsurf", platform, dataType, now, func() (uint64, *string, *string, string, string, *int8, error) {
|
||||
c.extractWithProbe("windsurf", platform, dataType, now, "", func() (uint64, *string, *string, string, string, *int8, error) {
|
||||
var row models.PlatformAccountPoolWindsurf
|
||||
qs := models.Orm.QueryTable(new(models.PlatformAccountPoolWindsurf)).
|
||||
Filter("is_extracted", 0).
|
||||
@@ -160,7 +186,7 @@ func (c *ApiGetCardController) extractWindsurf(platform, dataType string, startI
|
||||
}
|
||||
|
||||
func (c *ApiGetCardController) extractKrio(platform, dataType string, startID uint64, now time.Time) {
|
||||
c.extractWithProbe("krio", platform, dataType, now, func() (uint64, *string, *string, string, string, *int8, error) {
|
||||
c.extractWithProbe("krio", platform, dataType, now, "", func() (uint64, *string, *string, string, string, *int8, error) {
|
||||
var row models.PlatformAccountPoolKiro
|
||||
qs := models.Orm.QueryTable(new(models.PlatformAccountPoolKiro)).
|
||||
Filter("is_extracted", 0).
|
||||
@@ -179,7 +205,7 @@ func (c *ApiGetCardController) extractKrio(platform, dataType string, startID ui
|
||||
}
|
||||
|
||||
func (c *ApiGetCardController) extractCodex(platform, dataType string, startID uint64, now time.Time) {
|
||||
c.extractWithProbe("codex", platform, dataType, now, func() (uint64, *string, *string, string, string, *int8, error) {
|
||||
c.extractWithProbe("codex", platform, dataType, now, "", func() (uint64, *string, *string, string, string, *int8, error) {
|
||||
var row models.PlatformAccountPoolCodex
|
||||
qs := models.Orm.QueryTable(new(models.PlatformAccountPoolCodex)).
|
||||
Filter("is_extracted", 0).
|
||||
@@ -203,6 +229,7 @@ type poolRowFetcher func() (id uint64, account, password *string, token, rowData
|
||||
func (c *ApiGetCardController) extractWithProbe(
|
||||
module, platform, dataType string,
|
||||
now time.Time,
|
||||
machineCode string,
|
||||
fetch poolRowFetcher,
|
||||
) {
|
||||
for {
|
||||
@@ -221,14 +248,20 @@ func (c *ApiGetCardController) extractWithProbe(
|
||||
c.cardErr(500, 500, "无效模块")
|
||||
return
|
||||
}
|
||||
|
||||
params := map[string]interface{}{
|
||||
"is_extracted": 1,
|
||||
"extracted_time": now,
|
||||
"extracted_platform": platform,
|
||||
"update_time": now,
|
||||
}
|
||||
if module == "cursor" && machineCode != "" {
|
||||
params["machine_code"] = machineCode
|
||||
}
|
||||
|
||||
_, err = models.Orm.QueryTable(tableName).
|
||||
Filter("id", id).
|
||||
Update(map[string]interface{}{
|
||||
"is_extracted": 1,
|
||||
"extracted_time": now,
|
||||
"extracted_platform": platform,
|
||||
"update_time": now,
|
||||
})
|
||||
Update(params)
|
||||
if err != nil {
|
||||
c.cardErr(500, 500, "提取失败")
|
||||
return
|
||||
@@ -240,10 +273,16 @@ func (c *ApiGetCardController) extractWithProbe(
|
||||
c.cardOK(buildCardResult(account, password, token, rowDataType))
|
||||
return
|
||||
}
|
||||
if module == "cursor" && machineCode != "" {
|
||||
_, _ = models.Orm.QueryTable(tableName).Filter("id", id).Update(map[string]interface{}{"machine_code": "", "update_time": time.Now()})
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !poolProbeToken(module, rowDataType, token, id) {
|
||||
if module == "cursor" && machineCode != "" {
|
||||
_, _ = models.Orm.QueryTable(tableName).Filter("id", id).Update(map[string]interface{}{"machine_code": "", "update_time": time.Now()})
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/pkg/jwtutil"
|
||||
@@ -209,39 +208,17 @@ func (c *BackendLoginVerifyController) SaveLoginVerifyInfos() {
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
var existed models.PlatformLoginVerify
|
||||
err = models.Orm.QueryTable(new(models.PlatformLoginVerify)).OrderBy("-id").One(&existed)
|
||||
if err == nil {
|
||||
_, err = models.Orm.QueryTable(new(models.PlatformLoginVerify)).
|
||||
Filter("id", existed.ID).
|
||||
Update(map[string]interface{}{
|
||||
"open_verify_enabled": openVerifyEnabled,
|
||||
"verify_type": verifyType,
|
||||
"geetest3_id": geetest3ID,
|
||||
"geetest3_key": geetest3Key,
|
||||
"geetest4_id": geetest4ID,
|
||||
"geetest4_key": geetest4Key,
|
||||
"update_time": now,
|
||||
})
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "保存失败")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
row := &models.PlatformLoginVerify{
|
||||
OpenVerifyEnabled: openVerifyEnabled,
|
||||
VerifyType: verifyType,
|
||||
Geetest3ID: geetest3ID,
|
||||
Geetest3Key: geetest3Key,
|
||||
Geetest4ID: geetest4ID,
|
||||
Geetest4Key: geetest4Key,
|
||||
UpdateTime: &now,
|
||||
}
|
||||
if _, err := models.Orm.Insert(row); err != nil {
|
||||
c.jsonErr(500, 500, "保存失败")
|
||||
return
|
||||
}
|
||||
err = models.SavePlatformLoginVerify(&models.PlatformLoginVerify{
|
||||
OpenVerifyEnabled: openVerifyEnabled,
|
||||
VerifyType: verifyType,
|
||||
Geetest3ID: geetest3ID,
|
||||
Geetest3Key: geetest3Key,
|
||||
Geetest4ID: geetest4ID,
|
||||
Geetest4Key: geetest4Key,
|
||||
})
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "保存失败")
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "保存成功"}
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/pkg/jwtutil"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
type PlatformBarkController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *PlatformBarkController) platformClaims() (*jwtutil.Claims, error) {
|
||||
auth := c.Ctx.Request.Header.Get("Authorization")
|
||||
if auth == "" {
|
||||
return nil, fmt.Errorf("未登录")
|
||||
}
|
||||
parts := strings.SplitN(auth, " ", 2)
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
return nil, fmt.Errorf("认证信息格式错误")
|
||||
}
|
||||
claims, err := jwtutil.ParseToken(parts[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无效的token")
|
||||
}
|
||||
if claims.UserType != "platform" {
|
||||
return nil, fmt.Errorf("无权访问")
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
func (c *PlatformBarkController) jsonErr(httpStatus, bizCode int, msg string) {
|
||||
c.Ctx.Output.SetStatus(httpStatus)
|
||||
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetBarkInfo GET /platform/bark/info
|
||||
func (c *PlatformBarkController) GetBarkInfo() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
enabledStr := models.GetPlatformSettingValue("bark_enabled", "0")
|
||||
serverURL := models.GetPlatformSettingValue("bark_server_url", "https://api.day.app")
|
||||
deviceKey := models.GetPlatformSettingValue("bark_device_key", "")
|
||||
|
||||
enabled := false
|
||||
if enabledStr == "1" {
|
||||
enabled = true
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": map[string]interface{}{
|
||||
"enabled": enabled,
|
||||
"server_url": serverURL,
|
||||
"device_key": deviceKey,
|
||||
},
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
type barkEditPayload struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
ServerUrl string `json:"server_url"`
|
||||
DeviceKey string `json:"device_key"`
|
||||
}
|
||||
|
||||
// EditBarkInfo POST /platform/bark/editinfo
|
||||
func (c *PlatformBarkController) EditBarkInfo() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
raw, err := io.ReadAll(c.Ctx.Request.Body)
|
||||
if err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
var p barkEditPayload
|
||||
if err := json.Unmarshal(raw, &p); err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
enabledStr := "0"
|
||||
if p.Enabled {
|
||||
enabledStr = "1"
|
||||
}
|
||||
|
||||
serverURL := strings.TrimSpace(p.ServerUrl)
|
||||
if serverURL == "" {
|
||||
serverURL = "https://api.day.app"
|
||||
}
|
||||
deviceKey := strings.TrimSpace(p.DeviceKey)
|
||||
|
||||
settings := []struct {
|
||||
code string
|
||||
name string
|
||||
value string
|
||||
remark string
|
||||
}{
|
||||
{"bark_enabled", "Bark推送启用状态", enabledStr, "0为关闭,1为开启"},
|
||||
{"bark_server_url", "Bark推送服务器地址", serverURL, ""},
|
||||
{"bark_device_key", "Bark设备Key", deviceKey, ""},
|
||||
}
|
||||
|
||||
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 {
|
||||
c.jsonErr(500, 500, "保存失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
} 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 {
|
||||
c.jsonErr(500, 500, "保存失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "保存成功"}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
type barkTestPayload struct {
|
||||
ServerUrl string `json:"server_url"`
|
||||
DeviceKey string `json:"device_key"`
|
||||
}
|
||||
|
||||
// SendTestBark POST /platform/bark/sendtest
|
||||
func (c *PlatformBarkController) SendTestBark() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
raw, err := io.ReadAll(c.Ctx.Request.Body)
|
||||
if err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
var p barkTestPayload
|
||||
if err := json.Unmarshal(raw, &p); err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
serverURL := strings.TrimSpace(p.ServerUrl)
|
||||
if serverURL == "" {
|
||||
serverURL = models.GetPlatformSettingValue("bark_server_url", "https://api.day.app")
|
||||
}
|
||||
deviceKey := strings.TrimSpace(p.DeviceKey)
|
||||
if deviceKey == "" {
|
||||
deviceKey = models.GetPlatformSettingValue("bark_device_key", "")
|
||||
}
|
||||
|
||||
if deviceKey == "" {
|
||||
c.jsonErr(400, 400, "设备 Key 不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 拼接发送 URL,注意去除多余斜杠
|
||||
baseURL := strings.TrimRight(serverURL, "/")
|
||||
// Bark 的格式是: base_url/device_key/title/body
|
||||
testURL := fmt.Sprintf("%s/%s/测试通知/您配置的 Bark 推送服务已连接成功!", baseURL, deviceKey)
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Get(testURL)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "发送失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||
c.jsonErr(500, 500, fmt.Sprintf("发送失败,HTTP 状态码: %d, 返回内容: %s", resp.StatusCode, string(bodyBytes)))
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "测试推送已发出,请注意查收"}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
@@ -105,10 +105,11 @@ func (c *PlatformCursorEquipmentController) cursorActivationSummary(row *models.
|
||||
return count, &latest
|
||||
}
|
||||
|
||||
func (c *PlatformCursorEquipmentController) cursorExtractSummary() (int64, *models.PlatformAccountPoolCursor) {
|
||||
func (c *PlatformCursorEquipmentController) cursorExtractSummary(machineCode string) (int64, *models.PlatformAccountPoolCursor) {
|
||||
qs := models.Orm.QueryTable(new(models.PlatformAccountPoolCursor)).
|
||||
Filter("delete_time__isnull", true).
|
||||
Filter("is_extracted__gt", 0)
|
||||
Filter("is_extracted__gt", 0).
|
||||
Filter("machine_code", machineCode)
|
||||
|
||||
count, _ := qs.Count()
|
||||
|
||||
@@ -122,7 +123,7 @@ func (c *PlatformCursorEquipmentController) cursorExtractSummary() (int64, *mode
|
||||
|
||||
func (c *PlatformCursorEquipmentController) rowToMap(row *models.PlatformCursorEquipment) map[string]interface{} {
|
||||
activationCount, latestActivation := c.cursorActivationSummary(row)
|
||||
extractCount, latestExtract := c.cursorExtractSummary()
|
||||
extractCount, latestExtract := c.cursorExtractSummary(row.MachineCode)
|
||||
|
||||
var bindActivationCode interface{}
|
||||
var activationCodeId interface{}
|
||||
@@ -640,7 +641,8 @@ func (c *PlatformCursorEquipmentController) ExtractRecords() {
|
||||
|
||||
qs := models.Orm.QueryTable(new(models.PlatformAccountPoolCursor)).
|
||||
Filter("delete_time__isnull", true).
|
||||
Filter("is_extracted__gt", 0)
|
||||
Filter("is_extracted__gt", 0).
|
||||
Filter("machine_code", equipment.MachineCode)
|
||||
|
||||
total, _ := qs.Count()
|
||||
|
||||
|
||||
@@ -85,37 +85,18 @@ func (c *PlatformLoginVerifyController) SaveLoginVerifyInfos() {
|
||||
}
|
||||
}
|
||||
|
||||
var existed models.PlatformLoginVerify
|
||||
err := models.Orm.QueryTable(new(models.PlatformLoginVerify)).OrderBy("-id").One(&existed)
|
||||
if err == nil {
|
||||
update := map[string]interface{}{
|
||||
"open_verify_enabled": openVerifyEnabled,
|
||||
"verify_type": verifyType,
|
||||
"geetest3_id": p.Geetest3ID,
|
||||
"geetest3_key": p.Geetest3Key,
|
||||
"geetest4_id": p.Geetest4ID,
|
||||
"geetest4_key": p.Geetest4Key,
|
||||
}
|
||||
_, err = models.Orm.QueryTable(new(models.PlatformLoginVerify)).Filter("id", existed.ID).Update(update)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "保存失败"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
} else {
|
||||
row := &models.PlatformLoginVerify{
|
||||
OpenVerifyEnabled: openVerifyEnabled,
|
||||
VerifyType: verifyType,
|
||||
Geetest3ID: p.Geetest3ID,
|
||||
Geetest3Key: p.Geetest3Key,
|
||||
Geetest4ID: p.Geetest4ID,
|
||||
Geetest4Key: p.Geetest4Key,
|
||||
}
|
||||
if _, err := models.Orm.Insert(row); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "保存失败"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
err := models.SavePlatformLoginVerify(&models.PlatformLoginVerify{
|
||||
OpenVerifyEnabled: openVerifyEnabled,
|
||||
VerifyType: verifyType,
|
||||
Geetest3ID: p.Geetest3ID,
|
||||
Geetest3Key: p.Geetest3Key,
|
||||
Geetest4ID: p.Geetest4ID,
|
||||
Geetest4Key: p.Geetest4Key,
|
||||
})
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "保存失败"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "保存成功"}
|
||||
|
||||
@@ -55,24 +55,8 @@ func (c *PlatformSMSController) GetSmsInfo() {
|
||||
return
|
||||
}
|
||||
|
||||
var row models.SystemSMS
|
||||
// 优先默认通道,其次 custom
|
||||
err := models.Orm.QueryTable(new(models.SystemSMS)).
|
||||
Filter("is_default", 1).
|
||||
Filter("status", 1).
|
||||
OrderBy("-weight", "-id").
|
||||
Limit(1).
|
||||
One(&row)
|
||||
if err != nil {
|
||||
_ = models.Orm.QueryTable(new(models.SystemSMS)).
|
||||
Filter("config_code", "custom").
|
||||
OrderBy("-id").
|
||||
Limit(1).
|
||||
One(&row)
|
||||
}
|
||||
|
||||
backendURL := strings.TrimSpace(row.ApiURL)
|
||||
apiKey := strings.TrimSpace(row.ApiKey)
|
||||
backendURL := models.GetPlatformSettingValue("sms_custom_url", "")
|
||||
apiKey := models.GetPlatformSettingValue("sms_custom_key", "")
|
||||
|
||||
data := []map[string]interface{}{{
|
||||
"backend_url": backendURL,
|
||||
@@ -93,7 +77,7 @@ type smsEditPayload struct {
|
||||
}
|
||||
|
||||
// EditSmsInfo POST /platform/sms/editinfo
|
||||
// 将旧前端的 backendUrl/apiKey 落到 yz_system_sms 的 api_url/api_key(写入 config_code=custom)
|
||||
// 将旧前端的 backendUrl/apiKey 落到 yz_platform_normal_setting 表中
|
||||
func (c *PlatformSMSController) EditSmsInfo() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
@@ -128,44 +112,46 @@ func (c *PlatformSMSController) EditSmsInfo() {
|
||||
return
|
||||
}
|
||||
|
||||
// 确保只有一个默认:先清空默认,再 upsert custom 为默认
|
||||
_, _ = models.Orm.QueryTable(new(models.SystemSMS)).Update(map[string]interface{}{"is_default": 0})
|
||||
settings := []struct {
|
||||
code string
|
||||
name string
|
||||
value string
|
||||
remark string
|
||||
}{
|
||||
{"sms_custom_url", "自定义短信网关地址", backendURL, ""},
|
||||
{"sms_custom_key", "自定义短信API KEY", apiKey, ""},
|
||||
}
|
||||
|
||||
var existed models.SystemSMS
|
||||
e := models.Orm.QueryTable(new(models.SystemSMS)).Filter("config_code", "custom").Limit(1).One(&existed)
|
||||
if e == nil && existed.ID > 0 {
|
||||
_, err = models.Orm.QueryTable(new(models.SystemSMS)).Filter("id", existed.ID).Update(map[string]interface{}{
|
||||
"config_name": "自定义网关",
|
||||
"channel_type": 2,
|
||||
"api_url": backendURL,
|
||||
"api_key": apiKey,
|
||||
"weight": 10,
|
||||
"is_default": 1,
|
||||
"status": 1,
|
||||
})
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "更新失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
} else {
|
||||
row := &models.SystemSMS{
|
||||
ConfigCode: "custom",
|
||||
ConfigName: "自定义网关",
|
||||
ChannelType: 2,
|
||||
ApiURL: backendURL,
|
||||
ApiKey: apiKey,
|
||||
ApiSecret: "",
|
||||
SignName: "",
|
||||
TemplateID: "",
|
||||
TestPhone: "",
|
||||
Weight: 10,
|
||||
IsDefault: 1,
|
||||
Status: 1,
|
||||
Remark: "",
|
||||
}
|
||||
if _, err := models.Orm.Insert(row); err != nil {
|
||||
c.jsonErr(500, 500, "更新失败: "+err.Error())
|
||||
return
|
||||
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 {
|
||||
c.jsonErr(500, 500, "保存失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
} 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 {
|
||||
c.jsonErr(500, 500, "保存失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,18 +218,11 @@ func (c *PlatformSMSController) SendTestSms() {
|
||||
|
||||
// 兜底:body 未带时从默认配置取
|
||||
if backendURL == "" || apiKey == "" {
|
||||
var row models.SystemSMS
|
||||
_ = models.Orm.QueryTable(new(models.SystemSMS)).
|
||||
Filter("is_default", 1).
|
||||
Filter("status", 1).
|
||||
OrderBy("-weight", "-id").
|
||||
Limit(1).
|
||||
One(&row)
|
||||
if backendURL == "" {
|
||||
backendURL = strings.TrimSpace(row.ApiURL)
|
||||
backendURL = models.GetPlatformSettingValue("sms_custom_url", "")
|
||||
}
|
||||
if apiKey == "" {
|
||||
apiKey = strings.TrimSpace(row.ApiKey)
|
||||
apiKey = models.GetPlatformSettingValue("sms_custom_key", "")
|
||||
}
|
||||
}
|
||||
if backendURL == "" {
|
||||
|
||||
Reference in New Issue
Block a user