fix: 补上未进子任务的首页、课程筛选和客户端构造
This commit is contained in:
@@ -38,10 +38,7 @@ def build_client(login_name: str, password: str, proxy: str | None = None):
|
||||
|
||||
|
||||
def _open_client(login_name: str, password: str, proxy: str | None = None):
|
||||
try:
|
||||
return build_client(login_name, password, proxy)
|
||||
except TypeError:
|
||||
return build_client(login_name, password)
|
||||
return build_client(login_name, password, proxy)
|
||||
|
||||
|
||||
def record_outcome(account: Account, valid: bool | None) -> None:
|
||||
|
||||
@@ -13,6 +13,8 @@ from sqlalchemy.orm import Session
|
||||
from euniv.client import EUNIV
|
||||
from euniv.models import Account, Course, CourseSync
|
||||
|
||||
UNCATEGORIZED = "__none__"
|
||||
|
||||
_lock = threading.Lock()
|
||||
_running = False
|
||||
_message = ""
|
||||
@@ -65,19 +67,26 @@ def course_message() -> str:
|
||||
return _message
|
||||
|
||||
|
||||
def _like_pattern(value: str) -> str:
|
||||
escaped = value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
||||
return f"%{escaped}%"
|
||||
|
||||
|
||||
def course_page(db: Session, query: str, offset: int, limit: int, kind: str = "") -> dict:
|
||||
text = query.strip()
|
||||
base = []
|
||||
if text:
|
||||
like = f"%{text}%"
|
||||
base.append(or_(Course.title.like(like), Course.contributor.like(like)))
|
||||
like = _like_pattern(text)
|
||||
base.append(or_(Course.title.like(like, escape="\\"), Course.contributor.like(like, escape="\\")))
|
||||
counts = db.execute(select(Course.kind, func.count()).where(*base).group_by(Course.kind)).all()
|
||||
kinds = [
|
||||
{"name": name or "未分类", "value": name, "count": int(count)}
|
||||
{"name": name or "未分类", "value": name if name else UNCATEGORIZED, "count": int(count)}
|
||||
for name, count in sorted(counts, key=lambda row: (_kind_rank(row[0]), row[0]))
|
||||
]
|
||||
filters = list(base)
|
||||
if kind:
|
||||
if kind == UNCATEGORIZED:
|
||||
filters.append(Course.kind == "")
|
||||
elif kind:
|
||||
filters.append(Course.kind == kind)
|
||||
total = db.scalar(select(func.count()).select_from(Course).where(*filters)) or 0
|
||||
rows = db.scalars(
|
||||
|
||||
@@ -176,10 +176,7 @@ def _client(factory, employee_id: str, password: str, account: Account | None =
|
||||
from euniv.proxy import bind_url
|
||||
|
||||
proxy = bind_url(account) if account is not None else None
|
||||
try:
|
||||
return factory(employee_id, password, proxy)
|
||||
except TypeError:
|
||||
return factory(employee_id, password)
|
||||
return factory(employee_id, password, proxy)
|
||||
|
||||
|
||||
def _switch_proxy(account: Account | None, client, exc: BaseException) -> bool:
|
||||
|
||||
@@ -365,7 +365,7 @@ class _Alive:
|
||||
|
||||
|
||||
class _Dead:
|
||||
def __init__(self, login_name: str, password: str) -> None:
|
||||
def __init__(self, login_name: str, password: str, proxy: str | None = None) -> None:
|
||||
self.login_name = login_name
|
||||
|
||||
def close(self) -> None:
|
||||
@@ -391,7 +391,7 @@ def test_refresh_records_outcome_and_skips_paused_accounts() -> None:
|
||||
_add_account("empty-user", credential=None, refreshed_at=stamped, valid=False)
|
||||
created = []
|
||||
|
||||
def factory(login_name: str, password: str):
|
||||
def factory(login_name: str, password: str, proxy: str | None = None):
|
||||
created.append(login_name)
|
||||
if login_name == "failed-user":
|
||||
return _Dead(login_name, password)
|
||||
@@ -426,8 +426,8 @@ def test_paused_account_still_updates_when_opening_home() -> None:
|
||||
_add_account("paused-user", paused=True)
|
||||
|
||||
class HomeClient(_Alive):
|
||||
def __init__(self, login_name: str, password: str) -> None:
|
||||
super().__init__(login_name, password)
|
||||
def __init__(self, login_name: str, password: str, proxy: str | None = None) -> None:
|
||||
super().__init__(login_name, password, proxy)
|
||||
self._token = "yxt-1"
|
||||
|
||||
def home_summary(self) -> dict:
|
||||
@@ -455,7 +455,7 @@ def test_check_skips_empty_credential_and_updates_saved_one() -> None:
|
||||
_add_account("saved-user")
|
||||
calls = []
|
||||
|
||||
def factory(login_name: str, password: str):
|
||||
def factory(login_name: str, password: str, proxy: str | None = None):
|
||||
calls.append(login_name)
|
||||
return _Alive(login_name, password)
|
||||
|
||||
@@ -525,7 +525,7 @@ def test_check_all_reports_each_saved_account() -> None:
|
||||
_add_account("c-user", credential=None)
|
||||
calls = []
|
||||
|
||||
def factory(login_name: str, password: str):
|
||||
def factory(login_name: str, password: str, proxy: str | None = None):
|
||||
calls.append(login_name)
|
||||
if login_name == "b-user":
|
||||
raise RuntimeError("网络失败")
|
||||
|
||||
@@ -213,7 +213,7 @@ def test_manual_jigsaw_keeps_image_pixels_and_retries() -> None:
|
||||
service_module._pending.clear()
|
||||
|
||||
class ManualClient:
|
||||
def __init__(self, login_name: str, password: str) -> None:
|
||||
def __init__(self, login_name: str, password: str, proxy: str | None = None) -> None:
|
||||
self.login_name = login_name
|
||||
self.password = password
|
||||
self._profile = {
|
||||
|
||||
@@ -100,5 +100,56 @@ def test_course_page_filters_by_title() -> None:
|
||||
assert {item["name"] for item in filtered["kinds"]} == {"课程包", "文档"}
|
||||
|
||||
|
||||
def test_uncategorized_filter_and_like_escape() -> None:
|
||||
from euniv.courses import UNCATEGORIZED, course_page
|
||||
from euniv.db import SessionLocal
|
||||
from euniv.models import Course
|
||||
|
||||
with SessionLocal() as db:
|
||||
db.add(Course(
|
||||
path="/kng/view/document/plain.html",
|
||||
title="100%完成",
|
||||
kind="",
|
||||
contributor="甲",
|
||||
recommend="0",
|
||||
learners="1",
|
||||
released_on="2026-01-01",
|
||||
updated_on="2026-01-01",
|
||||
comments="0",
|
||||
))
|
||||
db.add(Course(
|
||||
path="/kng/view/document/other.html",
|
||||
title="axb",
|
||||
kind="文档",
|
||||
contributor="乙",
|
||||
recommend="0",
|
||||
learners="1",
|
||||
released_on="2026-01-02",
|
||||
updated_on="2026-01-02",
|
||||
comments="0",
|
||||
))
|
||||
db.add(Course(
|
||||
path="/kng/view/document/underscore.html",
|
||||
title="a_b",
|
||||
kind="文档",
|
||||
contributor="丙",
|
||||
recommend="0",
|
||||
learners="1",
|
||||
released_on="2026-01-03",
|
||||
updated_on="2026-01-03",
|
||||
comments="0",
|
||||
))
|
||||
db.commit()
|
||||
blank = course_page(db, "", 0, 50, kind=UNCATEGORIZED)
|
||||
assert blank["total"] == 1
|
||||
assert blank["items"][0]["title"] == "100%完成"
|
||||
kinds = {item["name"]: item["value"] for item in blank["kinds"]}
|
||||
assert kinds["未分类"] == UNCATEGORIZED
|
||||
found = course_page(db, "100%", 0, 50)
|
||||
assert [item["title"] for item in found["items"]] == ["100%完成"]
|
||||
underscore = course_page(db, "a_b", 0, 50)
|
||||
assert [item["title"] for item in underscore["items"]] == ["a_b"]
|
||||
|
||||
|
||||
def test_course_gap_stays_between_eight_and_sixteen_hours() -> None:
|
||||
assert next_gap(_FixedRandom()) == timedelta(hours=12)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { computed, onMounted, onUnmounted, ref } from "vue";
|
||||
import { NAlert, NButton, NIcon, NInput, NPagination, NTooltip } from "naive-ui";
|
||||
import { DownloadOutline } from "@vicons/ionicons5";
|
||||
import { formatTime, isRecord, textOf } from "../../shared/text";
|
||||
|
||||
interface CourseRow {
|
||||
title: string;
|
||||
@@ -35,14 +36,6 @@ const kindTotal = computed(() => kinds.value.reduce((sum, item) => sum + item.co
|
||||
const fetching = ref(false);
|
||||
let poll = 0;
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function textOf(value: unknown) {
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
function asCourse(value: unknown): CourseRow | null {
|
||||
if (!isRecord(value) || typeof value.title !== "string") return null;
|
||||
return {
|
||||
@@ -66,14 +59,6 @@ function courseKey(row: CourseRow, index: number) {
|
||||
return first === index ? base : `${base}#${index}`;
|
||||
}
|
||||
|
||||
function formatTime(value: string | null) {
|
||||
if (!value) return "";
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) return value;
|
||||
const pad = (part: number) => String(part).padStart(2, "0");
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
|
||||
}
|
||||
|
||||
async function load(clamp = true) {
|
||||
if (items.value.length === 0) loading.value = true;
|
||||
try {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { computed, onMounted, onUnmounted, ref } from "vue";
|
||||
import { NAlert, NButton, NIcon, NInput, NPagination, NSpin, NTooltip } from "naive-ui";
|
||||
import { RefreshOutline } from "@vicons/ionicons5";
|
||||
import { formatTime, isRecord, textOf } from "../../shared/text";
|
||||
|
||||
interface ProxyRow {
|
||||
id: number;
|
||||
@@ -51,14 +52,6 @@ const kinds = computed<StatusOption[]>(() => [
|
||||
const kindTotal = computed(() => alive.value + dead.value + pending.value);
|
||||
const probeScope = computed(() => (appliedQuery.value || status.value ? "当前筛选" : "全部"));
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function textOf(value: unknown) {
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
function asRow(value: unknown): ProxyRow | null {
|
||||
if (!isRecord(value) || typeof value.id !== "number" || typeof value.host !== "string") return null;
|
||||
return {
|
||||
@@ -77,14 +70,6 @@ function asRow(value: unknown): ProxyRow | null {
|
||||
};
|
||||
}
|
||||
|
||||
function formatTime(value: string | null) {
|
||||
if (!value) return "";
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) return value;
|
||||
const pad = (part: number) => String(part).padStart(2, "0");
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
||||
}
|
||||
|
||||
function formatLatency(value: number | null) {
|
||||
if (value == null) return "—";
|
||||
return `${value} ms`;
|
||||
@@ -323,7 +308,7 @@ onUnmounted(() => stopPoll());
|
||||
<header class="page-head">
|
||||
<p v-if="!loading">
|
||||
可用 {{ alive }} · 失效 {{ dead }}
|
||||
<template v-if="fetchedAt"> · 更新于 {{ formatTime(fetchedAt) }}</template>
|
||||
<template v-if="fetchedAt"> · 更新于 {{ formatTime(fetchedAt, true) }}</template>
|
||||
</p>
|
||||
<form class="course-search" @submit.prevent="search">
|
||||
<n-tooltip>
|
||||
@@ -393,7 +378,7 @@ onUnmounted(() => stopPoll());
|
||||
<td :class="statusClass(row)" :title="row.note || undefined">
|
||||
{{ row.status_text }}<template v-if="row.note"> · {{ row.note }}</template>
|
||||
</td>
|
||||
<td class="when">{{ formatTime(row.last_checked_at) || "—" }}</td>
|
||||
<td class="when">{{ formatTime(row.last_checked_at, true) || "—" }}</td>
|
||||
<td>
|
||||
<n-button
|
||||
size="tiny"
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NCheckbox, NDropdown, NForm, NFormItem, NIcon, NInput, NInputNumber, NModal, NRadioButton, NRadioGroup, NSelect, NTimePicker, NTooltip, useDialog, type DropdownOption } from "naive-ui";
|
||||
import { NAlert, NButton, NCheckbox, NDropdown, NForm, NFormItem, NIcon, NInputNumber, NModal, NRadioButton, NRadioGroup, NSelect, NTimePicker, NTooltip, useDialog, type DropdownOption } from "naive-ui";
|
||||
import { EllipsisHorizontalOutline, EllipsisVerticalOutline, PauseOutline, PlayOutline, SettingsOutline, WarningOutline } from "@vicons/ionicons5";
|
||||
import ProxyFields from "../../shared/ProxyFields.vue";
|
||||
import { dayOptions, workerOptions } from "../../shared/studyFields";
|
||||
import { isRecord, textOf } from "../../shared/text";
|
||||
|
||||
interface UserRow {
|
||||
employee_id: string;
|
||||
@@ -69,8 +71,6 @@ const proxyPassword = ref("");
|
||||
const proxyHasPassword = ref(false);
|
||||
const proxyManual = computed(() => proxyScheme.value !== "off" && proxyScheme.value !== "pool" && proxyScheme.value !== "keep");
|
||||
const scheduleTimeDisabled = computed(() => scheduleChoice.value === "off");
|
||||
const proxyAccountProps = { autocomplete: "off", name: "proxy-account" };
|
||||
const proxyPasswordProps = { autocomplete: "new-password", name: "proxy-password" };
|
||||
const proxyOptions = [
|
||||
{ label: "保持不变", value: "keep" },
|
||||
{ label: "关闭", value: "off" },
|
||||
@@ -139,19 +139,11 @@ const startTime = computed({
|
||||
},
|
||||
});
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function detailOf(payload: unknown, fallback: string) {
|
||||
if (isRecord(payload) && typeof payload.detail === "string" && payload.detail) return payload.detail;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function textOf(value: unknown) {
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
function readValid(value: unknown): boolean | null {
|
||||
if (value === true) return true;
|
||||
if (value === false) return false;
|
||||
@@ -926,29 +918,17 @@ onMounted(() => {
|
||||
<n-form-item label="同时学习课程数">
|
||||
<n-select class="worker-select" v-model:value="workersValue" :options="workerOptions" placeholder="保持不变" />
|
||||
</n-form-item>
|
||||
<n-form-item label="代理类型">
|
||||
<n-select v-model:value="proxyScheme" :options="proxyOptions" />
|
||||
</n-form-item>
|
||||
<template v-if="proxyManual">
|
||||
<n-form-item label="代理地址">
|
||||
<n-input v-model:value="proxyHost" placeholder="例如 127.0.0.1" />
|
||||
</n-form-item>
|
||||
<n-form-item label="代理端口">
|
||||
<n-input-number v-model:value="proxyPort" button-placement="both" :min="1" :max="65535" :precision="0" placeholder="请输入" />
|
||||
</n-form-item>
|
||||
<n-form-item label="代理账号">
|
||||
<n-input v-model:value="proxyUser" placeholder="可不填" :input-props="proxyAccountProps" />
|
||||
</n-form-item>
|
||||
<n-form-item label="代理密码">
|
||||
<n-input
|
||||
v-model:value="proxyPassword"
|
||||
type="password"
|
||||
show-password-on="click"
|
||||
:placeholder="settingsIds.length === 1 && proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||||
:input-props="proxyPasswordProps"
|
||||
/>
|
||||
</n-form-item>
|
||||
</template>
|
||||
<ProxyFields
|
||||
v-model:scheme="proxyScheme"
|
||||
v-model:host="proxyHost"
|
||||
v-model:port="proxyPort"
|
||||
v-model:username="proxyUser"
|
||||
v-model:password="proxyPassword"
|
||||
:options="proxyOptions"
|
||||
:manual="proxyManual"
|
||||
label-prefix="代理"
|
||||
:password-placeholder="settingsIds.length === 1 && proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||||
/>
|
||||
<div class="modal-actions">
|
||||
<n-button attr-type="button" @click="settingsOpen = false">取消</n-button>
|
||||
<n-button attr-type="submit" type="primary" :loading="settingsSaving" :disabled="!settingsReady">保存</n-button>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NIcon, NModal, NTooltip } from "naive-ui";
|
||||
import { RefreshOutline } from "@vicons/ionicons5";
|
||||
import { failureText } from "../../shared/text";
|
||||
|
||||
interface ExamItem {
|
||||
name: string;
|
||||
@@ -45,13 +46,6 @@ let detailRequest = 0;
|
||||
let refreshRunning = false;
|
||||
let busy = false;
|
||||
|
||||
function failureText(payload: { detail?: unknown }, fallback: string) {
|
||||
const detail = payload.detail;
|
||||
if (typeof detail !== "string" || !detail.trim()) return fallback;
|
||||
if (/traceback|exception|sqlite|httpx|errno|timeout|error:/i.test(detail)) return fallback;
|
||||
return detail;
|
||||
}
|
||||
|
||||
async function load() {
|
||||
if (busy) return;
|
||||
busy = true;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
|
||||
import { NAlert, NButton, NForm, NFormItem, NIcon, NInput, NInputNumber, NModal, NProgress, NSelect, NSwitch, NTimePicker, NTooltip } from "naive-ui";
|
||||
import { NAlert, NButton, NForm, NFormItem, NIcon, NInputNumber, NModal, NProgress, NSelect, NSwitch, NTimePicker, NTooltip } from "naive-ui";
|
||||
import { GlobeOutline, PlayOutline, RefreshOutline, StopOutline, WarningOutline } from "@vicons/ionicons5";
|
||||
import ProxyFields from "../../shared/ProxyFields.vue";
|
||||
import { dayOptions, workerOptions } from "../../shared/studyFields";
|
||||
import { failureText } from "../../shared/text";
|
||||
|
||||
interface HomeSummary {
|
||||
name: string;
|
||||
@@ -105,7 +107,6 @@ const studyLabel = computed(() => {
|
||||
if (state === "学习中") return "正在领取课程";
|
||||
if (studyControl.value.active || studyControl.value.started) return "正在启动学习任务";
|
||||
if (!studyControl.value.schedule_enabled) return "未开启定时自动学习";
|
||||
if (message && !message.startsWith("下一次") && !message.startsWith("运行中")) return message;
|
||||
return "未到开始时间";
|
||||
});
|
||||
|
||||
@@ -143,13 +144,6 @@ const facts = computed(() => {
|
||||
];
|
||||
});
|
||||
|
||||
function failureText(payload: { detail?: unknown }, fallback: string) {
|
||||
const detail = payload.detail;
|
||||
if (typeof detail !== "string" || !detail.trim()) return fallback;
|
||||
if (/traceback|exception|sqlite|httpx|errno|timeout|error:/i.test(detail)) return fallback;
|
||||
return detail;
|
||||
}
|
||||
|
||||
function asSummary(value: unknown): HomeSummary | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
const row = value as Record<string, unknown>;
|
||||
@@ -429,8 +423,6 @@ function restoreProxy() {
|
||||
proxyHasPassword.value = savedProxy.hasPassword;
|
||||
proxyError.value = "";
|
||||
}
|
||||
const proxyAccountProps = { autocomplete: "off", name: "proxy-account" };
|
||||
const proxyPasswordProps = { autocomplete: "new-password", name: "proxy-password" };
|
||||
const proxyOptions = [
|
||||
{ label: "关闭", value: "off" },
|
||||
{ label: "自动", value: "pool" },
|
||||
@@ -928,30 +920,17 @@ onUnmounted(() => {
|
||||
>
|
||||
<n-alert v-if="proxyError" class="summary-error" type="error">{{ proxyError }}</n-alert>
|
||||
<n-form class="compact-nform" label-placement="left" label-align="left" :label-width="72" :show-feedback="false" size="small">
|
||||
<n-form-item label="类型">
|
||||
<n-select v-model:value="proxyScheme" size="small" :options="proxyOptions" />
|
||||
</n-form-item>
|
||||
<template v-if="proxyManual">
|
||||
<n-form-item label="地址">
|
||||
<n-input v-model:value="proxyHost" size="small" placeholder="例如 127.0.0.1" />
|
||||
</n-form-item>
|
||||
<n-form-item label="端口">
|
||||
<n-input-number v-model:value="proxyPort" size="small" button-placement="both" :min="1" :max="65535" :precision="0" placeholder="请输入" />
|
||||
</n-form-item>
|
||||
<n-form-item label="账号">
|
||||
<n-input v-model:value="proxyUser" size="small" placeholder="可不填" :input-props="proxyAccountProps" />
|
||||
</n-form-item>
|
||||
<n-form-item label="密码">
|
||||
<n-input
|
||||
v-model:value="proxyPassword"
|
||||
size="small"
|
||||
type="password"
|
||||
show-password-on="click"
|
||||
:placeholder="proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||||
:input-props="proxyPasswordProps"
|
||||
/>
|
||||
</n-form-item>
|
||||
</template>
|
||||
<ProxyFields
|
||||
v-model:scheme="proxyScheme"
|
||||
v-model:host="proxyHost"
|
||||
v-model:port="proxyPort"
|
||||
v-model:username="proxyUser"
|
||||
v-model:password="proxyPassword"
|
||||
:options="proxyOptions"
|
||||
:manual="proxyManual"
|
||||
size="small"
|
||||
:password-placeholder="proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||||
/>
|
||||
</n-form>
|
||||
<template #footer>
|
||||
<div class="modal-actions">
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NIcon, NModal, NTooltip } from "naive-ui";
|
||||
import { RefreshOutline } from "@vicons/ionicons5";
|
||||
import { failureText } from "../../shared/text";
|
||||
|
||||
interface TrainingItem {
|
||||
name: string;
|
||||
@@ -61,13 +62,6 @@ function trainingMeta(item: TrainingItem) {
|
||||
return parts.join(" · ");
|
||||
}
|
||||
|
||||
function failureText(payload: { detail?: unknown }, fallback: string) {
|
||||
const detail = payload.detail;
|
||||
if (typeof detail !== "string" || !detail.trim()) return fallback;
|
||||
if (/traceback|exception|sqlite|httpx|errno|timeout|error:/i.test(detail)) return fallback;
|
||||
return detail;
|
||||
}
|
||||
|
||||
async function load() {
|
||||
if (busy) return;
|
||||
busy = true;
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
<script setup lang="ts">
|
||||
import { NFormItem, NInput, NInputNumber, NSelect } from "naive-ui";
|
||||
|
||||
const scheme = defineModel<string>("scheme", { required: true });
|
||||
const host = defineModel<string>("host", { required: true });
|
||||
const port = defineModel<number | null>("port", { required: true });
|
||||
const username = defineModel<string>("username", { required: true });
|
||||
const password = defineModel<string>("password", { required: true });
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
options: { label: string; value: string }[];
|
||||
manual: boolean;
|
||||
size?: "small" | "medium";
|
||||
labelPrefix?: string;
|
||||
passwordPlaceholder: string;
|
||||
}>(),
|
||||
{ size: "medium", labelPrefix: "" },
|
||||
);
|
||||
|
||||
const accountProps = { autocomplete: "off", name: "proxy-account" };
|
||||
const passwordProps = { autocomplete: "new-password", name: "proxy-password" };
|
||||
|
||||
function label(name: string) {
|
||||
return `${props.labelPrefix}${name}`;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<n-form-item :label="label('类型')">
|
||||
<n-select v-model:value="scheme" :size="size" :options="options" />
|
||||
</n-form-item>
|
||||
<template v-if="manual">
|
||||
<n-form-item :label="label('地址')">
|
||||
<n-input v-model:value="host" :size="size" placeholder="例如 127.0.0.1" />
|
||||
</n-form-item>
|
||||
<n-form-item :label="label('端口')">
|
||||
<n-input-number v-model:value="port" :size="size" button-placement="both" :min="1" :max="65535" :precision="0" placeholder="请输入" />
|
||||
</n-form-item>
|
||||
<n-form-item :label="label('账号')">
|
||||
<n-input v-model:value="username" :size="size" placeholder="可不填" :input-props="accountProps" />
|
||||
</n-form-item>
|
||||
<n-form-item :label="label('密码')">
|
||||
<n-input
|
||||
v-model:value="password"
|
||||
:size="size"
|
||||
type="password"
|
||||
show-password-on="click"
|
||||
:placeholder="passwordPlaceholder"
|
||||
:input-props="passwordProps"
|
||||
/>
|
||||
</n-form-item>
|
||||
</template>
|
||||
</template>
|
||||
@@ -0,0 +1,24 @@
|
||||
export function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export function textOf(value: unknown) {
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
export function failureText(payload: { detail?: unknown }, fallback: string) {
|
||||
const detail = payload.detail;
|
||||
if (typeof detail !== "string" || !detail.trim()) return fallback;
|
||||
if (/traceback|exception|sqlite|httpx|errno|timeout|error:/i.test(detail)) return fallback;
|
||||
return detail;
|
||||
}
|
||||
|
||||
export function formatTime(value: string | null, withSeconds = false) {
|
||||
if (!value) return "";
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) return value;
|
||||
const pad = (part: number) => String(part).padStart(2, "0");
|
||||
const clock = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
|
||||
const seconds = withSeconds ? `:${pad(date.getSeconds())}` : "";
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${clock}${seconds}`;
|
||||
}
|
||||
Reference in New Issue
Block a user