0f2afdaac1
按需求四项: 1. 微信新用户昵称改「微信用户_XXXXX」:后缀从 openid 的 sha1 派生(去混淆字母表), openid 唯一 → 后缀实际不重复,且同 openid 每次一致(重登不换名)。用户可自行改名。 2. 后台加「平台 → 微信用户」列表:昵称 / openid(点击复制)/ 积分余额 / 加入时间。 每人仍独立租户与积分(各买各的,确认过不共享积分池),此页只做统一观测。 3. 登录设置的 AppSecret 明文回显(不再只显示"已保存")。 4. 支付配置的 APIv3 密钥明文回显。 —— 用户明确后台单人使用、RequireAdmin 已拦,接受这一安全降级;密文仍加密入库。 修一个 gorm Scan 坑:WechatUserRow 的 WechatOpenID/BalanceMicro 没加 column tag, gorm 把 WechatOpenID 断成列名 wechat_open_id,与 SQL alias wechat_openid 对不上 → openid 静默返回空。教训:Scan 到自定义结构 + SQL 用 alias 时,字段一律显式加 column tag。 本地验证:真库造两个微信用户,列表接口正确返回 openid(修 tag 前是空); 登录设置页 AppSecret 已是可见文本框;nickname 单测覆盖唯一/稳定/去混淆。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
115 lines
4.1 KiB
Go
115 lines
4.1 KiB
Go
package handler
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"net/http"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/sundynix/sundynix-gateway/internal/payment"
|
||
"github.com/sundynix/sundynix-shared/secrets"
|
||
)
|
||
|
||
// 微信支付配置控制面:配置进 DB(sundynix_setting)、改完热生效不重启;
|
||
// APIv3 密钥入库前 AES-GCM 加密(与模型 API Key 同一把 SUNDYNIX_SECRET_KEY),
|
||
// 私钥文件留在服务器磁盘,库里只存路径。env 仍作兜底(DB 优先 → env → 隐藏)。
|
||
|
||
// SettingWechatPay 是 settings KV 里的键,值为 payment.Config 的 JSON(apiv3_key 为密文)。
|
||
const SettingWechatPay = "payment_wechat"
|
||
|
||
// loadWechatConfig 读支付配置:DB 优先(解密 apiv3),空则回退 env。
|
||
func (h *Handler) loadWechatConfig(ctx context.Context) payment.Config {
|
||
raw := h.db.GetSetting(ctx, SettingWechatPay)
|
||
if raw == "" {
|
||
return payment.ConfigFromEnv()
|
||
}
|
||
var c payment.Config
|
||
if err := json.Unmarshal([]byte(raw), &c); err != nil {
|
||
return payment.ConfigFromEnv()
|
||
}
|
||
if c.APIv3Key != "" {
|
||
if plain, err := secrets.Decrypt(c.APIv3Key); err == nil {
|
||
c.APIv3Key = plain
|
||
}
|
||
}
|
||
return c
|
||
}
|
||
|
||
// InitWechat 启动时装配微信渠道(DB 优先 → env)。失败只降级隐藏,不阻断启动。
|
||
func (h *Handler) InitWechat(ctx context.Context) {
|
||
_ = h.pay.ReloadWechat(ctx, h.loadWechatConfig(ctx))
|
||
}
|
||
|
||
// AdminGetWechatPay: GET /api/v1/admin/payment/wechat —— 当前配置(密钥不回显)+ 渠道状态。
|
||
func (h *Handler) AdminGetWechatPay(c *gin.Context) {
|
||
cfg := h.loadWechatConfig(c.Request.Context())
|
||
enabled, reason := h.pay.Status(payment.ChannelWechat)
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"config": gin.H{
|
||
"mchid": cfg.MchID,
|
||
"cert_serial": cfg.CertSerial,
|
||
"private_key_path": cfg.PrivateKeyPath,
|
||
"public_key_path": cfg.PublicKeyPath,
|
||
"public_key_id": cfg.PublicKeyID,
|
||
"appid": cfg.AppID,
|
||
"notify_url": cfg.NotifyURL,
|
||
"apiv3_key": cfg.APIv3Key, // 单管理员后台:明文回显(RequireAdmin 已拦),方便核对
|
||
},
|
||
"enabled": enabled,
|
||
"reason": reason,
|
||
})
|
||
}
|
||
|
||
// AdminSaveWechatPay: PUT /api/v1/admin/payment/wechat —— 保存配置并热重载渠道。
|
||
// apiv3_key 留空 = 沿用已存的(只写不回显的编辑语义,同模型 Key)。
|
||
func (h *Handler) AdminSaveWechatPay(c *gin.Context) {
|
||
var b struct {
|
||
MchID string `json:"mchid"`
|
||
CertSerial string `json:"cert_serial"`
|
||
PrivateKeyPath string `json:"private_key_path"`
|
||
APIv3Key string `json:"apiv3_key"`
|
||
AppID string `json:"appid"`
|
||
NotifyURL string `json:"notify_url"`
|
||
PublicKeyPath string `json:"public_key_path"`
|
||
PublicKeyID string `json:"public_key_id"`
|
||
}
|
||
if err := c.ShouldBindJSON(&b); err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
|
||
return
|
||
}
|
||
ctx := c.Request.Context()
|
||
apiv3 := strings.TrimSpace(b.APIv3Key)
|
||
if apiv3 == "" {
|
||
apiv3 = h.loadWechatConfig(ctx).APIv3Key // 留空沿用旧密钥(此处为明文,稍后统一加密入库)
|
||
}
|
||
cfg := payment.Config{
|
||
MchID: strings.TrimSpace(b.MchID),
|
||
CertSerial: strings.TrimSpace(b.CertSerial),
|
||
PrivateKeyPath: strings.TrimSpace(b.PrivateKeyPath),
|
||
APIv3Key: apiv3,
|
||
AppID: strings.TrimSpace(b.AppID),
|
||
NotifyURL: strings.TrimSpace(b.NotifyURL),
|
||
PublicKeyPath: strings.TrimSpace(b.PublicKeyPath),
|
||
PublicKeyID: strings.TrimSpace(b.PublicKeyID),
|
||
}
|
||
stored := cfg
|
||
if stored.APIv3Key != "" {
|
||
enc, err := secrets.Encrypt(stored.APIv3Key)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{"error": "密钥加密失败: " + err.Error()})
|
||
return
|
||
}
|
||
stored.APIv3Key = enc
|
||
}
|
||
raw, _ := json.Marshal(stored)
|
||
if err := h.db.SetSetting(ctx, SettingWechatPay, string(raw)); err != nil {
|
||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
// 热重载:配置有毛病只会让渠道隐藏并给出原因,不影响其它功能。
|
||
_ = h.pay.ReloadWechat(ctx, cfg)
|
||
enabled, reason := h.pay.Status(payment.ChannelWechat)
|
||
c.JSON(http.StatusOK, gin.H{"status": "ok", "enabled": enabled, "reason": reason})
|
||
}
|