feat: 调整余额支持用户搜索选择(昵称/openid前缀,不再需要手打28位完整openid)+ API前缀匹配

This commit is contained in:
2026-09-14 13:48:32 +08:00
parent 83d907f2bf
commit b5d6071a3e
2 changed files with 73 additions and 8 deletions
+8 -2
View File
@@ -1339,8 +1339,14 @@ router.post('/vpay/adjust', requirePermission('orders:write'), async (req, res)
try {
const { openid, amount, reason } = req.body
if (!openid || !amount || Math.abs(amount) > 100000) return ApiResponse.error(res, '参数无效', 400)
const user = await User.findOne({ openid })
if (!user) return ApiResponse.notFound(res, '用户不存在')
// 支持前缀匹配:输入至少8位openid前缀即可(完整openid有28位,手打太长)
let user = await User.findOne({ openid })
if (!user && openid.length >= 8) {
const matches = await User.find({ openid: { $regex: '^' + openid } }).limit(5)
if (matches.length === 1) user = matches[0]
else if (matches.length > 1) return ApiResponse.error(res, `匹配到 ${matches.length} 个用户,请输入更多字符`, 400)
}
if (!user) return ApiResponse.notFound(res, '用户不存在(支持输入openid前8位以上)')
user.vpayCoins = (user.vpayCoins || 0) + amount
await user.save()