webadmin系统设置
This commit is contained in:
@@ -20,7 +20,6 @@ const JSAPI_TICKET_KEY = 'wechat:jsapi_ticket';
|
||||
@Injectable()
|
||||
export class WechatApiProvider implements IWechatProvider {
|
||||
private readonly logger = new Logger(WechatApiProvider.name);
|
||||
private readonly config = loadAppConfig();
|
||||
private readonly appId = process.env.WX_APP_ID ?? '';
|
||||
private readonly appSecret = process.env.WX_APP_SECRET ?? '';
|
||||
/** 小程序独立凭证;未配置时回退公众号/H5 的 WX_APP_ID(须与开发者工具 appid 一致) */
|
||||
@@ -39,7 +38,8 @@ export class WechatApiProvider implements IWechatProvider {
|
||||
) {}
|
||||
|
||||
isEnabled() {
|
||||
return this.config.wechatAuthEnabled && !!this.appId && !!this.appSecret;
|
||||
const cfg = loadAppConfig();
|
||||
return !cfg.mockWechat && !!this.appId && !!this.appSecret;
|
||||
}
|
||||
|
||||
isMock() {
|
||||
@@ -47,8 +47,9 @@ export class WechatApiProvider implements IWechatProvider {
|
||||
}
|
||||
|
||||
isPayEnabled() {
|
||||
const cfg = loadAppConfig();
|
||||
return (
|
||||
this.config.wechatPayEnabled &&
|
||||
!cfg.mockPay &&
|
||||
!!this.appId &&
|
||||
!!this.mchId &&
|
||||
!!this.mchSerialNo &&
|
||||
@@ -290,7 +291,7 @@ export class WechatApiProvider implements IWechatProvider {
|
||||
}) {
|
||||
if (!this.isPayEnabled()) {
|
||||
throw new InternalServerErrorException(
|
||||
'微信支付未配置:请设置 WECHAT_PAY_ENABLED=true、WX_MCH_ID、WX_MCH_SERIAL_NO、WX_MCH_PRIVATE_KEY、WX_API_V3_KEY',
|
||||
'微信支付未配置:请关闭 MOCK_PAY 并配置 WX_MCH_ID、WX_MCH_SERIAL_NO、WX_MCH_PRIVATE_KEY、WX_API_V3_KEY',
|
||||
);
|
||||
}
|
||||
const notifyUrl = params.notifyUrl || this.notifyUrl;
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { loadAppConfig, needsRealWechatApi } from '@dukang/shared-types';
|
||||
import type { IWechatProvider } from './wechat.interface';
|
||||
import { WechatApiProvider } from './wechat.api.provider';
|
||||
import { WechatDisabledProvider } from './wechat.disabled.provider';
|
||||
import { WechatMockProvider } from './wechat.mock.provider';
|
||||
|
||||
/** 按当前 process.env 动态选择 Mock / 真实微信 / 禁用 */
|
||||
@Injectable()
|
||||
export class WechatRouterProvider implements IWechatProvider {
|
||||
constructor(
|
||||
private readonly api: WechatApiProvider,
|
||||
private readonly disabled: WechatDisabledProvider,
|
||||
private readonly mock: WechatMockProvider,
|
||||
) {}
|
||||
|
||||
private resolve(): IWechatProvider {
|
||||
const cfg = loadAppConfig();
|
||||
if (needsRealWechatApi(cfg)) return this.api;
|
||||
if (cfg.mockWechat) return this.mock;
|
||||
return this.disabled;
|
||||
}
|
||||
|
||||
isEnabled() {
|
||||
return this.resolve().isEnabled();
|
||||
}
|
||||
|
||||
isMock() {
|
||||
return this.resolve().isMock();
|
||||
}
|
||||
|
||||
isPayEnabled() {
|
||||
return this.resolve().isPayEnabled();
|
||||
}
|
||||
|
||||
getMchId() {
|
||||
return this.resolve().getMchId();
|
||||
}
|
||||
|
||||
code2Session(code: string, actorRef?: { refType: string; refId: bigint }) {
|
||||
return this.resolve().code2Session(code, actorRef);
|
||||
}
|
||||
|
||||
oauth2AccessToken(code: string, actorRef?: { refType: string; refId: bigint }) {
|
||||
return this.resolve().oauth2AccessToken(code, actorRef);
|
||||
}
|
||||
|
||||
fetchOAuthUserInfo(
|
||||
accessToken: string,
|
||||
openId: string,
|
||||
actorRef?: { refType: string; refId: bigint },
|
||||
) {
|
||||
return this.resolve().fetchOAuthUserInfo(accessToken, openId, actorRef);
|
||||
}
|
||||
|
||||
createJssdkConfig(url: string, actorRef?: { refType: string; refId: bigint }) {
|
||||
return this.resolve().createJssdkConfig(url, actorRef);
|
||||
}
|
||||
|
||||
buildOAuthUrl(redirectUri: string, state: string, scope?: string) {
|
||||
return this.resolve().buildOAuthUrl(redirectUri, state, scope);
|
||||
}
|
||||
|
||||
getPhoneNumberByCode(
|
||||
code: string,
|
||||
platform: 'mini' | 'h5',
|
||||
actorRef?: { refType: string; refId: bigint },
|
||||
) {
|
||||
return this.resolve().getPhoneNumberByCode(code, platform, actorRef);
|
||||
}
|
||||
|
||||
createJsapiPrepay(params: {
|
||||
orderNo: string;
|
||||
description: string;
|
||||
amountFen: number;
|
||||
openId: string;
|
||||
notifyUrl: string;
|
||||
}) {
|
||||
return this.resolve().createJsapiPrepay(params);
|
||||
}
|
||||
|
||||
parsePayNotification(
|
||||
headers: Record<string, string | string[] | undefined>,
|
||||
rawBody: string,
|
||||
) {
|
||||
return this.resolve().parsePayNotification(headers, rawBody);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user