fix:完善后台管理页面wechat模块,修复自动配置类的加载条件等

This commit is contained in:
2025-06-02 01:13:22 +08:00
parent 44f4745b70
commit 67da3882e2
9 changed files with 319 additions and 80 deletions
@@ -61,14 +61,15 @@ public class SysWechatConfigController {
@Operation(summary = "保存微信配置")
@PostMapping
public Result<Boolean> saveConfig(@RequestBody WechatConfig config) {
config.setTenantId(TenantContextHolder.getTenant());
if (config.getId() == null) {
config.setTenantId(TenantContextHolder.getTenant());
}
return Result.succeed(wechatConfigService.saveConfig(config));
}
@Operation(summary = "更新微信配置")
@PutMapping
public Result<Boolean> updateConfig(@RequestBody WechatConfig config) {
config.setTenantId(TenantContextHolder.getTenant());
return Result.succeed(wechatConfigService.updateConfig(config));
}
@@ -20,22 +20,22 @@ public class TencentAuthenticationToken extends BaseAuthenticationToken {
private final Object principal;
private final String encryptedData;
private final String iv;
private final String appId;
private final String appCode;
public TencentAuthenticationToken(String code, String encryptedData, String iv, String appId) {
public TencentAuthenticationToken(String code, String encryptedData, String iv, String appCode) {
super(GRANT_TYPE);
this.principal = code;
this.encryptedData = encryptedData;
this.iv = iv;
this.appId = appId;
this.appCode = appCode;
super.setAuthenticated(true);
}
public TencentAuthenticationToken(UserDetails user, Collection<? extends GrantedAuthority> authorities, String encryptedData, String iv, String appId) {
public TencentAuthenticationToken(UserDetails user, Collection<? extends GrantedAuthority> authorities, String encryptedData, String iv, String appCode) {
super(authorities);
this.principal = user;
this.encryptedData = encryptedData;
this.iv = iv;
this.appId = appId;
this.appCode = appCode;
}
}
@@ -19,6 +19,7 @@ import com.wechat.pay.java.service.refund.RefundService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@@ -34,6 +35,7 @@ public class WechatAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(WechatConfigService.class)
public WxMpService wxMpService(WechatConfigService configService) {
return new WxMpServiceImpl() {
@Override
@@ -49,8 +51,16 @@ public class WechatAutoConfiguration {
};
}
/**
* 这个配置类在启动时可能会遇到问题,因为:
* WechatAutoConfiguration是一个自动配置类
* 它依赖于WechatConfigService这个Feign客户端
* 如果Feign客户端还没有被创建,就会导致注入失败
* 因此使用@ConditionalOnBean注解确保配置类只在Feign客户端可用时才创建相关Bean
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(WechatConfigService.class)
public WxMaService wxMaService(WechatConfigService configService) {
return new WxMaServiceImpl() {
@Override
@@ -88,6 +98,7 @@ public class WechatAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(WechatConfigService.class)
public Config wxPayConfig(WechatConfigService configService, PrivateKeyService privateKeyService) {
WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
String privateKey = privateKeyService.getPrivateKey(getCurrentTenantId(), "PAY");
@@ -113,6 +124,7 @@ public class WechatAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(WechatConfigService.class)
public NotificationConfig notificationConfig(WechatConfigService configService, PrivateKeyService privateKeyService) {
WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
String privateKey = privateKeyService.getPrivateKey(getCurrentTenantId(), "PAY");
@@ -138,6 +150,7 @@ public class WechatAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(WechatConfigService.class)
public SettleInfo settleInfo(WechatConfigService configService) {
WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
SettleInfo settleInfo = new SettleInfo();
@@ -147,6 +160,7 @@ public class WechatAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(WechatConfigService.class)
public Executor profitSharingExecutor(WechatConfigService configService) {
WechatConfig config = configService.getConfig("PAY", getCurrentTenantId());
if (!config.getAsyncEnabled()) {
@@ -72,10 +72,10 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
String code = (String) authToken.getPrincipal();
String encryptedData = authToken.getEncryptedData();
String iv = authToken.getIv();
String appid = authToken.getAppId(); // 前端需传递
String appCode = authToken.getAppCode(); // 前端需传递
// 1. 根据appid查找配置
WechatConfig config = userDetailsServiceFactory.getService(authToken).loadMiniAppConfig(appid);
WechatConfig config = userDetailsServiceFactory.getService(authToken).loadMiniAppConfig(appCode);
if (config == null) {
throw new CustomOAuth2AuthenticationException("不支持的appid");
}
@@ -99,7 +99,7 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
throw new CustomOAuth2AuthenticationException("用户登录信息待处理,请重新登录");
}
return new TencentAuthenticationToken(userDetails, userDetails.getAuthorities(),encryptedData, iv,appid);
return new TencentAuthenticationToken(userDetails, userDetails.getAuthorities(),encryptedData, iv,appCode);
}
@Override
@@ -9,23 +9,23 @@
export default {
dev: {
'/api-uaa/': {
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-user/': {
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-log/': {
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-search/': {
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-generator/': {
@@ -34,18 +34,18 @@
changeOrigin: true,
},
'/api-file/': {
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-order/': {
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-device/': {
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
},
@@ -3,13 +3,21 @@ import {
ProForm,
ProFormText,
ProFormSelect,
ProFormSwitch,
ProFormDigit,
} from '@ant-design/pro-components';
import React from 'react';
const STATUS_OPTIONS = [
{ label: '启用', value: 1 },
{ label: '禁用', value: 0 },
] as const;
];
const TYPE_OPTIONS = [
{ label: '公众号', value: 'MP' },
{ label: '小程序', value: 'MINI' },
{ label: '支付', value: 'PAY' },
];
interface UpdateFormProps {
onSubmit: (values: any) => Promise<void>;
@@ -21,7 +29,7 @@ interface UpdateFormProps {
const UpdateForm: React.FC<UpdateFormProps> = (props) => {
return (
<ModalForm
title="修改小程序"
title="修改微信配置"
width={600}
visible={props.updateModalVisible}
onVisibleChange={props.onVisibleChange}
@@ -30,23 +38,94 @@ const UpdateForm: React.FC<UpdateFormProps> = (props) => {
modalProps={{ destroyOnClose: true }}
>
<ProForm.Group>
<ProFormText
rules={[{ required: true, message: '小程序名称不能为空' }]}
<ProFormSelect
rules={[{ required: true, message: '配置类型不能为空' }]}
width="md"
name="name"
label="小程序名称"
name="type"
label="配置类型"
options={TYPE_OPTIONS}
/>
<ProFormText
rules={[{ required: true, message: 'AppID不能为空' }]}
rules={[{ required: true, message: '商户ID不能为空' }]}
width="md"
name="appid"
label="AppID"
name="merchantId"
label="商户ID"
/>
<ProFormText
rules={[{ required: true, message: 'Secret不能为空' }]}
rules={[{ required: true, message: '应用ID不能为空' }]}
width="md"
name="secret"
label="Secret"
name="appId"
label="应用ID"
/>
<ProFormText
rules={[{ required: true, message: '应用密钥不能为空' }]}
width="md"
name="appSecret"
label="应用密钥"
/>
<ProFormText
width="md"
name="mchKey"
label="商户API密钥"
/>
<ProFormText
width="md"
name="mchSerialNo"
label="商户证书序列号"
/>
<ProFormText
width="md"
name="privateKeyPath"
label="商户私钥文件路径"
/>
<ProFormText
width="md"
name="apiV3Key"
label="APIv3密钥"
/>
<ProFormText
width="md"
name="notifyUrl"
label="支付回调地址"
/>
<ProFormText
width="md"
name="token"
label="微信公众号token"
/>
<ProFormText
width="md"
name="aesKey"
label="微信公众号aesKey"
/>
<ProFormText
width="md"
name="paymentSuccessTemplateId"
label="支付成功通知模板ID"
/>
<ProFormText
width="md"
name="videoCompleteTemplateId"
label="视频处理完成通知模板ID"
/>
<ProFormText
width="md"
name="videoFailTemplateId"
label="视频处理失败通知模板ID"
/>
<ProFormSwitch
name="profitSharingEnabled"
label="启用分账"
/>
<ProFormSwitch
name="asyncEnabled"
label="启用异步处理"
/>
<ProFormDigit
name="asyncPoolSize"
label="异步处理线程池大小"
min={1}
max={100}
/>
<ProFormSelect
rules={[{ required: true, message: '状态不能为空' }]}
@@ -55,6 +134,11 @@ const UpdateForm: React.FC<UpdateFormProps> = (props) => {
label="状态"
options={STATUS_OPTIONS}
/>
<ProFormText
width="md"
name="remark"
label="备注"
/>
</ProForm.Group>
</ModalForm>
);
@@ -1,8 +1,5 @@
import {
PlusOutlined,
ExportOutlined,
ImportOutlined,
DownloadOutlined,
} from '@ant-design/icons';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import {
@@ -11,18 +8,26 @@ import {
ProForm,
ProFormText,
ProFormSelect,
ProFormSwitch,
ProFormDigit,
ProTable,
QueryFilter,
} from '@ant-design/pro-components';
import { Button, message, Space, Tag } from 'antd';
import React, { useRef, useState } from 'react';
import { getMiniAppList, saveOrUpdateMiniApp, deleteMiniApp } from '@/services/system/api';
import { getMiniAppList, saveMiniApp, updateMiniApp, deleteMiniApp } from '@/services/system/api';
import UpdateForm from './components/UpdateForm';
const STATUS_OPTIONS = [
const STATUS_OPTIONS: { label: string; value: number }[] = [
{ label: '启用', value: 1 },
{ label: '禁用', value: 0 },
] as const;
];
const TYPE_OPTIONS: { label: string; value: string }[] = [
{ label: '公众号', value: 'MP' },
{ label: '小程序', value: 'MINI' },
{ label: '支付', value: 'PAY' },
];
const TableList: React.FC = () => {
const [searchParams, setSearchParams] = useState<Record<string, string | number>>({});
@@ -62,18 +67,23 @@ const TableList: React.FC = () => {
width: 48,
},
{
title: '小程序名称',
dataIndex: 'name',
title: '配置类型',
dataIndex: 'type',
valueType: 'select',
valueEnum: {
MP: { text: '公众号' },
MINI: { text: '小程序' },
PAY: { text: '支付' },
},
},
{
title: '商户ID',
dataIndex: 'merchantId',
copyable: true,
},
{
title: 'AppCode',
dataIndex: 'appid',
copyable: true,
},
{
title: 'AppID',
dataIndex: 'appid',
title: '应用ID',
dataIndex: 'appId',
copyable: true,
},
{
@@ -90,6 +100,16 @@ const TableList: React.FC = () => {
</Tag>
),
},
{
title: '分账功能',
dataIndex: 'profitSharingEnabled',
valueType: 'switch',
},
{
title: '异步处理',
dataIndex: 'asyncEnabled',
valueType: 'switch',
},
{
title: '创建时间',
dataIndex: 'createTime',
@@ -141,23 +161,94 @@ const TableList: React.FC = () => {
// 表单字段定义
const renderModalContent = () => (
<ProForm.Group>
<ProFormText
rules={[{ required: true, message: '小程序名称不能为空' }]}
<ProFormSelect
rules={[{ required: true, message: '配置类型不能为空' }]}
width="md"
name="name"
label="小程序名称"
name="type"
label="配置类型"
options={TYPE_OPTIONS}
/>
<ProFormText
rules={[{ required: true, message: 'AppID不能为空' }]}
rules={[{ required: true, message: '商户ID不能为空' }]}
width="md"
name="appid"
label="AppID"
name="merchantId"
label="商户ID"
/>
<ProFormText
rules={[{ required: true, message: 'Secret不能为空' }]}
rules={[{ required: true, message: '应用ID不能为空' }]}
width="md"
name="secret"
label="Secret"
name="appId"
label="应用ID"
/>
<ProFormText
rules={[{ required: true, message: '应用密钥不能为空' }]}
width="md"
name="appSecret"
label="应用密钥"
/>
<ProFormText
width="md"
name="mchKey"
label="商户API密钥"
/>
<ProFormText
width="md"
name="mchSerialNo"
label="商户证书序列号"
/>
<ProFormText
width="md"
name="privateKeyPath"
label="商户私钥文件路径"
/>
<ProFormText
width="md"
name="apiV3Key"
label="APIv3密钥"
/>
<ProFormText
width="md"
name="notifyUrl"
label="支付回调地址"
/>
<ProFormText
width="md"
name="token"
label="微信公众号token"
/>
<ProFormText
width="md"
name="aesKey"
label="微信公众号aesKey"
/>
<ProFormText
width="md"
name="paymentSuccessTemplateId"
label="支付成功通知模板ID"
/>
<ProFormText
width="md"
name="videoCompleteTemplateId"
label="视频处理完成通知模板ID"
/>
<ProFormText
width="md"
name="videoFailTemplateId"
label="视频处理失败通知模板ID"
/>
<ProFormSwitch
name="profitSharingEnabled"
label="启用分账"
/>
<ProFormSwitch
name="asyncEnabled"
label="启用异步处理"
/>
<ProFormDigit
name="asyncPoolSize"
label="异步处理线程池大小"
min={1}
max={100}
/>
<ProFormSelect
rules={[{ required: true, message: '状态不能为空' }]}
@@ -167,11 +258,16 @@ const TableList: React.FC = () => {
options={STATUS_OPTIONS}
initialValue={1}
/>
<ProFormText
width="md"
name="remark"
label="备注"
/>
</ProForm.Group>
);
return (
<PageContainer header={{ subTitle: '管理系统中的小程序配置' }}>
<PageContainer header={{ subTitle: '管理系统中的微信配置' }}>
<QueryFilter
defaultCollapsed
split
@@ -181,8 +277,9 @@ const TableList: React.FC = () => {
name="searchKey"
label="搜索"
valueEnum={{
name: '小程序名称',
appid: 'AppID',
type: '配置类型',
merchantId: '商户ID',
appId: '应用ID',
}}
/>
<ProFormText name="searchValue" placeholder="请输入关键字" />
@@ -190,15 +287,15 @@ const TableList: React.FC = () => {
<ProTable<SYSTEM.MiniApp>
rowKey="id"
headerTitle="小程序管理"
headerTitle="微信配置管理"
actionRef={actionRef}
request={async (params, sorter, filter) => {
const res = await getMiniAppList({
page: params.current,
limit: params.pageSize,
page: params.current || 1,
limit: params.pageSize || 10,
searchKey: params.searchKey,
searchValue: params.searchValue,
});
}) as any;
return {
data: res.data || [],
@@ -221,13 +318,19 @@ const TableList: React.FC = () => {
/>
<ModalForm
title="新增小程序"
title="新增微信配置"
visible={createModalVisible}
onVisibleChange={handleModalVisible}
modalProps={{ destroyOnClose: true }}
onFinish={async (values) => {
const success = await handleRequest(
() => saveOrUpdateMiniApp(values),
() => saveMiniApp({
...values,
id: currentRow?.id,
appId: values.appId,
appSecret: values.appSecret,
status: values.status,
} as SYSTEM.MiniApp),
'正在添加',
'添加成功'
);
@@ -245,7 +348,7 @@ const TableList: React.FC = () => {
<UpdateForm
onSubmit={async (values) => {
const success = await handleRequest(
() => saveOrUpdateMiniApp({ ...values, id: currentRow?.id }),
() => updateMiniApp({ ...values, id: currentRow?.id }),
'正在更新',
'更新成功'
);
@@ -254,21 +254,33 @@ export async function getMiniAppList(params: {
searchKey?: string;
searchValue?: string;
}) {
return request<API.OptionalResult<SYSTEM.MiniApp[]>>('/api-user/miniApp/list', {
return request<{
data: SYSTEM.MiniApp[];
count: number;
resp_code: number;
resp_msg?: string;
}>('/api-user/wechat/config', {
method: 'GET',
params,
});
}
export async function saveOrUpdateMiniApp(data: SYSTEM.MiniApp) {
return request<API.OptionalResult<any>>('/api-user/miniApp/saveOrUpdate', {
export async function saveMiniApp(data: SYSTEM.MiniApp) {
return request<API.OptionalResult<any>>('/api-user/wechat/config', {
method: 'POST',
data,
});
}
export async function updateMiniApp(data: SYSTEM.MiniApp) {
return request<API.OptionalResult<any>>('/api-user/wechat/config', {
method: 'PUT',
data,
});
}
export async function deleteMiniApp(id: number) {
return request<API.OptionalResult<any>>(`/api-user/miniApp/delete/${id}`, {
return request<API.OptionalResult<any>>(`/api-user/wechat/config/${id}`, {
method: 'DELETE',
});
}
@@ -104,11 +104,36 @@ declare namespace SYSTEM {
interface MiniApp {
id: number;
appid: string;
secret: string;
name: string;
tenantId?: string;
type?: string;
merchantId?: string;
appId: string;
appSecret: string;
mchKey?: string;
mchSerialNo?: string;
privateKeyPath?: string;
apiV3Key?: string;
notifyUrl?: string;
token?: string;
aesKey?: string;
paymentSuccessTemplateId?: string;
videoCompleteTemplateId?: string;
videoFailTemplateId?: string;
profitSharingEnabled?: boolean;
asyncEnabled?: boolean;
asyncPoolSize?: number;
status: number;
createTime: string;
updateTime: string;
remark?: string;
createTime?: string;
updateTime?: string;
}
}
declare namespace API {
interface OptionalResult<T> {
data: T;
count: number;
resp_code: number;
resp_msg?: string;
}
}