Files
EnuivTools/web/src/features/admin/SettingsView.vue
T

248 lines
10 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { apiFetch } from "../../shared/http";
import { onMounted, onUnmounted, ref } from "vue";
import { NAlert, NButton, NCollapse, NCollapseItem, NForm, NFormItem, NInput, NInputNumber, NRadioButton, NRadioGroup, NSwitch } from "naive-ui";
const emit = defineEmits<{ saved: [] }>();
const loading = ref(true);
const loaded = ref(false);
const saving = ref(false);
const errorText = ref("");
const successText = ref("");
const refreshInterval = ref<number | null>(null);
const showManualJigsaw = ref(false);
const manualPreference = ref(false);
const attempts = ref<number | null>(null);
const username = ref("");
const password = ref("");
const confirmPassword = ref("");
const currentPassword = ref("");
const hbMode = ref("manual");
const hbVideo = ref<number | null>(30);
const hbDocument = ref<number | null>(120);
const hbScorm = ref<number | null>(120);
const hoursSource = ref("api");
let savedTimer = 0;
function detailOf(payload: unknown, fallback: string) {
if (!payload || typeof payload !== "object" || !("detail" in payload)) return fallback;
const detail = (payload as { detail?: unknown }).detail;
if (typeof detail === "string" && detail) return detail;
if (!Array.isArray(detail)) return fallback;
const texts = detail.flatMap((item) => {
if (typeof item === "string" && item) return [item];
if (item && typeof item === "object" && "msg" in item) {
const msg = (item as { msg?: unknown }).msg;
if (typeof msg === "string" && msg) return [msg];
}
return [];
});
return texts.length ? texts.join(";") : fallback;
}
function readNumber(value: unknown) {
if (typeof value === "number" && Number.isFinite(value)) return value;
if (typeof value === "string" && value.trim()) {
const parsed = Number(value);
if (Number.isFinite(parsed)) return parsed;
}
return null;
}
onUnmounted(() => window.clearTimeout(savedTimer));
onMounted(async () => {
try {
const response = await apiFetch("/api/admin/settings");
const payload = await response.json().catch(() => ({}));
if (!response.ok || !payload || typeof payload !== "object") {
errorText.value = detailOf(payload, "系统配置没有获取到。");
return;
}
const body = payload as Record<string, unknown>;
const minutes = readNumber(body.refresh_interval_minutes);
const count = readNumber(body.auto_jigsaw_attempts);
if (minutes == null || count == null || typeof body.admin_username !== "string") {
errorText.value = "系统配置没有获取到。";
return;
}
refreshInterval.value = minutes;
attempts.value = count;
showManualJigsaw.value = body.show_manual_jigsaw === true;
manualPreference.value = body.manual_preference === true;
username.value = body.admin_username;
hbMode.value = typeof body.heartbeat_interval_mode === "string" ? body.heartbeat_interval_mode : "manual";
hbVideo.value = readNumber(body.heartbeat_video_seconds) ?? 30;
hbDocument.value = readNumber(body.heartbeat_document_seconds) ?? 120;
hbScorm.value = readNumber(body.heartbeat_scorm_seconds) ?? 120;
hoursSource.value = body.hours_source === "history" ? "history" : "api";
loaded.value = true;
} catch {
errorText.value = "系统配置没有获取到。";
} finally {
loading.value = false;
}
});
function invalidInteger(value: number | null, min: number, max: number) {
return value == null || !Number.isInteger(value) || value < min || value > max;
}
async function save() {
if (saving.value || !loaded.value) return;
errorText.value = "";
successText.value = "";
if (invalidInteger(refreshInterval.value, 1, 1440)) {
errorText.value = "定时刷新间隔须为 1 到 1440 的整数";
return;
}
if (invalidInteger(attempts.value, 1, 10)) {
errorText.value = "自动滑动次数须为 1 到 10 的整数";
return;
}
if (hbMode.value === "manual") {
if (invalidInteger(hbVideo.value, 0, 300)) {
errorText.value = "视频心跳间隔须为 0 到 300 的整数";
return;
}
if (invalidInteger(hbDocument.value, 0, 600)) {
errorText.value = "文档心跳间隔须为 0 到 600 的整数";
return;
}
if (invalidInteger(hbScorm.value, 0, 600)) {
errorText.value = "Scorm 心跳间隔须为 0 到 600 的整数";
return;
}
}
if (hbMode.value !== "manual") {
if (hbVideo.value == null) hbVideo.value = 30;
if (hbDocument.value == null) hbDocument.value = 120;
if (hbScorm.value == null) hbScorm.value = 120;
}
const adminUsername = username.value.trim();
if (!adminUsername) {
errorText.value = "请填写管理员用户名";
return;
}
const body: Record<string, unknown> = {
refresh_interval_minutes: refreshInterval.value as number,
show_manual_jigsaw: showManualJigsaw.value,
manual_preference: manualPreference.value,
auto_jigsaw_attempts: attempts.value as number,
admin_username: adminUsername,
heartbeat_interval_mode: hbMode.value,
heartbeat_video_seconds: hbVideo.value as number,
heartbeat_document_seconds: hbDocument.value as number,
heartbeat_scorm_seconds: hbScorm.value as number,
hours_source: hoursSource.value,
};
if (password.value !== "" || confirmPassword.value !== "") {
if (!currentPassword.value) {
errorText.value = "请填写管理员密码";
return;
}
if (password.value !== confirmPassword.value) {
errorText.value = "两次输入的新密码不一致";
return;
}
body.admin_password = password.value;
body.admin_password_current = currentPassword.value;
}
saving.value = true;
try {
const response = await apiFetch("/api/admin/settings", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
const payload = await response.json().catch(() => ({}));
if (!response.ok) {
errorText.value = detailOf(payload, "没有保存。");
return;
}
password.value = "";
confirmPassword.value = "";
currentPassword.value = "";
successText.value = "已保存";
window.clearTimeout(savedTimer);
savedTimer = window.setTimeout(() => {
successText.value = "";
savedTimer = 0;
}, 3000);
emit("saved");
} catch {
errorText.value = "没有保存。";
} finally {
saving.value = false;
}
}
</script>
<template>
<section class="page admin-page">
<div class="settings-form">
<header class="page-head">
<n-button type="primary" :loading="saving" :disabled="!loaded" @click="save">保存</n-button>
</header>
<n-alert v-if="errorText" class="admin-alert" type="error">{{ errorText }}</n-alert>
<n-alert v-else-if="successText" class="admin-alert" type="success">{{ successText }}</n-alert>
<div class="settings-stack">
<p v-if="loading && !loaded" class="admin-status">正在获取</p>
<n-collapse v-else-if="loaded" class="settings-tree" :default-expanded-names="['login', 'hours', 'admin']" arrow-placement="left">
<n-collapse-item title="登录验证" name="login">
<n-form class="compact-nform" label-placement="left" label-align="left" :label-width="176" :show-feedback="false" size="small" @submit.prevent="save">
<n-form-item label="定时刷新间隔">
<div class="target-control">
<n-input-number v-model:value="refreshInterval" button-placement="both" :min="1" :max="1440" :precision="0" />
<span class="target-unit">分钟</span>
</div>
</n-form-item>
<n-form-item label="显示手动滑动验证码">
<n-switch v-model:value="showManualJigsaw" />
</n-form-item>
<n-form-item label="默认勾选手动滑动验证码">
<n-switch v-model:value="manualPreference" />
</n-form-item>
<n-form-item label="自动滑动次数">
<div class="target-control">
<n-input-number v-model:value="attempts" button-placement="both" :min="1" :max="10" :precision="0" />
<span class="target-unit">次</span>
</div>
</n-form-item>
</n-form>
</n-collapse-item>
<n-collapse-item title="学时达标" name="hours">
<n-form class="compact-nform" label-placement="left" label-align="left" :label-width="176" :show-feedback="false" size="small" @submit.prevent="save">
<n-form-item label="判断依据">
<n-radio-group v-model:value="hoursSource" size="small">
<n-radio-button value="api">官方统计数据</n-radio-button>
<n-radio-button value="history">历史统计数据</n-radio-button>
</n-radio-group>
</n-form-item>
</n-form>
<p class="form-hint">新用户默认使用这里的选择,已有用户不受影响</p>
</n-collapse-item>
<n-collapse-item title="管理员" name="admin">
<n-form class="compact-nform" label-placement="left" label-align="left" :label-width="176" :show-feedback="false" size="small" @submit.prevent="save">
<n-form-item label="用户名">
<n-input v-model:value="username" :input-props="{ autocomplete: 'username', name: 'username' }" />
</n-form-item>
<n-form-item label="管理员密码">
<n-input v-model:value="currentPassword" type="password" show-password-on="click" placeholder="修改密码时填写" :input-props="{ autocomplete: 'current-password', name: 'current-password' }" />
</n-form-item>
<n-form-item label="新管理员密码">
<n-input v-model:value="password" type="password" show-password-on="click" placeholder="不修改请留空" :input-props="{ autocomplete: 'new-password', name: 'new-password' }" />
</n-form-item>
<n-form-item label="再输入一次">
<n-input v-model:value="confirmPassword" type="password" show-password-on="click" placeholder="请输入" :input-props="{ autocomplete: 'new-password', name: 'confirm-password' }" />
</n-form-item>
</n-form>
</n-collapse-item>
</n-collapse>
</div>
</div>
</section>
</template>