feat: 物联网卡管理
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import React, { useEffect, useRef } from 'react';
|
||||
import {
|
||||
ModalForm,
|
||||
ProForm,
|
||||
ProFormText,
|
||||
ProFormSelect,
|
||||
ProFormDateTimePicker,
|
||||
ProFormTextArea,
|
||||
} from '@ant-design/pro-components';
|
||||
import TenantSelector from '@/components/ui/TenantSelector';
|
||||
import { CARRIER_OPTIONS, ENABLED_OPTIONS } from '@/services/iotCard';
|
||||
|
||||
export type IotCardFormValues = {
|
||||
id?: number;
|
||||
cardNumber?: string;
|
||||
msisdn?: string;
|
||||
imsi?: string;
|
||||
carrier?: number;
|
||||
packageType?: string;
|
||||
enabled?: number;
|
||||
deviceSn?: string;
|
||||
activationTime?: string;
|
||||
expirationTime?: string;
|
||||
tenantId?: string;
|
||||
remark?: string;
|
||||
};
|
||||
|
||||
interface IotCardFormProps {
|
||||
title: string;
|
||||
visible: boolean;
|
||||
onVisibleChange: (v: boolean) => void;
|
||||
onFinish: (values: IotCardFormValues) => Promise<boolean>;
|
||||
initialValues?: Partial<IotCardFormValues>;
|
||||
}
|
||||
|
||||
const IotCardForm: React.FC<IotCardFormProps> = ({
|
||||
title,
|
||||
visible,
|
||||
onVisibleChange,
|
||||
onFinish,
|
||||
initialValues,
|
||||
}) => {
|
||||
const formRef = useRef<any>();
|
||||
|
||||
useEffect(() => {
|
||||
if (visible && initialValues) {
|
||||
setTimeout(() => {
|
||||
formRef.current?.setFieldsValue(initialValues);
|
||||
}, 0);
|
||||
}
|
||||
}, [visible, initialValues]);
|
||||
|
||||
return (
|
||||
<ModalForm<IotCardFormValues>
|
||||
title={title}
|
||||
visible={visible}
|
||||
formRef={formRef}
|
||||
onVisibleChange={onVisibleChange}
|
||||
onFinish={onFinish}
|
||||
modalProps={{ destroyOnClose: true }}
|
||||
initialValues={initialValues}
|
||||
>
|
||||
{/* 所属租户 */}
|
||||
<TenantSelector
|
||||
nameTenant="tenantId"
|
||||
labelTenant="所属租户"
|
||||
hideIncludeChildren
|
||||
rulesTenant={[{ required: true, message: '请选择租户' }]}
|
||||
/>
|
||||
|
||||
<ProForm.Group>
|
||||
<ProFormText
|
||||
name="cardNumber"
|
||||
label="卡号(ICCID)"
|
||||
rules={[{ required: true, message: '请输入卡号' }]}
|
||||
width="md"
|
||||
placeholder="请输入物联网卡ICCID号"
|
||||
disabled={!!initialValues?.id}
|
||||
/>
|
||||
<ProFormText name="msisdn" label="MSISDN号码" width="md" placeholder="手机号" />
|
||||
<ProFormText name="imsi" label="IMSI号码" width="md" placeholder="IMSI" />
|
||||
</ProForm.Group>
|
||||
|
||||
<ProForm.Group>
|
||||
<ProFormSelect
|
||||
name="carrier"
|
||||
label="运营商"
|
||||
options={CARRIER_OPTIONS}
|
||||
width="md"
|
||||
placeholder="请选择运营商"
|
||||
/>
|
||||
<ProFormText name="packageType" label="套餐类型" width="md" placeholder="如:月包30M" />
|
||||
<ProFormSelect
|
||||
name="enabled"
|
||||
label="启用状态"
|
||||
options={ENABLED_OPTIONS}
|
||||
width="md"
|
||||
initialValue={1}
|
||||
/>
|
||||
</ProForm.Group>
|
||||
|
||||
<ProForm.Group>
|
||||
<ProFormDateTimePicker name="activationTime" label="激活时间" width="md" />
|
||||
<ProFormDateTimePicker name="expirationTime" label="到期时间" width="md" />
|
||||
</ProForm.Group>
|
||||
|
||||
<ProForm.Group>
|
||||
<ProFormTextArea name="remark" label="备注" width="lg" />
|
||||
</ProForm.Group>
|
||||
</ModalForm>
|
||||
);
|
||||
};
|
||||
|
||||
export default IotCardForm;
|
||||
@@ -0,0 +1,127 @@
|
||||
import React, { forwardRef, useImperativeHandle, useRef, useEffect } from 'react';
|
||||
import { ProTable } from '@ant-design/pro-components';
|
||||
import type { ProColumns, ActionType } from '@ant-design/pro-components';
|
||||
import { Space, Tag, Tooltip, Popconfirm } from 'antd';
|
||||
import { EditOutlined, DeleteOutlined, LinkOutlined, DisconnectOutlined } from '@ant-design/icons';
|
||||
import { iotCardApi, CARRIER_MAP } from '@/services/iotCard';
|
||||
|
||||
export interface IotCardTableRef {
|
||||
reload: () => void;
|
||||
}
|
||||
|
||||
interface IotCardTableProps {
|
||||
params?: Record<string, any>;
|
||||
onEdit?: (record: any) => void;
|
||||
onDelete?: (record: any) => void;
|
||||
onBind?: (record: any) => void;
|
||||
onUnbind?: (record: any) => void;
|
||||
}
|
||||
|
||||
const IotCardTable = forwardRef<IotCardTableRef, IotCardTableProps>(
|
||||
({ params, onEdit, onDelete, onBind, onUnbind }, ref) => {
|
||||
const actionRef = useRef<ActionType>();
|
||||
useImperativeHandle(ref, () => ({ reload: () => actionRef.current?.reload() }));
|
||||
|
||||
// 当查询参数变化时,自动重置到第一页
|
||||
useEffect(() => {
|
||||
if (actionRef.current) {
|
||||
actionRef.current.setPageInfo?.({ current: 1 });
|
||||
actionRef.current.reload();
|
||||
}
|
||||
}, [JSON.stringify(params)]);
|
||||
|
||||
const columns: ProColumns<any>[] = [
|
||||
{ dataIndex: 'index', valueType: 'indexBorder', width: 48, fixed: 'left' },
|
||||
{ title: '卡号(ICCID)', dataIndex: 'cardNumber', copyable: true, fixed: 'left', width: 200 },
|
||||
{ title: 'MSISDN', dataIndex: 'msisdn', width: 140 },
|
||||
{ title: 'IMSI', dataIndex: 'imsi', width: 160 },
|
||||
{
|
||||
title: '运营商',
|
||||
dataIndex: 'carrier',
|
||||
width: 100,
|
||||
render: (_, record) => {
|
||||
const name = CARRIER_MAP[record.carrier];
|
||||
return name || '-';
|
||||
},
|
||||
},
|
||||
{ title: '套餐类型', dataIndex: 'packageType', width: 120, ellipsis: true },
|
||||
{
|
||||
title: '状态',
|
||||
dataIndex: 'enabled',
|
||||
width: 80,
|
||||
render: (_, record) => (
|
||||
<Tag color={record.enabled === 1 ? 'green' : 'default'}>
|
||||
{record.enabled === 1 ? '启用' : '停用'}
|
||||
</Tag>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: '绑定设备',
|
||||
dataIndex: 'deviceSn',
|
||||
width: 180,
|
||||
render: (_, record) =>
|
||||
record.deviceSn ? (
|
||||
<Tooltip title={record.deviceName || record.deviceSn}>
|
||||
<Tag color="blue">{record.deviceSn}</Tag>
|
||||
</Tooltip>
|
||||
) : (
|
||||
<Tag>未绑定</Tag>
|
||||
),
|
||||
},
|
||||
{ title: '所属租户', dataIndex: 'tenantId', width: 120 },
|
||||
{ title: '激活时间', dataIndex: 'activationTime', valueType: 'dateTime', width: 170 },
|
||||
{ title: '到期时间', dataIndex: 'expirationTime', valueType: 'dateTime', width: 170 },
|
||||
{ title: '备注', dataIndex: 'remark', ellipsis: true, width: 150 },
|
||||
{ title: '创建时间', dataIndex: 'createTime', valueType: 'dateTime', width: 170 },
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
valueType: 'option',
|
||||
width: 140,
|
||||
fixed: 'right',
|
||||
render: (_, record) => (
|
||||
<Space>
|
||||
{record.deviceSn ? (
|
||||
<Tooltip title="解绑设备">
|
||||
<a style={{ color: '#faad14' }} onClick={() => onUnbind?.(record)}>
|
||||
<DisconnectOutlined />
|
||||
</a>
|
||||
</Tooltip>
|
||||
) : (
|
||||
<Tooltip title="绑定设备">
|
||||
<a style={{ color: '#1890ff' }} onClick={() => onBind?.(record)}>
|
||||
<LinkOutlined />
|
||||
</a>
|
||||
</Tooltip>
|
||||
)}
|
||||
<Tooltip title="编辑">
|
||||
<a onClick={() => onEdit?.(record)}>
|
||||
<EditOutlined />
|
||||
</a>
|
||||
</Tooltip>
|
||||
<Tooltip title="删除">
|
||||
<a style={{ color: '#ff4d4f' }} onClick={() => onDelete?.(record)}>
|
||||
<DeleteOutlined />
|
||||
</a>
|
||||
</Tooltip>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<ProTable<any>
|
||||
rowKey="id"
|
||||
headerTitle="物联网卡列表"
|
||||
actionRef={actionRef}
|
||||
request={(p) => iotCardApi.list({ page: p.current, limit: p.pageSize, ...params })}
|
||||
columns={columns}
|
||||
search={false}
|
||||
scroll={{ x: 2000 }}
|
||||
pagination={{ showSizeChanger: true, showQuickJumper: true }}
|
||||
/>
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
export default IotCardTable;
|
||||
@@ -0,0 +1,229 @@
|
||||
import React, { useRef, useState, useCallback } from 'react';
|
||||
import { PageContainer, ProFormText, ProFormSelect, ModalForm } from '@ant-design/pro-components';
|
||||
import { Modal, message } from 'antd';
|
||||
import { PlusOutlined } from '@ant-design/icons';
|
||||
import { iotCardApi, CARRIER_OPTIONS, ENABLED_OPTIONS } from '@/services/iotCard';
|
||||
import { deviceApi } from '@/services/device';
|
||||
import IotCardTable, { IotCardTableRef } from './components/IotCardTable';
|
||||
import IotCardForm, { IotCardFormValues } from './components/IotCardForm';
|
||||
import { ActionBar, GlassCard, GradientButton, SearchBarPlus } from '@/components/ui';
|
||||
|
||||
const IotCardPage: React.FC = () => {
|
||||
const tableRef = useRef<IotCardTableRef>(null);
|
||||
const [query, setQuery] = useState<Record<string, any>>({});
|
||||
const [createOpen, setCreateOpen] = useState(false);
|
||||
const [editOpen, setEditOpen] = useState(false);
|
||||
const [current, setCurrent] = useState<any | null>(null);
|
||||
const [bindOpen, setBindOpen] = useState(false);
|
||||
const [bindRecord, setBindRecord] = useState<any | null>(null);
|
||||
const [deviceOptions, setDeviceOptions] = useState<{ label: string; value: string }[]>([]);
|
||||
const [deviceSearching, setDeviceSearching] = useState(false);
|
||||
|
||||
/** 搜索设备列表,用于绑定下拉框 */
|
||||
const fetchDeviceOptions = useCallback(async (keyword?: string) => {
|
||||
setDeviceSearching(true);
|
||||
try {
|
||||
const res = await deviceApi.deviceList({ page: 1, limit: 50, deviceSn: keyword || undefined, deviceName: keyword || undefined });
|
||||
const options = (res.data || []).map((d: any) => ({
|
||||
label: `${d.deviceSn}${d.deviceName ? ' - ' + d.deviceName : ''}`,
|
||||
value: d.deviceSn,
|
||||
}));
|
||||
setDeviceOptions(options);
|
||||
} catch {
|
||||
setDeviceOptions([]);
|
||||
} finally {
|
||||
setDeviceSearching(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
/** 新增 */
|
||||
const add = async (values: IotCardFormValues) => {
|
||||
const hide = message.loading('保存中...');
|
||||
try {
|
||||
await iotCardApi.saveOrUpdate(values as any);
|
||||
message.success('新增成功');
|
||||
tableRef.current?.reload();
|
||||
return true;
|
||||
} catch (e) {
|
||||
message.error('新增失败');
|
||||
return false;
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
};
|
||||
|
||||
/** 编辑 */
|
||||
const edit = async (values: IotCardFormValues) => {
|
||||
const hide = message.loading('保存中...');
|
||||
try {
|
||||
await iotCardApi.saveOrUpdate({ ...(current || {}), ...values } as any);
|
||||
message.success('修改成功');
|
||||
tableRef.current?.reload();
|
||||
return true;
|
||||
} catch (e) {
|
||||
message.error('修改失败');
|
||||
return false;
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
};
|
||||
|
||||
/** 删除 */
|
||||
const del = async (record: any) => {
|
||||
Modal.confirm({
|
||||
title: `确认删除物联网卡 [${record.cardNumber}]?`,
|
||||
onOk: async () => {
|
||||
const hide = message.loading('正在删除...');
|
||||
try {
|
||||
await iotCardApi.remove(record.id);
|
||||
message.success('删除成功');
|
||||
tableRef.current?.reload();
|
||||
} catch (e) {
|
||||
message.error('删除失败');
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
/** 绑定设备 */
|
||||
const handleBind = async (values: { deviceSn: string }) => {
|
||||
if (!bindRecord) return false;
|
||||
const hide = message.loading('绑定中...');
|
||||
try {
|
||||
await iotCardApi.bindDevice(bindRecord.id, values.deviceSn);
|
||||
message.success('绑定成功');
|
||||
tableRef.current?.reload();
|
||||
return true;
|
||||
} catch (e) {
|
||||
message.error('绑定失败');
|
||||
return false;
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
};
|
||||
|
||||
/** 解绑设备 */
|
||||
const handleUnbind = (record: any) => {
|
||||
Modal.confirm({
|
||||
title: `确认解绑设备 [${record.deviceSn}]?`,
|
||||
onOk: async () => {
|
||||
const hide = message.loading('解绑中...');
|
||||
try {
|
||||
await iotCardApi.unbindDevice(record.id);
|
||||
message.success('解绑成功');
|
||||
tableRef.current?.reload();
|
||||
} catch (e) {
|
||||
message.error('解绑失败');
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<PageContainer header={{ title: '物联网卡管理' }}>
|
||||
{/* 搜索栏 */}
|
||||
<SearchBarPlus
|
||||
defaultCollapsed
|
||||
split
|
||||
span={6}
|
||||
onTenantChanged={() => tableRef.current?.reload()}
|
||||
showTenant={true}
|
||||
onFinish={async (values) => setQuery(values)}
|
||||
>
|
||||
<ProFormText name="cardNumber" label="卡号" />
|
||||
<ProFormText name="msisdn" label="MSISDN" />
|
||||
<ProFormText name="deviceSn" label="设备SN" />
|
||||
<ProFormSelect name="carrier" label="运营商" options={CARRIER_OPTIONS} />
|
||||
<ProFormSelect name="enabled" label="启用状态" options={ENABLED_OPTIONS} />
|
||||
</SearchBarPlus>
|
||||
|
||||
{/* 操作栏 */}
|
||||
<ActionBar
|
||||
left={
|
||||
<GradientButton
|
||||
size="middle"
|
||||
type="primary"
|
||||
icon={<PlusOutlined />}
|
||||
onClick={() => setCreateOpen(true)}
|
||||
>
|
||||
新增
|
||||
</GradientButton>
|
||||
}
|
||||
/>
|
||||
|
||||
{/* 表格 */}
|
||||
<GlassCard>
|
||||
<IotCardTable
|
||||
ref={tableRef}
|
||||
params={query}
|
||||
onEdit={(r) => {
|
||||
setCurrent(r);
|
||||
setEditOpen(true);
|
||||
}}
|
||||
onDelete={(r) => del(r)}
|
||||
onBind={(r) => {
|
||||
setBindRecord(r);
|
||||
setBindOpen(true);
|
||||
}}
|
||||
onUnbind={(r) => handleUnbind(r)}
|
||||
/>
|
||||
</GlassCard>
|
||||
|
||||
{/* 新增表单 */}
|
||||
<IotCardForm title="新增物联网卡" visible={createOpen} onVisibleChange={setCreateOpen} onFinish={add} />
|
||||
|
||||
{/* 编辑表单 */}
|
||||
<IotCardForm
|
||||
title="编辑物联网卡"
|
||||
visible={editOpen}
|
||||
onVisibleChange={(v) => {
|
||||
setEditOpen(v);
|
||||
if (!v) setCurrent(null);
|
||||
}}
|
||||
onFinish={edit}
|
||||
initialValues={current || {}}
|
||||
/>
|
||||
|
||||
{/* 绑定设备弹窗 */}
|
||||
<ModalForm<{ deviceSn: string }>
|
||||
title={`绑定设备 - 卡号: ${bindRecord?.cardNumber || ''}`}
|
||||
visible={bindOpen}
|
||||
onVisibleChange={(v) => {
|
||||
setBindOpen(v);
|
||||
if (!v) {
|
||||
setBindRecord(null);
|
||||
setDeviceOptions([]);
|
||||
}
|
||||
if (v) {
|
||||
// 弹窗打开时加载设备列表
|
||||
fetchDeviceOptions();
|
||||
}
|
||||
}}
|
||||
onFinish={handleBind}
|
||||
modalProps={{ destroyOnClose: true }}
|
||||
>
|
||||
<ProFormSelect
|
||||
name="deviceSn"
|
||||
label="选择设备"
|
||||
rules={[{ required: true, message: '请选择要绑定的设备' }]}
|
||||
width="lg"
|
||||
placeholder="输入设备SN或名称搜索"
|
||||
showSearch
|
||||
options={deviceOptions}
|
||||
fieldProps={{
|
||||
loading: deviceSearching,
|
||||
filterOption: false,
|
||||
onSearch: (val: string) => fetchDeviceOptions(val),
|
||||
notFoundContent: deviceSearching ? '加载中...' : '暂无设备',
|
||||
}}
|
||||
/>
|
||||
</ModalForm>
|
||||
</PageContainer>
|
||||
);
|
||||
};
|
||||
|
||||
export default IotCardPage;
|
||||
@@ -0,0 +1,84 @@
|
||||
import { request } from 'umi';
|
||||
|
||||
/** 物联网卡数据项 */
|
||||
export interface IotCardItem {
|
||||
id?: number;
|
||||
cardNumber?: string;
|
||||
msisdn?: string;
|
||||
imsi?: string;
|
||||
carrier?: number;
|
||||
packageType?: string;
|
||||
enabled?: number;
|
||||
deviceSn?: string;
|
||||
deviceName?: string;
|
||||
activationTime?: string;
|
||||
expirationTime?: string;
|
||||
tenantId?: string;
|
||||
remark?: string;
|
||||
createTime?: string;
|
||||
updateTime?: string;
|
||||
}
|
||||
|
||||
/** 运营商映射 */
|
||||
export const CARRIER_MAP: Record<number, string> = {
|
||||
1: '中国移动',
|
||||
2: '中国联通',
|
||||
3: '中国电信',
|
||||
};
|
||||
|
||||
/** 运营商选项 */
|
||||
export const CARRIER_OPTIONS = [
|
||||
{ label: '中国移动', value: 1 },
|
||||
{ label: '中国联通', value: 2 },
|
||||
{ label: '中国电信', value: 3 },
|
||||
];
|
||||
|
||||
/** 启用状态选项 */
|
||||
export const ENABLED_OPTIONS = [
|
||||
{ label: '启用', value: 1 },
|
||||
{ label: '停用', value: 0 },
|
||||
];
|
||||
|
||||
export const iotCardApi = {
|
||||
/** 分页列表 */
|
||||
list: async (params: { page?: number; limit?: number; [k: string]: any }) => {
|
||||
const res = await request('/api-device/iot-card', { method: 'GET', params });
|
||||
return { data: (res as any).data, total: (res as any).count } as { data: IotCardItem[]; total: number };
|
||||
},
|
||||
|
||||
/** 查询详情 */
|
||||
getById: async (id: number) => {
|
||||
const res = await request(`/api-device/iot-card/${id}`, { method: 'GET' });
|
||||
return (res as any).data || res;
|
||||
},
|
||||
|
||||
/** 新增/更新 */
|
||||
saveOrUpdate: async (data: Partial<IotCardItem>) => {
|
||||
const res = await request('/api-device/iot-card', { method: 'POST', data });
|
||||
return (res as any).data || res;
|
||||
},
|
||||
|
||||
/** 删除 */
|
||||
remove: async (id: number) => {
|
||||
const res = await request(`/api-device/iot-card/${id}`, { method: 'DELETE' });
|
||||
return (res as any).data || res;
|
||||
},
|
||||
|
||||
/** 绑定设备 */
|
||||
bindDevice: async (id: number, deviceSn: string) => {
|
||||
const res = await request(`/api-device/iot-card/${id}/bind`, { method: 'POST', params: { deviceSn } });
|
||||
return (res as any).data || res;
|
||||
},
|
||||
|
||||
/** 解绑设备 */
|
||||
unbindDevice: async (id: number) => {
|
||||
const res = await request(`/api-device/iot-card/${id}/unbind`, { method: 'POST' });
|
||||
return (res as any).data || res;
|
||||
},
|
||||
|
||||
/** 启用/停用 */
|
||||
toggleEnabled: async (id: number, enabled: number) => {
|
||||
const res = await request(`/api-device/iot-card/${id}/toggle`, { method: 'PUT', params: { enabled } });
|
||||
return (res as any).data || res;
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user