2026-04-05 22:46:11 +08:00
|
|
|
package account
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-26 22:19:41 +08:00
|
|
|
"strconv"
|
|
|
|
|
|
2026-04-05 22:46:11 +08:00
|
|
|
"github.com/ssdomei232/goodBaby/handler/db"
|
2026-07-26 22:19:41 +08:00
|
|
|
"github.com/ssdomei232/goodBaby/internal/accountConfigChecker"
|
|
|
|
|
"github.com/ssdomei232/goodBaby/internal/meta"
|
2026-04-05 22:46:11 +08:00
|
|
|
"github.com/ssdomei232/goodBaby/model"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
func parseID(raw string) (uint, error) {
|
|
|
|
|
id, err := strconv.ParseUint(raw, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return uint(id), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// findAccount 查找属于该用户的账号
|
|
|
|
|
func findAccount(accountID, uid uint) (*model.Account, error) {
|
|
|
|
|
gormDB, err := db.GetGormDB()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var account model.Account
|
|
|
|
|
if err := gormDB.Where("id = ? AND uid = ?", accountID, uid).First(&account).Error; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &account, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getRulesByAccountID 获取账号相关的规则列表
|
2026-04-05 22:46:11 +08:00
|
|
|
func getRulesByAccountID(accountID uint, uid uint) ([]*model.Rule, error) {
|
|
|
|
|
gormDB, err := db.GetGormDB()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
rules := []*model.Rule{}
|
|
|
|
|
if err := gormDB.Where("account_id = ? AND uid = ?", accountID, uid).Find(&rules).Error; err != nil {
|
|
|
|
|
return nil, err
|
2026-04-05 22:46:11 +08:00
|
|
|
}
|
|
|
|
|
return rules, nil
|
|
|
|
|
}
|
2026-07-26 22:19:41 +08:00
|
|
|
|
|
|
|
|
// maskAccount 掩码账号配置中的敏感字段,避免 Cookie/密码原样返回给前端
|
|
|
|
|
func maskAccount(account model.Account) model.Account {
|
|
|
|
|
fields := accountConfigChecker.InitValidatorRegistry().FieldsOf(account.Type)
|
|
|
|
|
account.Config = meta.Mask(account.Config, fields)
|
|
|
|
|
return account
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func maskAccounts(accounts []model.Account) []model.Account {
|
|
|
|
|
masked := make([]model.Account, 0, len(accounts))
|
|
|
|
|
for _, a := range accounts {
|
|
|
|
|
masked = append(masked, maskAccount(a))
|
|
|
|
|
}
|
|
|
|
|
return masked
|
|
|
|
|
}
|
2026-08-30 17:47:53 +08:00
|
|
|
|
|
|
|
|
// getAccountOwnerUID 获取账号所属用户的 UID
|
|
|
|
|
func getAccountOwnerUID(accountID uint) (uint, error) {
|
|
|
|
|
gormDB, err := db.GetGormDB()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var account model.Account
|
|
|
|
|
if err := gormDB.Select("uid").Where("id = ?", accountID).First(&account).Error; err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return account.UID, nil
|
|
|
|
|
}
|