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,21 @@ package bilibili
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
)
|
||||
|
||||
// BilibiliAccountConfigValidator B站账号配置验证器
|
||||
type BilibiliAccountConfigValidator struct{}
|
||||
|
||||
func (v *BilibiliAccountConfigValidator) GetType() string {
|
||||
return "bilibili-account"
|
||||
return AccountType
|
||||
}
|
||||
|
||||
func (v *BilibiliAccountConfigValidator) Validate(config string) error {
|
||||
var cfg BiliAccount
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return fmt.Errorf("解析B站账号配置失败: %v", err)
|
||||
cfg, err := parseAccount(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cfg.RawCookies == "" {
|
||||
@@ -24,3 +26,49 @@ func (v *BilibiliAccountConfigValidator) Validate(config string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Test 用 cookie 拉一次账号信息,验证 cookie 是否还有效
|
||||
func (v *BilibiliAccountConfigValidator) Test(config string) error {
|
||||
cfg, err := parseAccount(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := newClient(cfg.RawCookies)
|
||||
info, err := client.GetAccountInformation()
|
||||
if err != nil {
|
||||
return fmt.Errorf("B站 cookie 校验失败: %w", err)
|
||||
}
|
||||
if info == nil || info.Mid == 0 {
|
||||
return fmt.Errorf("B站 cookie 无效或已过期")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *BilibiliAccountConfigValidator) Meta() meta.AccountMeta {
|
||||
return meta.AccountMeta{
|
||||
Type: AccountType,
|
||||
Label: "哔哩哔哩",
|
||||
Description: "使用浏览器中的完整 Cookie 登录 B 站,用于发送动态。",
|
||||
Docs: "docs/bilibili-config.md",
|
||||
Fields: []meta.Field{
|
||||
{
|
||||
Key: "raw_cookies",
|
||||
Label: "Cookies",
|
||||
Type: meta.FieldTextarea,
|
||||
Required: true,
|
||||
Secret: true,
|
||||
Placeholder: "SESSDATA=xxx; bili_jct=xxx; DedeUserID=xxx",
|
||||
Help: "登录 bilibili.com 后从浏览器开发者工具复制完整的 Cookie 字符串",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func parseAccount(config string) (*BiliAccount, error) {
|
||||
var cfg BiliAccount
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return nil, fmt.Errorf("解析B站账号配置失败: %v", err)
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
@@ -2,55 +2,40 @@ package bilibili
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
"fmt"
|
||||
|
||||
"github.com/CuteReimu/bilibili/v2"
|
||||
"github.com/cenkalti/backoff/v5"
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/internal/retry"
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
// 在Bilibili动态发送消息
|
||||
// SendBiliDynamicMsg 在 Bilibili 动态发送消息
|
||||
//
|
||||
// 暂时没有处理 429 和 403 的区别对待
|
||||
func SendBiliDynamicMsg(rule *model.Rule) {
|
||||
config, err := configs.GetConfig()
|
||||
if err != nil {
|
||||
log.Printf("获取配置失败: %v", err)
|
||||
return
|
||||
}
|
||||
var timeout time.Duration = time.Duration(config.TimeoutDurationHours) * time.Hour
|
||||
|
||||
func SendBiliDynamicMsg(ctx context.Context, rule *model.Rule) error {
|
||||
biliClient, err := getBiliClient(rule)
|
||||
if err != nil {
|
||||
log.Printf("获取B站客户端失败: %v", err)
|
||||
return
|
||||
}
|
||||
biliDyncmicConfig, err := getBiliDynamicConfig(rule)
|
||||
if err != nil {
|
||||
log.Printf("获取B站动态配置失败: %v", err)
|
||||
return
|
||||
return fmt.Errorf("获取B站客户端失败: %w", err)
|
||||
}
|
||||
|
||||
var dynamicParams bilibili.CreateDynamicParam
|
||||
dynamicParams = bilibili.CreateDynamicParam{
|
||||
biliDynamicConfig, err := getBiliDynamicConfig(rule)
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取B站动态配置失败: %w", err)
|
||||
}
|
||||
|
||||
dynamicParams := bilibili.CreateDynamicParam{
|
||||
DynamicId: 0,
|
||||
Type: 4,
|
||||
Rid: 0,
|
||||
Content: biliDyncmicConfig.Msg,
|
||||
Content: biliDynamicConfig.Msg,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
operation := func() (string, error) {
|
||||
if err := retry.Do(ctx, func() error {
|
||||
_, err := biliClient.CreateDynamic(dynamicParams)
|
||||
return "", err
|
||||
return err
|
||||
}); err != nil {
|
||||
return fmt.Errorf("发送B站动态失败: %w", err)
|
||||
}
|
||||
|
||||
_, err = backoff.Retry(ctx, operation, backoff.WithBackOff(backoff.NewExponentialBackOff()))
|
||||
if err != nil {
|
||||
log.Printf("发送B站动态失败: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package bilibili
|
||||
|
||||
const (
|
||||
// AccountType B 站账号类型标识
|
||||
AccountType = "bilibili"
|
||||
// RuleTypeDynamic 发送 B 站动态的规则类型标识
|
||||
RuleTypeDynamic = "bilibili-dynamic"
|
||||
)
|
||||
|
||||
type BiliAccount struct {
|
||||
RawCookies string `json:"raw_cookies"`
|
||||
}
|
||||
|
||||
@@ -3,13 +3,15 @@ package bilibili
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
)
|
||||
|
||||
// BilibiliDynamicRuleValidator B站动态规则验证器
|
||||
type BilibiliDynamicRuleValidator struct{}
|
||||
|
||||
func (v *BilibiliDynamicRuleValidator) GetType() string {
|
||||
return "bilibili-dynamic"
|
||||
return RuleTypeDynamic
|
||||
}
|
||||
|
||||
func (v *BilibiliDynamicRuleValidator) Validate(configJSON string) error {
|
||||
@@ -22,5 +24,28 @@ func (v *BilibiliDynamicRuleValidator) Validate(configJSON string) error {
|
||||
return fmt.Errorf("B站动态规则配置中 msg 不能为空")
|
||||
}
|
||||
|
||||
if len([]rune(config.Msg)) > 1000 {
|
||||
return fmt.Errorf("B站动态内容过长")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *BilibiliDynamicRuleValidator) Meta() meta.RuleMeta {
|
||||
return meta.RuleMeta{
|
||||
Type: RuleTypeDynamic,
|
||||
Label: "发送 B 站动态",
|
||||
Description: "触发时以关联的 B 站账号发送一条动态。",
|
||||
Docs: "docs/bilibili-config.md",
|
||||
AccountType: AccountType,
|
||||
Fields: []meta.Field{
|
||||
{
|
||||
Key: "msg",
|
||||
Label: "动态内容",
|
||||
Type: meta.FieldTextarea,
|
||||
Required: true,
|
||||
Placeholder: "要发送的动态正文",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package bilibili
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
@@ -10,13 +11,11 @@ import (
|
||||
type BilibiliDynamicExecutor struct{}
|
||||
|
||||
func (e *BilibiliDynamicExecutor) GetType() string {
|
||||
return "bilibili-dynamic"
|
||||
return RuleTypeDynamic
|
||||
}
|
||||
|
||||
func (e *BilibiliDynamicExecutor) Execute(rule *model.Rule) error {
|
||||
func (e *BilibiliDynamicExecutor) Execute(ctx context.Context, rule *model.Rule) error {
|
||||
log.Printf("执行B站动态规则: %s (ID: %d)", rule.Name, rule.ID)
|
||||
|
||||
SendBiliDynamicMsg(rule)
|
||||
|
||||
return nil
|
||||
return SendBiliDynamicMsg(ctx, rule)
|
||||
}
|
||||
|
||||
+10
-22
@@ -8,38 +8,26 @@ import (
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
func newClient(rawCookies string) *bilibili.Client {
|
||||
client := bilibili.New()
|
||||
client.SetRawCookies(rawCookies)
|
||||
return client
|
||||
}
|
||||
|
||||
// 获取 Bilibili Client
|
||||
func getBiliClient(rule *model.Rule) (*bilibili.Client, error) {
|
||||
client := bilibili.New()
|
||||
gormDB, err := db.GetGormDB()
|
||||
if err != nil {
|
||||
var accountConfig BiliAccount
|
||||
if err := db.LoadAccountConfig(rule.AccountID, &accountConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 1. 通过 account_id 获取到对应的 account 配置
|
||||
var biliAccount model.Account
|
||||
result := gormDB.Where("id = ?", rule.AccountID).First(&biliAccount)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
// 2. 通过 account 配置中的 config 字段获取到 CookiesString
|
||||
var biliAccountConfig BiliAccount
|
||||
err = json.Unmarshal([]byte(biliAccount.Config), &biliAccountConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client.SetRawCookies(biliAccountConfig.RawCookies)
|
||||
|
||||
return client, nil
|
||||
return newClient(accountConfig.RawCookies), nil
|
||||
}
|
||||
|
||||
// 获取 Bilibili Dynamic Config
|
||||
func getBiliDynamicConfig(rule *model.Rule) (*BiliDynamicConfig, error) {
|
||||
var biliDynamicConfig BiliDynamicConfig
|
||||
err := json.Unmarshal([]byte(rule.ConfigJson), &biliDynamicConfig)
|
||||
if err != nil {
|
||||
if err := json.Unmarshal([]byte(rule.ConfigJson), &biliDynamicConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user