feat: 账号可配置 HTTP/HTTPS/SOCKS 代理
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NDropdown, NIcon, NModal, useDialog, type DropdownOption } from "naive-ui";
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NCheckbox, NDropdown, NIcon, NInput, NInputNumber, NModal, NSelect, useDialog, type DropdownOption } from "naive-ui";
|
||||
import { SettingsOutline } from "@vicons/ionicons5";
|
||||
|
||||
interface UserRow {
|
||||
@@ -10,6 +10,8 @@ interface UserRow {
|
||||
last_refresh_at: string | null;
|
||||
credential_valid: boolean | null;
|
||||
month_hours: string;
|
||||
has_proxy: boolean;
|
||||
proxy: string;
|
||||
}
|
||||
|
||||
interface SecretField {
|
||||
@@ -30,6 +32,27 @@ const credentialLoading = ref(false);
|
||||
const credentialError = ref("");
|
||||
const credentialNote = ref("");
|
||||
const credentialFields = ref<SecretField[]>([]);
|
||||
const picked = ref<Record<string, boolean>>({});
|
||||
const proxyOpen = ref(false);
|
||||
const proxySaving = ref(false);
|
||||
const proxyError = ref("");
|
||||
const proxyIds = ref<string[]>([]);
|
||||
const proxyScheme = ref("http");
|
||||
const proxyHost = ref("");
|
||||
const proxyPort = ref<number | null>(null);
|
||||
const proxyUser = ref("");
|
||||
const proxyPassword = ref("");
|
||||
const proxyHasPassword = ref(false);
|
||||
const proxyOptions = [
|
||||
{ label: "HTTP", value: "http" },
|
||||
{ label: "HTTPS", value: "https" },
|
||||
{ label: "SOCKS4", value: "socks4" },
|
||||
{ label: "SOCKS5", value: "socks5" },
|
||||
];
|
||||
|
||||
const pickedIds = computed(() => users.value.filter((row) => picked.value[row.employee_id]).map((row) => row.employee_id));
|
||||
const allPicked = computed(() => users.value.length > 0 && pickedIds.value.length === users.value.length);
|
||||
const somePicked = computed(() => pickedIds.value.length > 0 && !allPicked.value);
|
||||
|
||||
const tokenFields: { label: string; key: string }[] = [
|
||||
{ label: "统一认证 token", key: "ucs_token" },
|
||||
@@ -68,6 +91,8 @@ function asUser(value: unknown): UserRow | null {
|
||||
last_refresh_at: typeof value.last_refresh_at === "string" && value.last_refresh_at ? value.last_refresh_at : null,
|
||||
credential_valid: readValid(value.credential_valid),
|
||||
month_hours: textOf(value.month_hours),
|
||||
has_proxy: value.has_proxy === true,
|
||||
proxy: textOf(value.proxy),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -290,11 +315,126 @@ function confirmDelete(row: UserRow) {
|
||||
});
|
||||
}
|
||||
|
||||
function toggleAll(checked: boolean) {
|
||||
const next: Record<string, boolean> = {};
|
||||
if (checked) {
|
||||
for (const row of users.value) next[row.employee_id] = true;
|
||||
}
|
||||
picked.value = next;
|
||||
}
|
||||
|
||||
function resetProxyForm() {
|
||||
proxyScheme.value = "http";
|
||||
proxyHost.value = "";
|
||||
proxyPort.value = null;
|
||||
proxyUser.value = "";
|
||||
proxyPassword.value = "";
|
||||
proxyHasPassword.value = false;
|
||||
proxyError.value = "";
|
||||
}
|
||||
|
||||
async function openProxy(ids: string[]) {
|
||||
if (!ids.length) return;
|
||||
proxyIds.value = ids;
|
||||
resetProxyForm();
|
||||
proxyOpen.value = true;
|
||||
if (ids.length !== 1) return;
|
||||
try {
|
||||
const response = await fetch(`/api/admin/users/${encodeURIComponent(ids[0])}/proxy`);
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
if (!response.ok || !isRecord(payload)) return;
|
||||
proxyScheme.value = textOf(payload.scheme) || "http";
|
||||
proxyHost.value = textOf(payload.host);
|
||||
proxyPort.value = typeof payload.port === "number" ? payload.port : null;
|
||||
proxyUser.value = textOf(payload.username);
|
||||
proxyHasPassword.value = payload.has_password === true;
|
||||
} catch { /* 静默 */ }
|
||||
}
|
||||
|
||||
function proxyPasswordPayload() {
|
||||
if (proxyPassword.value !== "") return proxyPassword.value;
|
||||
if (!proxyUser.value.trim()) return "";
|
||||
if (proxyIds.value.length === 1 && proxyHasPassword.value) return null;
|
||||
return "";
|
||||
}
|
||||
|
||||
async function saveProxy() {
|
||||
if (proxySaving.value || !proxyIds.value.length) return;
|
||||
proxyError.value = "";
|
||||
if (!proxyHost.value.trim()) {
|
||||
proxyError.value = "请填写代理地址";
|
||||
return;
|
||||
}
|
||||
if (proxyPort.value == null || !Number.isInteger(proxyPort.value)) {
|
||||
proxyError.value = "请填写代理端口";
|
||||
return;
|
||||
}
|
||||
proxySaving.value = true;
|
||||
try {
|
||||
const response = await fetch("/api/admin/users/proxy", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
employee_ids: proxyIds.value,
|
||||
scheme: proxyScheme.value,
|
||||
host: proxyHost.value.trim(),
|
||||
port: proxyPort.value,
|
||||
username: proxyUser.value.trim(),
|
||||
password: proxyPasswordPayload(),
|
||||
}),
|
||||
});
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
proxyError.value = detailOf(payload, "没有保存。");
|
||||
return;
|
||||
}
|
||||
proxyOpen.value = false;
|
||||
await load();
|
||||
} catch {
|
||||
proxyError.value = "没有保存。";
|
||||
} finally {
|
||||
proxySaving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function clearProxy(ids: string[]) {
|
||||
if (!ids.length) return;
|
||||
errorText.value = "";
|
||||
try {
|
||||
const response = await fetch("/api/admin/users/proxy/clear", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ employee_ids: ids }),
|
||||
});
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
errorText.value = detailOf(payload, "没有清除。");
|
||||
return;
|
||||
}
|
||||
await load();
|
||||
} catch {
|
||||
errorText.value = "没有清除。";
|
||||
}
|
||||
}
|
||||
|
||||
function confirmClearProxy(ids: string[]) {
|
||||
if (!ids.length) return;
|
||||
dialog.warning({
|
||||
title: "清除代理",
|
||||
content: ids.length === 1 ? `清除工号 ${ids[0]} 的代理?` : `清除已选 ${ids.length} 个账号的代理?`,
|
||||
positiveText: "清除",
|
||||
negativeText: "取消",
|
||||
onPositiveClick: () => clearProxy(ids),
|
||||
});
|
||||
}
|
||||
|
||||
function rowActions(row: UserRow): DropdownOption[] {
|
||||
const busy = checkingAll.value || checkingId.value !== "";
|
||||
return [
|
||||
{ label: "检查", key: "check", disabled: busy },
|
||||
{ label: "查看凭据", key: "credentials" },
|
||||
{ label: "设置代理", key: "proxy" },
|
||||
{ label: "清除代理", key: "proxy-clear" },
|
||||
{ label: row.refresh_paused ? "恢复自动刷新" : "停止自动刷新", key: "refresh" },
|
||||
{ label: "删除", key: "delete", props: { style: { color: "var(--danger)" } } },
|
||||
];
|
||||
@@ -303,6 +443,8 @@ function rowActions(row: UserRow): DropdownOption[] {
|
||||
function onRowAction(row: UserRow, key: string | number) {
|
||||
if (key === "check") void checkOne(row);
|
||||
else if (key === "credentials") void openCredentials(row);
|
||||
else if (key === "proxy") void openProxy([row.employee_id]);
|
||||
else if (key === "proxy-clear") confirmClearProxy([row.employee_id]);
|
||||
else if (key === "refresh") confirmRefresh(row);
|
||||
else if (key === "delete") confirmDelete(row);
|
||||
}
|
||||
@@ -316,7 +458,11 @@ onMounted(() => {
|
||||
<section class="page admin-page">
|
||||
<header class="page-head">
|
||||
<h2>用户管理</h2>
|
||||
<n-button :loading="checkingAll" :disabled="checkingAll || checkingId !== '' || !users.length" @click="checkAll">检查全部</n-button>
|
||||
<div class="page-head-actions">
|
||||
<n-button :disabled="!pickedIds.length" @click="openProxy(pickedIds)">设置代理</n-button>
|
||||
<n-button :disabled="!pickedIds.length" @click="confirmClearProxy(pickedIds)">清除代理</n-button>
|
||||
<n-button :loading="checkingAll" :disabled="checkingAll || checkingId !== '' || !users.length" @click="checkAll">检查全部</n-button>
|
||||
</div>
|
||||
</header>
|
||||
<n-alert v-if="errorText" class="admin-alert" type="error">{{ errorText }}</n-alert>
|
||||
<div class="admin-scroll">
|
||||
@@ -324,10 +470,14 @@ onMounted(() => {
|
||||
<table v-else-if="users.length" class="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="pick">
|
||||
<n-checkbox :checked="allPicked" :indeterminate="somePicked" @update:checked="toggleAll" />
|
||||
</th>
|
||||
<th>工号</th>
|
||||
<th>凭证</th>
|
||||
<th>凭据是否有效</th>
|
||||
<th>本月学时</th>
|
||||
<th>代理</th>
|
||||
<th>上次刷新时间</th>
|
||||
<th>自动刷新</th>
|
||||
<th>操作</th>
|
||||
@@ -335,10 +485,14 @@ onMounted(() => {
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="row in users" :key="row.employee_id">
|
||||
<td class="pick">
|
||||
<n-checkbox :checked="Boolean(picked[row.employee_id])" @update:checked="(checked) => (picked = { ...picked, [row.employee_id]: checked })" />
|
||||
</td>
|
||||
<td class="employee">{{ row.employee_id }}</td>
|
||||
<td>{{ row.has_credential ? "已保存凭证" : "未保存凭证" }}</td>
|
||||
<td :class="validityClass(validityText(row))">{{ validityText(row) }}</td>
|
||||
<td class="when">{{ row.month_hours || "—" }}</td>
|
||||
<td class="when">{{ row.proxy || "—" }}</td>
|
||||
<td class="when">{{ formatRefreshTime(row.last_refresh_at) }}</td>
|
||||
<td>{{ row.refresh_paused ? "已停止" : "开启" }}</td>
|
||||
<td>
|
||||
@@ -376,5 +530,49 @@ onMounted(() => {
|
||||
</div>
|
||||
</template>
|
||||
</n-modal>
|
||||
<n-modal
|
||||
:show="proxyOpen"
|
||||
preset="card"
|
||||
:title="proxyIds.length === 1 ? `工号 ${proxyIds[0]} 的代理` : `已选 ${proxyIds.length} 个账号的代理`"
|
||||
:style="{ width: 'min(520px, calc(100vw - 32px))' }"
|
||||
@update:show="(show) => (proxyOpen = show)"
|
||||
>
|
||||
<n-alert v-if="proxyError" class="admin-alert" type="error">{{ proxyError }}</n-alert>
|
||||
<form class="settings-form" @submit.prevent="saveProxy">
|
||||
<div class="form-row">
|
||||
<label for="admin-proxy-scheme">类型</label>
|
||||
<n-select id="admin-proxy-scheme" v-model:value="proxyScheme" :options="proxyOptions" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-proxy-host">地址</label>
|
||||
<n-input id="admin-proxy-host" v-model:value="proxyHost" placeholder="例如 127.0.0.1" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-proxy-port">端口</label>
|
||||
<n-input-number id="admin-proxy-port" v-model:value="proxyPort" button-placement="both" :min="1" :max="65535" :precision="0" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-proxy-user">账号</label>
|
||||
<n-input id="admin-proxy-user" v-model:value="proxyUser" placeholder="可不填" autocomplete="off" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-proxy-password">密码</label>
|
||||
<n-input
|
||||
id="admin-proxy-password"
|
||||
v-model:value="proxyPassword"
|
||||
type="password"
|
||||
show-password-on="click"
|
||||
:placeholder="proxyIds.length === 1 && proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||||
autocomplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
<template #footer>
|
||||
<div class="modal-actions">
|
||||
<n-button @click="proxyOpen = false">取消</n-button>
|
||||
<n-button type="primary" :loading="proxySaving" @click="saveProxy">保存</n-button>
|
||||
</div>
|
||||
</template>
|
||||
</n-modal>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref, watch } from "vue";
|
||||
import { NAlert, NButton, NIcon, NInputNumber, NTooltip } from "naive-ui";
|
||||
import { NAlert, NButton, NIcon, NInput, NInputNumber, NSelect, NTooltip } from "naive-ui";
|
||||
import { RefreshOutline } from "@vicons/ionicons5";
|
||||
|
||||
interface HomeSummary {
|
||||
@@ -136,6 +136,105 @@ function saveTarget(value: number | null) {
|
||||
|
||||
watch(monthlyTarget, (v) => { if (targetLoaded.value) saveTarget(v); });
|
||||
|
||||
const proxyLoaded = ref(false);
|
||||
const proxySaving = ref(false);
|
||||
const proxyError = ref("");
|
||||
const proxyScheme = ref("http");
|
||||
const proxyHost = ref("");
|
||||
const proxyPort = ref<number | null>(null);
|
||||
const proxyUser = ref("");
|
||||
const proxyPassword = ref("");
|
||||
const proxyHasPassword = ref(false);
|
||||
const proxyOptions = [
|
||||
{ label: "HTTP", value: "http" },
|
||||
{ label: "HTTPS", value: "https" },
|
||||
{ label: "SOCKS4", value: "socks4" },
|
||||
{ label: "SOCKS5", value: "socks5" },
|
||||
];
|
||||
|
||||
async function loadProxy() {
|
||||
try {
|
||||
const res = await fetch("/api/settings/proxy");
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) return;
|
||||
proxyScheme.value = typeof data.scheme === "string" && data.scheme ? data.scheme : "http";
|
||||
proxyHost.value = typeof data.host === "string" ? data.host : "";
|
||||
proxyPort.value = typeof data.port === "number" ? data.port : null;
|
||||
proxyUser.value = typeof data.username === "string" ? data.username : "";
|
||||
proxyPassword.value = "";
|
||||
proxyHasPassword.value = data.has_password === true;
|
||||
proxyLoaded.value = true;
|
||||
} catch { /* 静默 */ }
|
||||
}
|
||||
|
||||
function proxyPasswordPayload() {
|
||||
if (proxyPassword.value !== "") return proxyPassword.value;
|
||||
if (!proxyUser.value.trim()) return "";
|
||||
return proxyHasPassword.value ? null : "";
|
||||
}
|
||||
|
||||
async function saveProxy() {
|
||||
if (proxySaving.value) return;
|
||||
proxyError.value = "";
|
||||
if (!proxyHost.value.trim()) {
|
||||
proxyError.value = "请填写代理地址";
|
||||
return;
|
||||
}
|
||||
if (proxyPort.value == null || !Number.isInteger(proxyPort.value)) {
|
||||
proxyError.value = "请填写代理端口";
|
||||
return;
|
||||
}
|
||||
proxySaving.value = true;
|
||||
try {
|
||||
const res = await fetch("/api/settings/proxy", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
scheme: proxyScheme.value,
|
||||
host: proxyHost.value.trim(),
|
||||
port: proxyPort.value,
|
||||
username: proxyUser.value.trim(),
|
||||
password: proxyPasswordPayload(),
|
||||
}),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
proxyError.value = failureText(data, "没有保存。");
|
||||
return;
|
||||
}
|
||||
proxyPassword.value = "";
|
||||
proxyHasPassword.value = data.has_password === true;
|
||||
} catch {
|
||||
proxyError.value = "没有保存。";
|
||||
} finally {
|
||||
proxySaving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function clearProxy() {
|
||||
if (proxySaving.value) return;
|
||||
proxySaving.value = true;
|
||||
proxyError.value = "";
|
||||
try {
|
||||
const res = await fetch("/api/settings/proxy", { method: "DELETE" });
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
proxyError.value = failureText(data, "没有清除。");
|
||||
return;
|
||||
}
|
||||
proxyScheme.value = "http";
|
||||
proxyHost.value = "";
|
||||
proxyPort.value = null;
|
||||
proxyUser.value = "";
|
||||
proxyPassword.value = "";
|
||||
proxyHasPassword.value = false;
|
||||
} catch {
|
||||
proxyError.value = "没有清除。";
|
||||
} finally {
|
||||
proxySaving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadStudyStatus() {
|
||||
try {
|
||||
const res = await fetch("/api/autostudy/status");
|
||||
@@ -143,7 +242,7 @@ async function loadStudyStatus() {
|
||||
} catch { /* 静默 */ }
|
||||
}
|
||||
|
||||
onMounted(() => { refresh(); loadTarget(); loadStudyStatus(); });
|
||||
onMounted(() => { refresh(); loadTarget(); loadProxy(); loadStudyStatus(); });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -214,6 +313,41 @@ onMounted(() => { refresh(); loadTarget(); loadStudyStatus(); });
|
||||
<span class="target-unit">分钟</span>
|
||||
</div>
|
||||
</div>
|
||||
<form v-if="proxyLoaded" class="home-proxy" @submit.prevent="saveProxy">
|
||||
<n-alert v-if="proxyError" class="summary-error" type="error">{{ proxyError }}</n-alert>
|
||||
<div class="form-row">
|
||||
<label for="proxy-scheme">代理类型</label>
|
||||
<n-select id="proxy-scheme" v-model:value="proxyScheme" size="small" :options="proxyOptions" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="proxy-host">代理地址</label>
|
||||
<n-input id="proxy-host" v-model:value="proxyHost" size="small" placeholder="例如 127.0.0.1" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="proxy-port">代理端口</label>
|
||||
<n-input-number id="proxy-port" v-model:value="proxyPort" size="small" button-placement="both" :min="1" :max="65535" :precision="0" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="proxy-user">代理账号</label>
|
||||
<n-input id="proxy-user" v-model:value="proxyUser" size="small" placeholder="可不填" autocomplete="off" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="proxy-password">代理密码</label>
|
||||
<n-input
|
||||
id="proxy-password"
|
||||
v-model:value="proxyPassword"
|
||||
size="small"
|
||||
type="password"
|
||||
show-password-on="click"
|
||||
:placeholder="proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||||
autocomplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
<div class="home-proxy-actions">
|
||||
<n-button type="primary" size="small" attr-type="submit" :loading="proxySaving">保存</n-button>
|
||||
<n-button size="small" attr-type="button" :disabled="proxySaving" @click="clearProxy">清除</n-button>
|
||||
</div>
|
||||
</form>
|
||||
<div v-if="studyStatus" class="home-study-status">
|
||||
<span class="study-indicator" :class="{ active: Boolean(myStudy) || studyStatus.running }"></span>
|
||||
<span v-if="myStudy" class="study-text">
|
||||
|
||||
+47
-2
@@ -434,6 +434,40 @@ select {
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.home-proxy {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
padding-top: 16px;
|
||||
border-top: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.home-proxy .form-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.home-proxy .form-row label {
|
||||
flex: none;
|
||||
width: 84px;
|
||||
font-size: 13px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.home-proxy .form-row .n-select,
|
||||
.home-proxy .form-row .n-input,
|
||||
.home-proxy .form-row .n-input-number {
|
||||
flex: 1;
|
||||
max-width: 320px;
|
||||
}
|
||||
|
||||
.home-proxy-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
padding-left: 96px;
|
||||
}
|
||||
|
||||
.home-study-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@@ -1032,8 +1066,14 @@ select {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.admin-table {
|
||||
width: 100%;
|
||||
.page-head-actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.admin-table { width: 100%;
|
||||
min-width: 820px;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
@@ -1056,6 +1096,11 @@ select {
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.admin-table .pick {
|
||||
width: 36px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.admin-table .employee,
|
||||
.admin-table .when {
|
||||
font-variant-numeric: tabular-nums;
|
||||
|
||||
Reference in New Issue
Block a user