新增默认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 { Container } from '@/components/Container';
import { GEO_IP_PROVIDER } from '@/constant'; import { GEO_IP_PROVIDER } from '@/constant';
import { disableGeoIp, getGeoIpConfig, saveGeoIpConfig } from './service'; import { getGeoIpConfig, saveGeoIpConfig } from './service';
import { WarningOutlined } from '@ant-design/icons';
import { import {
PageContainer, PageContainer,
ProCard, ProCard,
ProForm, ProForm,
ProFormSelect, ProFormDependency,
ProFormSwitch, ProFormSegmented,
} from '@ant-design/pro-components'; } from '@ant-design/pro-components';
import { useAsyncEffect } from 'ahooks'; import { useAsyncEffect } from 'ahooks';
import { App, Form, Space, Spin } from 'antd'; import { App, Form, Space, Spin } from 'antd';
import { useState } from 'react'; import React, { useState } from 'react';
import MaxMind from './components/MaxMind'; import MaxMind from './components/MaxMind';
import { useIntl } from '@umijs/max'; import { useIntl } from '@umijs/max';
@ -62,28 +61,21 @@ const tailFormItemLayout = {
}, },
}, },
}; };
const defaultProvider = GEO_IP_PROVIDER.MAXMIND;
const GeoIP = () => { const GeoIP = () => {
const [form] = Form.useForm(); const [form] = Form.useForm();
const intl = useIntl(); const intl = useIntl();
const { message, modal } = App.useApp(); const { message } = App.useApp();
const [loading, setLoading] = useState<boolean>(false); const [loading, setLoading] = useState<boolean>(false);
const [enabled, setEnabled] = useState<boolean>(false);
const [provider, setProvider] = useState<string>(defaultProvider);
useAsyncEffect(async () => { useAsyncEffect(async () => {
form.resetFields(); form.resetFields();
setLoading(true); setLoading(true);
const { success, result } = await getGeoIpConfig(); const { success, result } = await getGeoIpConfig();
if (success && result && result.enabled) { if (success && result && result.enabled) {
setEnabled(result.enabled);
setProvider(result.provider);
form.setFieldsValue({ form.setFieldsValue({
...result, ...result,
}); });
} else {
form.setFieldsValue({ provider: provider });
} }
setLoading(false); setLoading(false);
}, []); }, []);
@ -97,50 +89,7 @@ const GeoIP = () => {
})} })}
headerBordered headerBordered
bordered={false} bordered={false}
collapsed={!enabled}
style={{ marginBottom: '24px' }} 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> <Container>
<ProForm <ProForm
@ -149,32 +98,18 @@ const GeoIP = () => {
layout={'horizontal'} layout={'horizontal'}
labelAlign={'right'} labelAlign={'right'}
{...layout} {...layout}
initialValues={{ provider: defaultProvider }} initialValues={{ provider: GEO_IP_PROVIDER.DEFAULT }}
onReset={() => { onReset={() => {
form.resetFields(); form.resetFields();
form.setFieldsValue({ enabled, provider });
}} }}
submitter={{ submitter={{
render: (p, dom) => { render: (_p, dom) => {
return ( return (
<Form.Item {...tailFormItemLayout}> <Form.Item {...tailFormItemLayout}>
<Space>{dom}</Space> <Space>{dom}</Space>
</Form.Item> </Form.Item>
); );
}, },
submitButtonProps: {
style: {
// 隐藏重置按钮
display: enabled ? '' : 'none',
},
},
// 配置按钮的属性
resetButtonProps: {
style: {
// 隐藏重置按钮
display: enabled ? '' : 'none',
},
},
}} }}
onFinish={async (values) => { onFinish={async (values) => {
setLoading(true); setLoading(true);
@ -194,44 +129,52 @@ const GeoIP = () => {
} }
}} }}
> >
{enabled && ( <>
<> <ProFormSegmented
<ProFormSelect name="provider"
name="provider" label={intl.formatMessage({
label={intl.formatMessage({ id: 'pages.setting.geoip.form_select',
id: 'pages.setting.geoip.form_select', })}
})} rules={[{ required: true }]}
rules={[{ required: true }]} fieldProps={{
fieldProps={{ onChange: async (value) => {
onChange: async (value: string) => { setLoading(true);
setLoading(true); form.resetFields();
setProvider(value); form.setFieldsValue({
form.resetFields(); provider: value,
});
const { success, result } = await getGeoIpConfig();
if (success && result && result.enabled && value === result.provider) {
form.setFieldsValue({ form.setFieldsValue({
provider: value, ...result,
}); });
const { success, result } = await getGeoIpConfig(); }
if (success && result && result.enabled && value === result.provider) { setLoading(false);
setEnabled(result.enabled); },
form.setFieldsValue({ }}
...result, request={async () => {
}); return [
} {
setLoading(false); value: GEO_IP_PROVIDER.DEFAULT,
label: intl.formatMessage({
id: 'pages.setting.geoip.form_select.option.default',
}),
}, },
}}
options={[
{ {
value: GEO_IP_PROVIDER.MAXMIND, value: GEO_IP_PROVIDER.MAXMIND,
label: intl.formatMessage({ label: intl.formatMessage({
id: 'pages.setting.geoip.form_select.option.maxmind', 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> </ProForm>
</Container> </Container>
</ProCard> </ProCard>
@ -239,6 +182,5 @@ const GeoIP = () => {
</PageContainer> </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.io.IOException;
import java.util.Objects; import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.config.annotation.RefreshScope; 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.entity.setting.SettingEntity;
import cn.topiam.employee.common.geo.GeoLocationProviderConfig; import cn.topiam.employee.common.geo.GeoLocationProviderConfig;
import cn.topiam.employee.common.geo.NoneGeoLocationServiceImpl; 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.MaxmindGeoLocationServiceImpl;
import cn.topiam.employee.common.geo.maxmind.MaxmindProviderConfig; import cn.topiam.employee.common.geo.maxmind.MaxmindProviderConfig;
import cn.topiam.employee.common.jackjson.encrypt.EncryptionModule; 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.core.setting.constant.GeoIpProviderConstants;
import cn.topiam.employee.support.geo.GeoLocationService; import cn.topiam.employee.support.geo.GeoLocationService;
import static cn.topiam.employee.common.constant.ConfigBeanNameConstants.GEO_LOCATION; 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; import static cn.topiam.employee.common.geo.maxmind.MaxmindGeoLocationServiceImpl.MAXMIND;
/** /**
@ -65,16 +68,22 @@ public class EiamGeoLocationConfiguration {
// 查询数据库是否开启地理位置服务 // 查询数据库是否开启地理位置服务
SettingEntity setting = settingRepository SettingEntity setting = settingRepository
.findByName(GeoIpProviderConstants.IPADDRESS_SETTING_NAME); .findByName(GeoIpProviderConstants.IPADDRESS_SETTING_NAME);
if (!Objects.isNull(setting) if (!Objects.isNull(setting) && StringUtils.isNotBlank(setting.getValue())
&& !SettingConstants.NOT_CONFIG.equals(setting.getValue())) { && !SettingConstants.NOT_CONFIG.equals(setting.getValue())) {
GeoLocationProviderConfig provider = objectMapper.readValue(setting.getValue(), GeoLocationProviderConfig provider = objectMapper.readValue(setting.getValue(),
GeoLocationProviderConfig.class); GeoLocationProviderConfig.class);
// 如果是maxmind,下载最新的数据库文件 // maxmind
if (MAXMIND.equals(provider.getProvider())) { if (MAXMIND.equals(provider.getProvider())) {
return new MaxmindGeoLocationServiceImpl( return new MaxmindGeoLocationServiceImpl(
(MaxmindProviderConfig) provider.getConfig(), restTemplate); (MaxmindProviderConfig) provider.getConfig(), restTemplate);
} }
// ip2region
if (IP2REGION.equals(provider.getProvider())) {
return new Ip2regionGeoLocationServiceImpl();
}
} }
//没有数据默认使用 ip2region
return new Ip2regionGeoLocationServiceImpl();
} catch (IOException e) { } catch (IOException e) {
logger.error("Create geo location Exception: {}", e.getMessage(), e); logger.error("Create geo location Exception: {}", e.getMessage(), e);
} }