新增默认IP地理库

pull/82/head^2
awenes 2024-04-04 21:18:53 +08:00
parent fda1cc785b
commit ca73577ace
3 changed files with 57 additions and 106 deletions

Binary file not shown.

View File

@ -17,19 +17,18 @@
*/
import { Container } from '@/components/Container';
import { GEO_IP_PROVIDER } from '@/constant';
import { disableGeoIp, getGeoIpConfig, saveGeoIpConfig } from './service';
import { WarningOutlined } from '@ant-design/icons';
import { getGeoIpConfig, saveGeoIpConfig } from './service';
import {
PageContainer,
ProCard,
ProForm,
ProFormSelect,
ProFormSwitch,
ProFormDependency,
ProFormSegmented,
} from '@ant-design/pro-components';
import { useAsyncEffect } from 'ahooks';
import { App, Form, Space, Spin } from 'antd';
import { useState } from 'react';
import React, { useState } from 'react';
import MaxMind from './components/MaxMind';
import { useIntl } from '@umijs/max';
@ -62,28 +61,21 @@ const tailFormItemLayout = {
},
},
};
const defaultProvider = GEO_IP_PROVIDER.MAXMIND;
const GeoIP = () => {
const [form] = Form.useForm();
const intl = useIntl();
const { message, modal } = App.useApp();
const { message } = App.useApp();
const [loading, setLoading] = useState<boolean>(false);
const [enabled, setEnabled] = useState<boolean>(false);
const [provider, setProvider] = useState<string>(defaultProvider);
useAsyncEffect(async () => {
form.resetFields();
setLoading(true);
const { success, result } = await getGeoIpConfig();
if (success && result && result.enabled) {
setEnabled(result.enabled);
setProvider(result.provider);
form.setFieldsValue({
...result,
});
} else {
form.setFieldsValue({ provider: provider });
}
setLoading(false);
}, []);
@ -97,50 +89,7 @@ const GeoIP = () => {
})}
headerBordered
bordered={false}
collapsed={!enabled}
style={{ marginBottom: '24px' }}
extra={
<ProFormSwitch
noStyle
labelAlign={'right'}
fieldProps={{
checked: enabled,
onChange: async (checked: boolean) => {
if (!checked) {
modal.confirm({
title: intl.formatMessage({ id: 'app.warn' }),
icon: <WarningOutlined />,
content: intl.formatMessage({
id: 'pages.setting.geoip.form.content',
}),
okText: intl.formatMessage({ id: 'app.confirm' }),
okType: 'danger',
cancelText: intl.formatMessage({ id: 'app.cancel' }),
centered: true,
onOk: async () => {
setLoading(true);
const { success } = await disableGeoIp().finally(() => {
setLoading(false);
});
if (success) {
setEnabled(checked);
message.success(intl.formatMessage({ id: 'app.operation_success' }));
setProvider(defaultProvider);
form.resetFields();
form.setFieldsValue({
provider: defaultProvider,
});
return;
}
},
});
} else {
setEnabled(checked);
}
},
}}
/>
}
>
<Container>
<ProForm
@ -149,32 +98,18 @@ const GeoIP = () => {
layout={'horizontal'}
labelAlign={'right'}
{...layout}
initialValues={{ provider: defaultProvider }}
initialValues={{ provider: GEO_IP_PROVIDER.DEFAULT }}
onReset={() => {
form.resetFields();
form.setFieldsValue({ enabled, provider });
}}
submitter={{
render: (p, dom) => {
render: (_p, dom) => {
return (
<Form.Item {...tailFormItemLayout}>
<Space>{dom}</Space>
</Form.Item>
);
},
submitButtonProps: {
style: {
// 隐藏重置按钮
display: enabled ? '' : 'none',
},
},
// 配置按钮的属性
resetButtonProps: {
style: {
// 隐藏重置按钮
display: enabled ? '' : 'none',
},
},
}}
onFinish={async (values) => {
setLoading(true);
@ -194,44 +129,52 @@ const GeoIP = () => {
}
}}
>
{enabled && (
<>
<ProFormSelect
name="provider"
label={intl.formatMessage({
id: 'pages.setting.geoip.form_select',
})}
rules={[{ required: true }]}
fieldProps={{
onChange: async (value: string) => {
setLoading(true);
setProvider(value);
form.resetFields();
<>
<ProFormSegmented
name="provider"
label={intl.formatMessage({
id: 'pages.setting.geoip.form_select',
})}
rules={[{ required: true }]}
fieldProps={{
onChange: async (value) => {
setLoading(true);
form.resetFields();
form.setFieldsValue({
provider: value,
});
const { success, result } = await getGeoIpConfig();
if (success && result && result.enabled && value === result.provider) {
form.setFieldsValue({
provider: value,
...result,
});
const { success, result } = await getGeoIpConfig();
if (success && result && result.enabled && value === result.provider) {
setEnabled(result.enabled);
form.setFieldsValue({
...result,
});
}
setLoading(false);
}
setLoading(false);
},
}}
request={async () => {
return [
{
value: GEO_IP_PROVIDER.DEFAULT,
label: intl.formatMessage({
id: 'pages.setting.geoip.form_select.option.default',
}),
},
}}
options={[
{
value: GEO_IP_PROVIDER.MAXMIND,
label: intl.formatMessage({
id: 'pages.setting.geoip.form_select.option.maxmind',
}),
},
]}
/>
{provider === GEO_IP_PROVIDER.MAXMIND && <MaxMind />}
</>
)}
];
}}
/>
<ProFormDependency name={['provider']}>
{({ provider }) => {
return provider === GEO_IP_PROVIDER.MAXMIND && <MaxMind />;
}}
</ProFormDependency>
</>
</ProForm>
</Container>
</ProCard>
@ -239,6 +182,5 @@ const GeoIP = () => {
</PageContainer>
);
};
export default () => {
return <GeoIP />;
};
export default GeoIP;

View File

@ -20,6 +20,7 @@ package cn.topiam.employee.core.configuration;
import java.io.IOException;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@ -34,6 +35,7 @@ import cn.topiam.employee.common.constant.SettingConstants;
import cn.topiam.employee.common.entity.setting.SettingEntity;
import cn.topiam.employee.common.geo.GeoLocationProviderConfig;
import cn.topiam.employee.common.geo.NoneGeoLocationServiceImpl;
import cn.topiam.employee.common.geo.ip2region.Ip2regionGeoLocationServiceImpl;
import cn.topiam.employee.common.geo.maxmind.MaxmindGeoLocationServiceImpl;
import cn.topiam.employee.common.geo.maxmind.MaxmindProviderConfig;
import cn.topiam.employee.common.jackjson.encrypt.EncryptionModule;
@ -41,6 +43,7 @@ import cn.topiam.employee.common.repository.setting.SettingRepository;
import cn.topiam.employee.core.setting.constant.GeoIpProviderConstants;
import cn.topiam.employee.support.geo.GeoLocationService;
import static cn.topiam.employee.common.constant.ConfigBeanNameConstants.GEO_LOCATION;
import static cn.topiam.employee.common.geo.ip2region.Ip2regionGeoLocationServiceImpl.IP2REGION;
import static cn.topiam.employee.common.geo.maxmind.MaxmindGeoLocationServiceImpl.MAXMIND;
/**
@ -65,16 +68,22 @@ public class EiamGeoLocationConfiguration {
// 查询数据库是否开启地理位置服务
SettingEntity setting = settingRepository
.findByName(GeoIpProviderConstants.IPADDRESS_SETTING_NAME);
if (!Objects.isNull(setting)
if (!Objects.isNull(setting) && StringUtils.isNotBlank(setting.getValue())
&& !SettingConstants.NOT_CONFIG.equals(setting.getValue())) {
GeoLocationProviderConfig provider = objectMapper.readValue(setting.getValue(),
GeoLocationProviderConfig.class);
// 如果是maxmind,下载最新的数据库文件
// maxmind
if (MAXMIND.equals(provider.getProvider())) {
return new MaxmindGeoLocationServiceImpl(
(MaxmindProviderConfig) provider.getConfig(), restTemplate);
}
// ip2region
if (IP2REGION.equals(provider.getProvider())) {
return new Ip2regionGeoLocationServiceImpl();
}
}
//没有数据默认使用 ip2region
return new Ip2regionGeoLocationServiceImpl();
} catch (IOException e) {
logger.error("Create geo location Exception: {}", e.getMessage(), e);
}