feat: 完善后端并新增 Vue3 WebUI
后端: - 修复登录校验、DB 并发(单例+WAL+纯Go sqlite)、会话密钥持久化等核心缺陷 - 新增 Timer CRUD/签到/手动触发、执行日志、仪表盘、驱动元数据等 API - 统一驱动接口: 带 context 的执行器、真实错误返回、敏感字段掩码、连通性测试 - 补全钉钉执行器; Timer 触发/提醒去重; 配置支持默认值与环境变量覆盖 前端: - Vite+Vue3+TS+Pinia+Element Plus, 由驱动元数据动态渲染配置表单 - 登录/仪表盘(倒计时环)/定时器/规则/账号/日志/设置页面, 支持暗色模式 - 主题色 #66ccff, 信号波纹 logo, go:embed 单二进制部署 构建: Dockerfile 增加前端构建阶段; 补充各驱动配置文档 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,19 +3,23 @@ package onebot
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
)
|
||||
|
||||
// AccountConfigValidator OneBot账号配置验证器接口
|
||||
// OneBotAccountConfigValidator OneBot账号配置验证器
|
||||
type OneBotAccountConfigValidator struct{}
|
||||
|
||||
func (v *OneBotAccountConfigValidator) GetType() string {
|
||||
return "onebot"
|
||||
return AccountType
|
||||
}
|
||||
|
||||
func (v *OneBotAccountConfigValidator) Validate(config string) error {
|
||||
var cfg OneBotAccount
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return fmt.Errorf("解析OneBot账号配置失败: %v", err)
|
||||
cfg, err := parseAccount(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cfg.Token == "" {
|
||||
@@ -25,5 +29,44 @@ func (v *OneBotAccountConfigValidator) Validate(config string) error {
|
||||
return fmt.Errorf("OneBot账号配置中URL不能为空")
|
||||
}
|
||||
|
||||
parsed, err := url.Parse(cfg.URL)
|
||||
if err != nil || parsed.Host == "" || !strings.HasPrefix(parsed.Scheme, "http") {
|
||||
return fmt.Errorf("OneBot账号配置中URL不合法,应形如 http://localhost:3000")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Test 调用 get_login_info 验证地址与 Token
|
||||
func (v *OneBotAccountConfigValidator) Test(config string) error {
|
||||
cfg, err := parseAccount(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var resp apiResponse
|
||||
if err := newClient(cfg).DoRequest("POST", "/get_login_info", map[string]any{}, &resp); err != nil {
|
||||
return fmt.Errorf("连接 OneBot 失败: %w", err)
|
||||
}
|
||||
return resp.err()
|
||||
}
|
||||
|
||||
func (v *OneBotAccountConfigValidator) Meta() meta.AccountMeta {
|
||||
return meta.AccountMeta{
|
||||
Type: AccountType,
|
||||
Label: "OneBot (QQ)",
|
||||
Description: "对接 NapCat / go-cqhttp 等 OneBot HTTP 服务,用于发送 QQ 消息。",
|
||||
Fields: []meta.Field{
|
||||
{Key: "url", Label: "HTTP 服务地址", Type: meta.FieldString, Required: true, Placeholder: "http://localhost:3000"},
|
||||
{Key: "token", Label: "Access Token", Type: meta.FieldPassword, Required: true, Secret: true},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func parseAccount(config string) (*OneBotAccount, error) {
|
||||
var cfg OneBotAccount
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return nil, fmt.Errorf("解析OneBot账号配置失败: %v", err)
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
+44
-2
@@ -1,11 +1,53 @@
|
||||
package onebot
|
||||
|
||||
const (
|
||||
// AccountType OneBot 账号类型标识
|
||||
AccountType = "onebot"
|
||||
// RuleType 发送 OneBot 消息的规则类型标识
|
||||
RuleType = "onebot"
|
||||
)
|
||||
|
||||
type OneBotConfig struct {
|
||||
SendGroups []int `json:"send_groups"`
|
||||
Msg string `json:"msg"`
|
||||
SendGroups []int64 `json:"send_groups"` // 群号列表
|
||||
SendUsers []int64 `json:"send_users"` // 好友 QQ 号列表
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type OneBotAccount struct {
|
||||
URL string `json:"url"` // 例如 "http://localhost:5700"
|
||||
Token string `json:"token"` // 例如 "your_token_here"
|
||||
}
|
||||
|
||||
// apiResponse OneBot HTTP API 的通用响应信封
|
||||
type apiResponse struct {
|
||||
Status string `json:"status"`
|
||||
Retcode int `json:"retcode"`
|
||||
Message string `json:"message"`
|
||||
Wording string `json:"wording"`
|
||||
}
|
||||
|
||||
// err 把 OneBot 返回的业务错误转换成 Go error
|
||||
func (r *apiResponse) err() error {
|
||||
if r.Status == "ok" && r.Retcode == 0 {
|
||||
return nil
|
||||
}
|
||||
detail := r.Wording
|
||||
if detail == "" {
|
||||
detail = r.Message
|
||||
}
|
||||
if detail == "" {
|
||||
detail = "请检查 OneBot 地址与 Token"
|
||||
}
|
||||
return &APIError{Status: r.Status, Retcode: r.Retcode, Detail: detail}
|
||||
}
|
||||
|
||||
// APIError OneBot 返回的业务错误
|
||||
type APIError struct {
|
||||
Status string
|
||||
Retcode int
|
||||
Detail string
|
||||
}
|
||||
|
||||
func (e *APIError) Error() string {
|
||||
return "OneBot 返回错误(status=" + e.Status + "): " + e.Detail
|
||||
}
|
||||
|
||||
+34
-25
@@ -2,41 +2,50 @@ package onebot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/cenkalti/backoff/v5"
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/internal/retry"
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
func SendOneBotMsg(rule *model.Rule) {
|
||||
oneBotConfig := getOneBotConfigFromRule(rule)
|
||||
// SendOneBotMsg 向规则中配置的群和好友发送消息
|
||||
func SendOneBotMsg(ctx context.Context, rule *model.Rule) error {
|
||||
oneBotConfig, err := getOneBotConfigFromRule(rule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
oneBotAccount, err := getOneBotAccountFromRule(rule)
|
||||
if err != nil {
|
||||
log.Printf("获取OneBot账户失败: %v", err)
|
||||
return
|
||||
return fmt.Errorf("获取OneBot账户失败: %w", err)
|
||||
}
|
||||
|
||||
var fails []string
|
||||
total := 0
|
||||
|
||||
for _, groupID := range oneBotConfig.SendGroups {
|
||||
config, err := configs.GetConfig()
|
||||
total++
|
||||
err := retry.Do(ctx, func() error {
|
||||
return sendGroupMsg(oneBotAccount, groupID, oneBotConfig.Msg)
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("获取配置失败: %v", err)
|
||||
return
|
||||
}
|
||||
var timeout time.Duration = time.Duration(config.TimeoutDurationHours) * time.Hour
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
operation := func() (string, error) {
|
||||
err := sendOneBotMsg(oneBotAccount, groupID, oneBotConfig.Msg)
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = backoff.Retry(ctx, operation, backoff.WithBackOff(backoff.NewExponentialBackOff()))
|
||||
if err != nil {
|
||||
log.Printf("发送OneBot消息失败: %v", err)
|
||||
fails = append(fails, fmt.Sprintf("群 %d: %v", groupID, err))
|
||||
}
|
||||
}
|
||||
|
||||
for _, userID := range oneBotConfig.SendUsers {
|
||||
total++
|
||||
err := retry.Do(ctx, func() error {
|
||||
return sendPrivateMsg(oneBotAccount, userID, oneBotConfig.Msg)
|
||||
})
|
||||
if err != nil {
|
||||
fails = append(fails, fmt.Sprintf("好友 %d: %v", userID, err))
|
||||
}
|
||||
}
|
||||
|
||||
if len(fails) > 0 {
|
||||
return fmt.Errorf("%d/%d 条消息发送失败: %s", len(fails), total, strings.Join(fails, "; "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,13 +3,15 @@ package onebot
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
)
|
||||
|
||||
// OneBotRuleValidator OneBot规则验证器
|
||||
type OneBotRuleValidator struct{}
|
||||
|
||||
func (v *OneBotRuleValidator) GetType() string {
|
||||
return "onebot"
|
||||
return RuleType
|
||||
}
|
||||
|
||||
func (v *OneBotRuleValidator) Validate(configJSON string) error {
|
||||
@@ -22,9 +24,29 @@ func (v *OneBotRuleValidator) Validate(configJSON string) error {
|
||||
return fmt.Errorf("OneBot规则配置中 msg 不能为空")
|
||||
}
|
||||
|
||||
if len(config.SendGroups) == 0 {
|
||||
return fmt.Errorf("OneBot规则配置中 send_groups 不能为空")
|
||||
if len(config.SendGroups) == 0 && len(config.SendUsers) == 0 {
|
||||
return fmt.Errorf("OneBot规则配置中 send_groups 与 send_users 不能同时为空")
|
||||
}
|
||||
|
||||
for _, id := range append(append([]int64{}, config.SendGroups...), config.SendUsers...) {
|
||||
if id <= 0 {
|
||||
return fmt.Errorf("OneBot规则配置中存在非法的群号/QQ号: %d", id)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *OneBotRuleValidator) Meta() meta.RuleMeta {
|
||||
return meta.RuleMeta{
|
||||
Type: RuleType,
|
||||
Label: "发送 QQ 消息",
|
||||
Description: "触发时通过 OneBot 向指定的群或好友发送消息。",
|
||||
AccountType: AccountType,
|
||||
Fields: []meta.Field{
|
||||
{Key: "msg", Label: "消息内容", Type: meta.FieldTextarea, Required: true},
|
||||
{Key: "send_groups", Label: "群号", Type: meta.FieldNumberList, Placeholder: "123456789"},
|
||||
{Key: "send_users", Label: "好友 QQ 号", Type: meta.FieldNumberList, Placeholder: "123456789"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package onebot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
@@ -10,13 +11,11 @@ import (
|
||||
type OneBotExecutor struct{}
|
||||
|
||||
func (e *OneBotExecutor) GetType() string {
|
||||
return "onebot"
|
||||
return RuleType
|
||||
}
|
||||
|
||||
func (e *OneBotExecutor) Execute(rule *model.Rule) error {
|
||||
func (e *OneBotExecutor) Execute(ctx context.Context, rule *model.Rule) error {
|
||||
log.Printf("执行OneBot规则: %s (ID: %d)", rule.Name, rule.ID)
|
||||
|
||||
SendOneBotMsg(rule)
|
||||
|
||||
return nil
|
||||
return SendOneBotMsg(ctx, rule)
|
||||
}
|
||||
|
||||
+34
-22
@@ -2,46 +2,58 @@ package onebot
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"fmt"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/handler/db"
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
napcat "github.com/ssdomei232/napcat-http-go-sdk"
|
||||
)
|
||||
|
||||
func newClient(account *OneBotAccount) *napcat.Client {
|
||||
return napcat.NewClient(account.Token, account.URL)
|
||||
}
|
||||
|
||||
// 从 Rule 中获取 OneBotConfig
|
||||
func getOneBotConfigFromRule(rule *model.Rule) *OneBotConfig {
|
||||
func getOneBotConfigFromRule(rule *model.Rule) (*OneBotConfig, error) {
|
||||
var oneBotConfig OneBotConfig
|
||||
err := json.Unmarshal([]byte(rule.ConfigJson), &oneBotConfig)
|
||||
if err != nil {
|
||||
return nil
|
||||
if err := json.Unmarshal([]byte(rule.ConfigJson), &oneBotConfig); err != nil {
|
||||
return nil, fmt.Errorf("解析OneBot规则配置失败: %w", err)
|
||||
}
|
||||
return &oneBotConfig
|
||||
return &oneBotConfig, nil
|
||||
}
|
||||
|
||||
// 从 Rule 中获取 OneBotAccount
|
||||
func getOneBotAccountFromRule(rule *model.Rule) (*OneBotAccount, error) {
|
||||
gormDB, err := db.GetGormDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var oneBotAccountEntry model.Account
|
||||
gormDB.Where("id = ?", rule.AccountID).First(&oneBotAccountEntry)
|
||||
|
||||
var oneBotAccount OneBotAccount
|
||||
err = json.Unmarshal([]byte(oneBotAccountEntry.Config), &oneBotAccount)
|
||||
if err != nil {
|
||||
if err := db.LoadAccountConfig(rule.AccountID, &oneBotAccount); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &oneBotAccount, nil
|
||||
}
|
||||
|
||||
// 发送一条消息到OneBot
|
||||
func sendOneBotMsg(account *OneBotAccount, groupID int, msg string) error {
|
||||
client := napcat.NewClient(account.Token, account.URL)
|
||||
// 发送一条群消息
|
||||
//
|
||||
// SDK 只返回传输层错误,这里自己解析 OneBot 的响应信封,
|
||||
// 否则 token 错误、群号不存在这类失败会被当成发送成功。
|
||||
func sendGroupMsg(account *OneBotAccount, groupID int64, msg string) error {
|
||||
return call(account, "/send_group_msg", map[string]any{
|
||||
"group_id": groupID,
|
||||
"message": msg,
|
||||
})
|
||||
}
|
||||
|
||||
err := client.SendGroupMsg(strconv.Itoa(groupID), msg)
|
||||
return err
|
||||
// 发送一条私聊消息
|
||||
func sendPrivateMsg(account *OneBotAccount, userID int64, msg string) error {
|
||||
return call(account, "/send_private_msg", map[string]any{
|
||||
"user_id": userID,
|
||||
"message": msg,
|
||||
})
|
||||
}
|
||||
|
||||
func call(account *OneBotAccount, endpoint string, payload map[string]any) error {
|
||||
var resp apiResponse
|
||||
if err := newClient(account).DoRequest("POST", endpoint, payload, &resp); err != nil {
|
||||
return fmt.Errorf("请求 OneBot 失败: %w", err)
|
||||
}
|
||||
return resp.err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user