|
|
@@ -84,20 +84,29 @@ export async function login({ base, username, password }) {
|
|
|
}
|
|
|
|
|
|
// ---------- httpJson ----------
|
|
|
-export async function httpJson(method, apiPath, body, { base, token, raw = false, json = true } = {}) {
|
|
|
- const tokenValue = token || loadToken();
|
|
|
- const headers = { 'X-Requested-With': 'XMLHttpRequest' };
|
|
|
- if (body !== undefined) headers['Content-Type'] = 'application/json';
|
|
|
- if (tokenValue) headers['X-Parse-Session-Token'] = tokenValue;
|
|
|
- let res;
|
|
|
- try {
|
|
|
- res = await fetch(baseUrl(base) + apiPath, {
|
|
|
- method: method.toUpperCase(),
|
|
|
- headers,
|
|
|
- body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
|
- });
|
|
|
- } catch (err) {
|
|
|
- fail(`无法连接服务器(${err.message})\n - 检查 SMARTBADGE_API_BASE / --base(默认线上 https://recording.sh-lami.com;本地 http://localhost:3002)\n - 以 / 开头的参数在 Git Bash 需先 export MSYS_NO_PATHCONV=1\n - 服务器是否已启动`, 3);
|
|
|
+// relogin=true 且 401 且环境有 SMARTBADGE_PASSWORD 时自动重新登录重试一次
|
|
|
+export async function httpJson(method, apiPath, body, { base, token, raw = false, json = true, relogin = false } = {}) {
|
|
|
+ const doFetch = async () => {
|
|
|
+ const tokenValue = token || loadToken();
|
|
|
+ const headers = { 'X-Requested-With': 'XMLHttpRequest' };
|
|
|
+ if (body !== undefined) headers['Content-Type'] = 'application/json';
|
|
|
+ if (tokenValue) headers['X-Parse-Session-Token'] = tokenValue;
|
|
|
+ let res;
|
|
|
+ try {
|
|
|
+ res = await fetch(baseUrl(base) + apiPath, {
|
|
|
+ method: method.toUpperCase(),
|
|
|
+ headers,
|
|
|
+ body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
|
+ });
|
|
|
+ } catch (err) {
|
|
|
+ fail(`无法连接服务器(${err.message})\n - 检查 SMARTBADGE_API_BASE / --base(默认线上 https://recording.sh-lami.com;本地 http://localhost:3002)\n - 以 / 开头的参数在 Git Bash 需先 export MSYS_NO_PATHCONV=1\n - 服务器是否已启动`, 3);
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ };
|
|
|
+ let res = await doFetch();
|
|
|
+ if (res.status === 401 && relogin && process.env.SMARTBADGE_PASSWORD) {
|
|
|
+ try { await login({ base: baseUrl(base), password: process.env.SMARTBADGE_PASSWORD }); } catch { /* 保留原 401 响应 */ }
|
|
|
+ res = await doFetch();
|
|
|
}
|
|
|
const text = await res.text();
|
|
|
if (json) {
|
|
|
@@ -107,7 +116,7 @@ export async function httpJson(method, apiPath, body, { base, token, raw = false
|
|
|
}
|
|
|
|
|
|
// ---------- 报告分页拉全 ----------
|
|
|
-export async function fetchAllReports({ base, token, type, start, end, deviceNo, pageSize = 100 } = {}) {
|
|
|
+export async function fetchAllReports({ base, token, type, start, end, deviceNo, pageSize = 100, relogin = false } = {}) {
|
|
|
const all = [];
|
|
|
let page = 1;
|
|
|
for (;;) {
|
|
|
@@ -116,7 +125,7 @@ export async function fetchAllReports({ base, token, type, start, end, deviceNo,
|
|
|
if (start) qs.set('start', start);
|
|
|
if (end) qs.set('end', end);
|
|
|
if (deviceNo) qs.set('deviceNo', deviceNo);
|
|
|
- const r = await httpJson('GET', `/api/reports?${qs}`, undefined, { base, token, raw: true });
|
|
|
+ const r = await httpJson('GET', `/api/reports?${qs}`, undefined, { base, token, raw: true, relogin });
|
|
|
const data = r.json?.data;
|
|
|
const items = data?.items ?? [];
|
|
|
all.push(...items);
|