初始提交:5G 基站退服时长智能分析与故障闭环管控系统
Flask + SQLite 后端,免构建 React + ECharts 前端,含合成样例数据。 测试参考数据(FOR-TEST-ONLY)与运行时数据库不入库。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
f8d1ccc526
39 files changed
+29010
No files matched your search
@@ -0,0 +1,64 @@
|
||||
/* 与 Flask /api 通信。所有请求都经过 api(),页面取数用 useFetch()。 */
|
||||
import {createContext, useContext, useEffect, useState, useToast} from 'vendor';
|
||||
|
||||
/** 对象 -> 查询串,自动丢弃空值。qs({a: 1, b: ''}) === 'a=1' */
|
||||
export function qs(params = {}) {
|
||||
const pairs = Object.entries(params).filter(([, v]) => v !== '' && v != null && v !== false);
|
||||
return new URLSearchParams(pairs).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送请求并返回 JSON;失败时抛出 Error(err.status 为 HTTP 状态码)。
|
||||
* api('/api/meta')
|
||||
* api('/api/outages', {json: {...}}) // POST JSON
|
||||
* api('/api/outages/1', {method: 'PUT', json: {}})
|
||||
* api('/api/import/preview', {body: formData}) // 上传文件
|
||||
*/
|
||||
export async function api(url, {json, method, body} = {}) {
|
||||
const init = {method: method || (json || body ? 'POST' : 'GET'), body};
|
||||
if (json) {
|
||||
init.headers = {'Content-Type': 'application/json'};
|
||||
init.body = JSON.stringify(json);
|
||||
}
|
||||
const res = await fetch(url, init);
|
||||
const data = await res.json().catch(() => ({error: '服务器响应异常'}));
|
||||
if (!res.ok || data.error) throw Object.assign(new Error(data.error || '请求失败'), {status: res.status});
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面取数:url 或参数变化时自动重新请求,出错时弹出提示。
|
||||
* const {data, reload} = useFetch('/api/dashboard', filters);
|
||||
* data 在首次加载完成前为 null;url 传 null 时不发请求。
|
||||
*/
|
||||
export function useFetch(url, params) {
|
||||
const toast = useToast();
|
||||
const [data, setData] = useState(null);
|
||||
const [version, setVersion] = useState(0);
|
||||
const query = qs(params);
|
||||
|
||||
useEffect(() => {
|
||||
if (!url) return;
|
||||
let current = true; // 参数快速变化时,丢弃过期请求的结果
|
||||
api(query ? `${url}?${query}` : url).then(
|
||||
result => current && setData(result),
|
||||
err => current && toast({message: err.message, tone: 'error'}));
|
||||
return () => { current = false; };
|
||||
}, [url, query, version]);
|
||||
|
||||
return {data, reload: () => setVersion(v => v + 1)};
|
||||
}
|
||||
|
||||
/** 浏览器原生下载(导出 Excel) */
|
||||
export function download(url, params) {
|
||||
location.href = params ? `${url}?${qs(params)}` : url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局上下文,任意页面通过 const {meta, isAdmin, filters} = useApp() 读取:
|
||||
* meta 下拉选项(场景、站型、根因、月份、片区、厂家、状态)、当前用户、数据概况
|
||||
* isAdmin 是否管理员
|
||||
* filters 顶栏全局筛选条件
|
||||
*/
|
||||
export const AppContext = createContext(null);
|
||||
export const useApp = () => useContext(AppContext);
|
||||
Reference in new issue
Block a user