76 lines
4.6 KiB
JavaScript
76 lines
4.6 KiB
JavaScript
// Enumerate operations rather than assuming every GET is read-only. Never accept a base URL.
|
|
export const OFFICIAL_ORIGIN = 'https://admin.zhenyangtang.com.cn'
|
|
const oneOf = (...values) => v => values.includes(v)
|
|
const int = v => /^[1-9]\d{0,14}$/.test(v) && Number.isSafeInteger(Number(v))
|
|
const text = v => typeof v === 'string' && v.length <= 120 && !/[\u0000-\u001f\u007f]/.test(v)
|
|
const day = v => /^\d{4}-\d{2}-\d{2}$/.test(v) &&
|
|
Number.isFinite(Date.parse(v)) && new Date(v).toISOString().slice(0, 10) === v
|
|
const timestamp = v => day(v) || (
|
|
/^\d{4}-\d{2}-\d{2} (?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/.test(v) && day(v.slice(0, 10))
|
|
)
|
|
const page = {page_no: int, page_size: int}
|
|
const range = {create_time_start: timestamp, create_time_end: timestamp}
|
|
const operations = new Map([
|
|
['/auth.admin/mySelf', [null, {}]],
|
|
['/order.order/orderStats', ['workbench', {days: oneOf('0','7','30'), order_type: oneOf('-1','0','1','2','3','4','5','6','7','8')}]],
|
|
['/tcm.diagnosis/assistantDiagnosisStats', ['workbench', {days: oneOf('0','7','14','30')}]],
|
|
['/order.order/lists', ['orders', {...page, ...range, order_no: text, patient_keyword: text, assistant_id: int,
|
|
order_type: oneOf('1','2','3','4','5','6','7','8'), status: oneOf('1','2','3','4','5'),
|
|
patient_association: oneOf('associated','pending')}]],
|
|
['/order.order/todayRevenue', ['orders', {}]],
|
|
['/tcm.diagnosis/getAssistants', ['orders', {}]],
|
|
['/user.user/lists', ['members', {...page, ...range, channel: oneOf('1','2','3','4','5','6'), keyword: text}]],
|
|
['/user.user/detail', ['memberDetail', {id: int}]],
|
|
['/auth.admin/lists', ['roster', {...page, role_id: oneOf('1'), exclude_disabled: oneOf('1'), apply_data_scope: oneOf('1')}]],
|
|
['/doctor.roster/lists', ['roster', {start_date: day, end_date: day, doctor_id: int}]],
|
|
['/dept.dept/all', ['deptStats', {apply_data_scope: oneOf('1')}]],
|
|
['/doctor.statistics/deptLists', ['deptStats', {time_type: oneOf('today','week','month','custom'), dept_id: int,
|
|
start_date: day, end_date: day, ...page}]]
|
|
])
|
|
|
|
function rejectQuery() { throw new Error('该请求不在只读预览的查询范围内。') }
|
|
function plain(o) {
|
|
return !!o && (Object.getPrototypeOf(o) === Object.prototype || Object.getPrototypeOf(o) === null)
|
|
}
|
|
|
|
export function prepareRequest(method, path, params = {}) {
|
|
if (!plain(params)) rejectQuery()
|
|
if (method === 'POST' && path === '/login/account') {
|
|
const {account, password, terminal} = params
|
|
if (Object.keys(params).some(k => !['account', 'password', 'terminal'].includes(k)) ||
|
|
typeof account !== 'string' || !account.trim() || !text(account) ||
|
|
typeof password !== 'string' || !password || password.length > 1024 ||
|
|
(terminal !== undefined && terminal !== 1)) rejectQuery()
|
|
// The original admin sign-in is a JSON POST with terminal=1, not a credential-bearing URL.
|
|
return {resource: null, query: null, body: JSON.stringify({account: account.trim(), password, terminal: 1})}
|
|
}
|
|
if (method !== 'GET' || !operations.has(path)) rejectQuery()
|
|
const [resource, fields] = operations.get(path)
|
|
const query = new URLSearchParams()
|
|
for (const [key, value] of Object.entries(params)) {
|
|
if (!Object.hasOwn(fields, key)) rejectQuery() // including unknown empty keys
|
|
if (value == null || value === '') continue
|
|
if (typeof value !== 'number' && typeof value !== 'string') rejectQuery()
|
|
const s = String(value)
|
|
if (!fields[key](s)) rejectQuery()
|
|
query.set(key, key === 'page_size' ? String(Math.min(Number(s), 100)) : s)
|
|
}
|
|
if (path === '/auth.admin/lists') {
|
|
if (query.get('role_id') !== '1' || query.get('exclude_disabled') !== '1') rejectQuery()
|
|
query.set('apply_data_scope', '1')
|
|
} else if (path === '/dept.dept/all') {
|
|
query.set('apply_data_scope', '1')
|
|
} else if (path === '/user.user/detail' && !query.has('id')) {
|
|
rejectQuery()
|
|
} else if (path === '/doctor.roster/lists') {
|
|
const start = query.get('start_date'), end = query.get('end_date')
|
|
if (!query.has('doctor_id') || !start || !end || end < start || Date.parse(end) - Date.parse(start) > 6 * 86400000) rejectQuery()
|
|
} else if (path === '/doctor.statistics/deptLists') {
|
|
const start = query.get('start_date'), end = query.get('end_date')
|
|
if (query.get('time_type') === 'custom') {
|
|
if (!start || !end || end < start || Date.parse(end) - Date.parse(start) > 365 * 86400000) rejectQuery()
|
|
} else if (start || end) rejectQuery()
|
|
}
|
|
return {resource, query, body: undefined}
|
|
}
|