微信小程序支付遇到问题,因为没有 WX_MCH_PRIVATE_KEY

This commit is contained in:
2026-07-21 07:59:57 +08:00
parent ade68972a9
commit bc0b0cafd6
6 changed files with 129 additions and 10 deletions
@@ -1,7 +1,10 @@
import { resolve } from 'path';
import { PrismaClient } from '@prisma/client';
import { normalizePemEnv } from '../../integrations/wechat/wechat-pay.util';
import { SYSTEM_CONFIG_KEY_SET } from './system-config.registry';
const PEM_ENV_KEYS = new Set(['WX_MCH_PRIVATE_KEY', 'WX_PLATFORM_CERT']);
function apiRoot() {
return resolve(__dirname, '..', '..');
}
@@ -11,6 +14,11 @@ export function resolveEnvFilePath() {
return resolve(apiRoot(), isProduction ? '.env.production' : '.env');
}
function normalizeConfigValue(key: string, value: string): string {
if (PEM_ENV_KEYS.has(key)) return normalizePemEnv(value);
return value;
}
/** 启动前从 DB 覆盖 process.env(在 Nest 创建前调用) */
export async function preloadSystemConfigEnv(): Promise<number> {
const prisma = new PrismaClient();
@@ -18,7 +26,7 @@ export async function preloadSystemConfigEnv(): Promise<number> {
const rows = await prisma.systemConfig.findMany();
for (const row of rows) {
if (SYSTEM_CONFIG_KEY_SET.has(row.configKey)) {
process.env[row.configKey] = row.value;
process.env[row.configKey] = normalizeConfigValue(row.configKey, row.value);
}
}
return rows.length;
@@ -41,7 +49,7 @@ export async function preloadSystemConfigEnv(): Promise<number> {
export function applyEnvOverlay(values: Record<string, string>) {
for (const [key, value] of Object.entries(values)) {
if (SYSTEM_CONFIG_KEY_SET.has(key)) {
process.env[key] = value;
process.env[key] = normalizeConfigValue(key, value);
}
}
}
@@ -205,17 +205,31 @@ export class SystemConfigService implements OnModuleInit {
}
}
private normalizeByMeta(meta: { type: string }, raw: string): string {
private normalizeByMeta(meta: { key?: string; type: string }, raw: string): string {
if (meta.type === 'boolean') {
return raw === 'true' || raw === '1' ? 'true' : 'false';
}
if (meta.key === 'WX_MCH_PRIVATE_KEY' || meta.key === 'WX_PLATFORM_CERT') {
let value = raw.trim();
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1).trim();
}
return value.replace(/\\r\\n/g, '\n').replace(/\\n/g, '\n').replace(/\r\n/g, '\n').trim();
}
return raw;
}
}
function formatEnvLine(key: string, value: string): string {
if (/[\s#"'\\]/.test(value)) {
return `${key}="${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}
return `${key}=${value}`;
const needsQuote = /[\s#"'\\]/.test(value) || value.includes('\n') || value.includes('\r');
if (!needsQuote) return `${key}=${value}`;
const escaped = value
.replace(/\\/g, '\\\\')
.replace(/\r\n/g, '\\n')
.replace(/\n/g, '\\n')
.replace(/"/g, '\\"');
return `${key}="${escaped}"`;
}