feat: 账号可配置 HTTP/HTTPS/SOCKS 代理
This commit is contained in:
@@ -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">
|
||||
|
||||
Reference in New Issue
Block a user