5d7eca5de3
用户拿旧项目代码对出来的真问题:我此前用 WithWechatPayAutoAuthCipher(平台证书 模式,APIv3 密钥自动下载平台证书验签),但 2024 起新注册商户只发「微信支付公钥」 (PUB_KEY_ID_ 开头)、没有平台证书——在该商户号上初始化/回调验签都会挂。 - 改 WithWechatPayPublicKeyAuthCipher(商户私钥+公钥ID+公钥文件);回调验签用 NewSHA256WithRSAPubkeyVerifier;平台证书模式不留双模式赘肉(YAGNI)。 - Config 增 public_key_path/public_key_id(必填,公钥文件同样只存路径); admin 卡片补两字段;env 兜底加 WECHAT_PUBLIC_KEY(_ID)。 - 顺手修 live 撞出的真 bug:sundynix_setting.value 是 varchar(255), 支付配置 JSON(含加密密钥)一条就超(SQLSTATE 22001)→ 改 text。 live:列类型已迁 text;缺公钥两项报「配置不全,缺: public_key_path, public_key_id」;GET 回显含新字段。go 6 包测试+tsc+41 vitest 全绿。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
52 lines
1.7 KiB
Go
52 lines
1.7 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
// Setting 是平台级键值配置(全局,非租户作用域)——如 token→积分汇率 等可后台热调的计费规则。
|
|
// 表名 sundynix_setting。Key 唯一。
|
|
type Setting struct {
|
|
BaseModel
|
|
Key string `gorm:"size:64;uniqueIndex"`
|
|
Value string `gorm:"type:text"` // 曾是 varchar(255):支付配置 JSON(含加密密钥)一条就超,live 撞过 22001
|
|
}
|
|
|
|
func (Setting) TableName() string { return "sundynix_setting" }
|
|
|
|
// 平台设置键。
|
|
const (
|
|
SettingTokensPerCredit = "tokens_per_credit"
|
|
SettingCreditEnforce = "credit_enforce" // "on"=余额≤0 拒绝新任务;其它/未设=软扣不拦
|
|
)
|
|
|
|
// CreditEnforceEnabled 是否开启积分硬拦截(默认关:软扣,余额可为负)。
|
|
func (p *Postgres) CreditEnforceEnabled(ctx context.Context) bool {
|
|
return p.GetSetting(ctx, SettingCreditEnforce) == "on"
|
|
}
|
|
|
|
// GetSetting 读一个平台设置;不存在返回空串(调用方回退默认)。
|
|
func (p *Postgres) GetSetting(ctx context.Context, key string) string {
|
|
if p.db == nil {
|
|
return ""
|
|
}
|
|
var s Setting
|
|
if err := p.db.WithContext(ctx).Select("value").Where("key = ?", key).First(&s).Error; err != nil {
|
|
return ""
|
|
}
|
|
return s.Value
|
|
}
|
|
|
|
// SetSetting 幂等写一个平台设置(key 唯一,重复即覆盖 value)。
|
|
func (p *Postgres) SetSetting(ctx context.Context, key, value string) error {
|
|
if p.db == nil {
|
|
return errStoreDisabled
|
|
}
|
|
return p.db.WithContext(ctx).Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "key"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"value", "updated_at"}),
|
|
}).Create(&Setting{Key: key, Value: value}).Error
|
|
}
|