feat(admin): 微信用户观测页 + 唯一昵称 + 密钥明文回显(单管理员后台)
按需求四项: 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>
This commit is contained in:
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io"
|
||||
@@ -84,6 +85,20 @@ func (h *Handler) accessToken(ctx context.Context, cfg wechat.Config) (string, e
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// wechatNickname 从 openid 派生稳定昵称:微信用户_XXXXX。
|
||||
// 后缀取 openid 的 sha1 前 5 位、映射到去混淆字母表 —— openid 唯一 → 后缀实际不重复,
|
||||
// 且同一 openid 每次一致(重登不会换名)。用户可事后自己改名。
|
||||
func wechatNickname(openID string) string {
|
||||
const alphabet = "ABCDEFGHJKMNPQRSTUVWXYZ23456789" // 去掉 0/O、1/I/L 等易混
|
||||
sum := sha1.Sum([]byte(openID))
|
||||
var sb strings.Builder
|
||||
sb.WriteString("微信用户_")
|
||||
for i := 0; i < 5; i++ {
|
||||
sb.WriteByte(alphabet[int(sum[i])%len(alphabet)])
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func newTicket() string {
|
||||
b := make([]byte, 16)
|
||||
_, _ = rand.Read(b)
|
||||
@@ -157,7 +172,7 @@ func (h *Handler) WxMPEvent(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if u == nil {
|
||||
u, err = h.db.CreateWechatUser(ctx, openID, "微信用户")
|
||||
u, err = h.db.CreateWechatUser(ctx, openID, wechatNickname(openID))
|
||||
if err != nil {
|
||||
log.Printf("[wxlogin] 建微信用户失败 openid=%s: %v", openID, err)
|
||||
c.String(http.StatusOK, "success")
|
||||
@@ -210,11 +225,12 @@ func (h *Handler) WxMPPoll(c *gin.Context) {
|
||||
|
||||
func (h *Handler) AdminGetWechatMP(c *gin.Context) {
|
||||
cfg := h.loadWechatMP(c.Request.Context())
|
||||
// 单管理员后台:AppSecret 明文回显(RequireAdmin 已拦,方便核对/复制)。
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"appid": cfg.AppID,
|
||||
"token": cfg.Token,
|
||||
"has_app_secret": cfg.AppSecret != "",
|
||||
"enabled": cfg.Enabled(),
|
||||
"appid": cfg.AppID,
|
||||
"token": cfg.Token,
|
||||
"app_secret": cfg.AppSecret,
|
||||
"enabled": cfg.Enabled(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -247,3 +263,8 @@ func (h *Handler) AdminSaveWechatMP(c *gin.Context) {
|
||||
h.cache.WxTokenSet(ctx, cfg.AppID, "", time.Millisecond) // 换密钥→旧 token 作废
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok", "enabled": cfg.Enabled()})
|
||||
}
|
||||
|
||||
// AdminWechatUsers: GET /api/v1/admin/wechat-users —— 后台微信用户列表(openid/昵称/加入时间/余额)。
|
||||
func (h *Handler) AdminWechatUsers(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"users": h.db.ListWechatUsers(c.Request.Context(), 200)})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user