feat: 账号可设置同时学习课程数并支持关闭代理

This commit is contained in:
2026-11-15 01:48:39 +08:00
parent ecd774507f
commit aa26d00c60
14 changed files with 564 additions and 119 deletions
+119 -23
View File
@@ -1,7 +1,7 @@
<script setup lang="ts">
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";
import { NAlert, NButton, NCheckbox, NDropdown, NIcon, NInput, NInputNumber, NModal, NSelect, NTooltip, useDialog, type DropdownOption } from "naive-ui";
import { HelpCircleOutline, SettingsOutline } from "@vicons/ionicons5";
interface UserRow {
employee_id: string;
@@ -17,6 +17,7 @@ interface UserRow {
study_start_hour: number;
study_start_minute: number;
study_start_second: number;
course_workers: number;
}
interface SecretField {
@@ -42,13 +43,15 @@ const proxyOpen = ref(false);
const proxySaving = ref(false);
const proxyError = ref("");
const proxyIds = ref<string[]>([]);
const proxyScheme = ref("http");
const proxyScheme = ref("off");
const proxyHost = ref("");
const proxyPort = ref<number | null>(null);
const proxyUser = ref("");
const proxyPassword = ref("");
const proxyHasPassword = ref(false);
const proxyOff = computed(() => proxyScheme.value === "off");
const proxyOptions = [
{ label: "关闭", value: "off" },
{ label: "HTTP", value: "http" },
{ label: "HTTPS", value: "https" },
{ label: "SOCKS4", value: "socks4" },
@@ -62,6 +65,11 @@ const startDay = ref<number | null>(1);
const startHour = ref<number | null>(0);
const startMinute = ref<number | null>(0);
const startSecond = ref<number | null>(0);
const workersOpen = ref(false);
const workersSaving = ref(false);
const workersError = ref("");
const workersIds = ref<string[]>([]);
const workersValue = ref<number | null>(1);
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);
@@ -111,6 +119,7 @@ function asUser(value: unknown): UserRow | null {
study_start_hour: typeof value.study_start_hour === "number" ? value.study_start_hour : 0,
study_start_minute: typeof value.study_start_minute === "number" ? value.study_start_minute : 0,
study_start_second: typeof value.study_start_second === "number" ? value.study_start_second : 0,
course_workers: typeof value.course_workers === "number" ? value.course_workers : 1,
};
}
@@ -342,7 +351,7 @@ function toggleAll(checked: boolean) {
}
function resetProxyForm() {
proxyScheme.value = "http";
proxyScheme.value = "off";
proxyHost.value = "";
proxyPort.value = null;
proxyUser.value = "";
@@ -361,7 +370,7 @@ async function openProxy(ids: string[]) {
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";
proxyScheme.value = textOf(payload.scheme) || "off";
proxyHost.value = textOf(payload.host);
proxyPort.value = typeof payload.port === "number" ? payload.port : null;
proxyUser.value = textOf(payload.username);
@@ -379,27 +388,33 @@ function proxyPasswordPayload() {
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;
if (!proxyOff.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(),
}),
body: JSON.stringify(
proxyOff.value
? { employee_ids: proxyIds.value, scheme: "off" }
: {
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) {
@@ -509,6 +524,49 @@ async function saveStart() {
}
}
function openWorkers(ids: string[]) {
if (!ids.length) return;
workersIds.value = ids;
workersError.value = "";
workersValue.value = 1;
if (ids.length === 1) {
const row = users.value.find((item) => item.employee_id === ids[0]);
if (row) workersValue.value = row.course_workers;
}
workersOpen.value = true;
}
async function saveWorkers() {
if (workersSaving.value || !workersIds.value.length) return;
workersError.value = "";
if (workersValue.value == null || !Number.isInteger(workersValue.value) || workersValue.value < 1 || workersValue.value > 8) {
workersError.value = "同时学习课程数须为 1 到 8";
return;
}
workersSaving.value = true;
try {
const response = await fetch("/api/admin/users/course-workers", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
employee_ids: workersIds.value,
workers: workersValue.value,
}),
});
const payload: unknown = await response.json().catch(() => ({}));
if (!response.ok) {
workersError.value = detailOf(payload, "没有保存。");
return;
}
workersOpen.value = false;
await load();
} catch {
workersError.value = "没有保存。";
} finally {
workersSaving.value = false;
}
}
function rowActions(row: UserRow): DropdownOption[] {
const busy = checkingAll.value || checkingId.value !== "";
return [
@@ -517,6 +575,7 @@ function rowActions(row: UserRow): DropdownOption[] {
{ label: "设置代理", key: "proxy" },
{ label: "清除代理", key: "proxy-clear" },
{ label: "开始时刻", key: "start" },
{ label: "同时课程", key: "workers" },
{ label: row.refresh_paused ? "恢复自动刷新" : "停止自动刷新", key: "refresh" },
{ label: "删除", key: "delete", props: { style: { color: "var(--danger)" } } },
];
@@ -528,6 +587,7 @@ function onRowAction(row: UserRow, key: string | number) {
else if (key === "proxy") void openProxy([row.employee_id]);
else if (key === "proxy-clear") confirmClearProxy([row.employee_id]);
else if (key === "start") openStart([row.employee_id]);
else if (key === "workers") openWorkers([row.employee_id]);
else if (key === "refresh") confirmRefresh(row);
else if (key === "delete") confirmDelete(row);
}
@@ -545,6 +605,7 @@ onMounted(() => {
<n-button :disabled="!pickedIds.length" @click="openProxy(pickedIds)">设置代理</n-button>
<n-button :disabled="!pickedIds.length" @click="confirmClearProxy(pickedIds)">清除代理</n-button>
<n-button :disabled="!pickedIds.length" @click="openStart(pickedIds)">开始时刻</n-button>
<n-button :disabled="!pickedIds.length" @click="openWorkers(pickedIds)">同时课程</n-button>
<n-button :loading="checkingAll" :disabled="checkingAll || checkingId !== '' || !users.length" @click="checkAll">检查全部</n-button>
</div>
</header>
@@ -562,6 +623,7 @@ onMounted(() => {
<th>凭据是否有效</th>
<th>本月学时</th>
<th>开始时刻</th>
<th>同时课程</th>
<th>代理</th>
<th>上次刷新时间</th>
<th>自动刷新</th>
@@ -578,6 +640,7 @@ onMounted(() => {
<td :class="validityClass(validityText(row))">{{ validityText(row) }}</td>
<td class="when">{{ row.month_hours || "—" }}</td>
<td class="when">{{ row.study_start || "—" }}</td>
<td class="when">{{ row.course_workers }}{{ row.course_workers === 1 ? "(推荐)" : "" }}</td>
<td class="when">{{ row.proxy || "—" }}</td>
<td class="when">{{ formatRefreshTime(row.last_refresh_at) }}</td>
<td>{{ row.refresh_paused ? "已停止" : "开启" }}</td>
@@ -631,15 +694,15 @@ onMounted(() => {
</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" />
<n-input id="admin-proxy-host" v-model:value="proxyHost" placeholder="例如 127.0.0.1" :disabled="proxyOff" />
</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" />
<n-input-number id="admin-proxy-port" v-model:value="proxyPort" button-placement="both" :min="1" :max="65535" :precision="0" :disabled="proxyOff" />
</div>
<div class="form-row">
<label for="admin-proxy-user">账号</label>
<n-input id="admin-proxy-user" v-model:value="proxyUser" placeholder="可不填" autocomplete="off" />
<n-input id="admin-proxy-user" v-model:value="proxyUser" placeholder="可不填" autocomplete="off" :disabled="proxyOff" />
</div>
<div class="form-row">
<label for="admin-proxy-password">密码</label>
@@ -650,6 +713,7 @@ onMounted(() => {
show-password-on="click"
:placeholder="proxyIds.length === 1 && proxyHasPassword ? '已保存,留空则保持' : '可不填'"
autocomplete="new-password"
:disabled="proxyOff"
/>
</div>
</form>
@@ -693,5 +757,37 @@ onMounted(() => {
</div>
</template>
</n-modal>
<n-modal
:show="workersOpen"
preset="card"
:title="workersIds.length === 1 ? `工号 ${workersIds[0]} 的同时课程` : `已选 ${workersIds.length} 个账号的同时课程`"
:style="{ width: 'min(520px, calc(100vw - 32px))' }"
@update:show="(show) => (workersOpen = show)"
>
<n-alert v-if="workersError" class="admin-alert" type="error">{{ workersError }}</n-alert>
<form class="settings-form" @submit.prevent="saveWorkers">
<div class="form-row">
<label class="field-title" for="admin-course-workers">
<span>同时学习</span>
<n-tooltip placement="top">
<template #trigger>
<button type="button" class="hint-icon" aria-label="说明">
<n-icon><HelpCircleOutline /></n-icon>
</button>
</template>
速度较慢,但是稳定且不容易触发风控,最大可设置8
</n-tooltip>
</label>
<n-input-number id="admin-course-workers" v-model:value="workersValue" button-placement="both" :min="1" :max="8" :precision="0" />
</div>
<p v-if="workersValue === 1" class="form-hint">1 门为推荐</p>
</form>
<template #footer>
<div class="modal-actions">
<n-button @click="workersOpen = false">取消</n-button>
<n-button type="primary" :loading="workersSaving" @click="saveWorkers">保存</n-button>
</div>
</template>
</n-modal>
</section>
</template>
+93 -46
View File
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import { NAlert, NButton, NIcon, NInput, NInputNumber, NSelect, NTooltip } from "naive-ui";
import { RefreshOutline } from "@vicons/ionicons5";
import { HelpCircleOutline, RefreshOutline } from "@vicons/ionicons5";
interface HomeSummary {
name: string;
@@ -180,16 +180,49 @@ function saveStart() {
watch([startDay, startHour, startMinute, startSecond], () => { if (startLoaded.value) saveStart(); });
const workersLoaded = ref(false);
const courseWorkers = ref<number | null>(1);
let workersSaveTimer: ReturnType<typeof setTimeout> | undefined;
async function loadWorkers() {
try {
const res = await fetch("/api/settings/course-workers");
if (res.ok) {
const data = await res.json();
courseWorkers.value = typeof data.workers === "number" ? data.workers : 1;
workersLoaded.value = true;
}
} catch { /* 静默 */ }
}
function saveWorkers(value: number | null) {
if (value == null || !Number.isInteger(value) || value < 1 || value > 8) return;
clearTimeout(workersSaveTimer);
workersSaveTimer = setTimeout(async () => {
try {
await fetch("/api/settings/course-workers", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ workers: value }),
});
} catch { /* 静默 */ }
}, 600);
}
watch(courseWorkers, (v) => { if (workersLoaded.value) saveWorkers(v); });
const proxyLoaded = ref(false);
const proxySaving = ref(false);
const proxyError = ref("");
const proxyScheme = ref("http");
const proxyScheme = ref("off");
const proxyHost = ref("");
const proxyPort = ref<number | null>(null);
const proxyUser = ref("");
const proxyPassword = ref("");
const proxyHasPassword = ref(false);
const proxyOff = computed(() => proxyScheme.value === "off");
const proxyOptions = [
{ label: "关闭", value: "off" },
{ label: "HTTP", value: "http" },
{ label: "HTTPS", value: "https" },
{ label: "SOCKS4", value: "socks4" },
@@ -201,7 +234,7 @@ async function loadProxy() {
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";
proxyScheme.value = typeof data.scheme === "string" && data.scheme ? data.scheme : "off";
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 : "";
@@ -220,26 +253,32 @@ function proxyPasswordPayload() {
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;
if (!proxyOff.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(),
}),
body: JSON.stringify(
proxyOff.value
? { scheme: "off" }
: {
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) {
@@ -248,6 +287,12 @@ async function saveProxy() {
}
proxyPassword.value = "";
proxyHasPassword.value = data.has_password === true;
if (proxyOff.value) {
proxyHost.value = "";
proxyPort.value = null;
proxyUser.value = "";
proxyHasPassword.value = false;
}
} catch {
proxyError.value = "没有保存。";
} finally {
@@ -255,30 +300,6 @@ async function saveProxy() {
}
}
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");
@@ -286,7 +307,7 @@ async function loadStudyStatus() {
} catch { /* 静默 */ }
}
onMounted(() => { refresh(); loadTarget(); loadStart(); loadProxy(); loadStudyStatus(); });
onMounted(() => { refresh(); loadTarget(); loadStart(); loadWorkers(); loadProxy(); loadStudyStatus(); });
</script>
<template>
@@ -369,6 +390,32 @@ onMounted(() => { refresh(); loadTarget(); loadStart(); loadProxy(); loadStudySt
<n-input-number v-model:value="startSecond" button-placement="both" :min="0" :max="59" :precision="0" size="small" />
</div>
</div>
<div v-if="workersLoaded" class="home-target">
<label class="field-title" for="course-workers">
<span>同时学习课程</span>
<n-tooltip placement="top">
<template #trigger>
<button type="button" class="hint-icon" aria-label="说明">
<n-icon><HelpCircleOutline /></n-icon>
</button>
</template>
速度较慢,但是稳定且不容易触发风控,最大可设置8
</n-tooltip>
</label>
<div class="target-control">
<n-input-number
id="course-workers"
v-model:value="courseWorkers"
button-placement="both"
:min="1"
:max="8"
:precision="0"
size="small"
/>
<span class="target-unit">门</span>
<span v-if="courseWorkers === 1" class="recommend-mark">推荐</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">
@@ -377,15 +424,15 @@ onMounted(() => { refresh(); loadTarget(); loadStart(); loadProxy(); loadStudySt
</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" />
<n-input id="proxy-host" v-model:value="proxyHost" size="small" placeholder="例如 127.0.0.1" :disabled="proxyOff" />
</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" />
<n-input-number id="proxy-port" v-model:value="proxyPort" size="small" button-placement="both" :min="1" :max="65535" :precision="0" :disabled="proxyOff" />
</div>
<div class="form-row">
<label for="proxy-user">代理账号</label>
<n-input id="proxy-user" v-model:value="proxyUser" size="small" placeholder="可不填" autocomplete="off" />
<n-input id="proxy-user" v-model:value="proxyUser" size="small" placeholder="可不填" autocomplete="off" :disabled="proxyOff" />
</div>
<div class="form-row">
<label for="proxy-password">代理密码</label>
@@ -397,11 +444,11 @@ onMounted(() => { refresh(); loadTarget(); loadStart(); loadProxy(); loadStudySt
show-password-on="click"
:placeholder="proxyHasPassword ? '已保存,留空则保持' : '可不填'"
autocomplete="new-password"
:disabled="proxyOff"
/>
</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">
+28
View File
@@ -419,6 +419,34 @@ select {
color: var(--muted);
}
.field-title {
display: inline-flex;
align-items: center;
gap: 4px;
}
.hint-icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 18px;
height: 18px;
padding: 0;
border: 0;
background: transparent;
color: var(--muted);
cursor: help;
}
.hint-icon .n-icon {
font-size: 15px;
}
.recommend-mark {
color: var(--muted);
font-size: 12px;
}
.target-control {
display: flex;
align-items: center;