fix: 记住下载选项并按登录地市选择默认地市
This commit is contained in:
@@ -15,12 +15,14 @@ import { HelpCircleOutline } from "@vicons/ionicons5";
|
|||||||
import { ApiError, chooseDirectory } from "../../shared/http";
|
import { ApiError, chooseDirectory } from "../../shared/http";
|
||||||
import { createJob, loadMeta, type Meta } from "./api";
|
import { createJob, loadMeta, type Meta } from "./api";
|
||||||
|
|
||||||
|
const props = defineProps<{ cityName?: string }>();
|
||||||
const emit = defineEmits<{ started: [id: string] }>();
|
const emit = defineEmits<{ started: [id: string] }>();
|
||||||
const message = useMessage();
|
const message = useMessage();
|
||||||
|
|
||||||
|
const FORM_KEY = "nqi.download.form";
|
||||||
const meta = ref<Meta | null>(null);
|
const meta = ref<Meta | null>(null);
|
||||||
const network = ref("NR");
|
const network = ref("NR");
|
||||||
const cityCode = ref("JM");
|
const cityCode = ref("");
|
||||||
const month = ref<string | null>(previousMonth());
|
const month = ref<string | null>(previousMonth());
|
||||||
const concurrency = ref(8);
|
const concurrency = ref(8);
|
||||||
const groups = ref<string[]>([]);
|
const groups = ref<string[]>([]);
|
||||||
@@ -39,6 +41,56 @@ const networkOptions = [
|
|||||||
{ label: "4G", value: "LTE" },
|
{ label: "4G", value: "LTE" },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
function readForm() {
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem(FORM_KEY);
|
||||||
|
if (!raw) return null;
|
||||||
|
const data = JSON.parse(raw) as {
|
||||||
|
network?: unknown;
|
||||||
|
gridSize?: unknown;
|
||||||
|
concurrency?: unknown;
|
||||||
|
outputDir?: unknown;
|
||||||
|
};
|
||||||
|
return data && typeof data === "object" ? data : null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyForm() {
|
||||||
|
const saved = readForm();
|
||||||
|
if (!saved) return;
|
||||||
|
if (saved.network === "NR" || saved.network === "LTE") network.value = saved.network;
|
||||||
|
if (saved.gridSize === "GRID20" || saved.gridSize === "GRID50") gridSize.value = saved.gridSize;
|
||||||
|
if (typeof saved.concurrency === "number" && saved.concurrency >= 1 && saved.concurrency <= 32) {
|
||||||
|
concurrency.value = Math.round(saved.concurrency);
|
||||||
|
}
|
||||||
|
if (typeof saved.outputDir === "string") outputDir.value = saved.outputDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
function rememberForm() {
|
||||||
|
localStorage.setItem(
|
||||||
|
FORM_KEY,
|
||||||
|
JSON.stringify({
|
||||||
|
network: network.value,
|
||||||
|
gridSize: gridSize.value,
|
||||||
|
concurrency: concurrency.value,
|
||||||
|
outputDir: outputDir.value,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function matchCity(cities: { name: string; code: string }[], cityName: string) {
|
||||||
|
const normalized = cityName.replace(/\s+/g, "");
|
||||||
|
if (!cities.length) return "";
|
||||||
|
if (!normalized) return cities[0].code;
|
||||||
|
const matches = cities.filter((city) => normalized === city.name || normalized === `${city.name}市` || normalized.includes(city.name));
|
||||||
|
matches.sort((left, right) => right.name.length - left.name.length);
|
||||||
|
return matches[0]?.code ?? cities[0].code;
|
||||||
|
}
|
||||||
|
|
||||||
|
applyForm();
|
||||||
|
|
||||||
function previousMonth() {
|
function previousMonth() {
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
date.setDate(1);
|
date.setDate(1);
|
||||||
@@ -49,6 +101,7 @@ function previousMonth() {
|
|||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
try {
|
try {
|
||||||
meta.value = await loadMeta();
|
meta.value = await loadMeta();
|
||||||
|
cityCode.value = matchCity(meta.value.cities || [], props.cityName || "");
|
||||||
syncGroups();
|
syncGroups();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
message.error(error instanceof ApiError ? error.message : "读取下载项目失败");
|
message.error(error instanceof ApiError ? error.message : "读取下载项目失败");
|
||||||
@@ -56,6 +109,7 @@ onMounted(async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
watch(network, syncGroups);
|
watch(network, syncGroups);
|
||||||
|
watch([network, gridSize, concurrency, outputDir], rememberForm);
|
||||||
|
|
||||||
function syncGroups() {
|
function syncGroups() {
|
||||||
const items = meta.value?.groups[network.value] || [];
|
const items = meta.value?.groups[network.value] || [];
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ async function signOut() {
|
|||||||
</div>
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
<main class="content">
|
<main class="content">
|
||||||
<GridDownloadView v-if="page === 'grid'" @started="onStarted" />
|
<GridDownloadView v-if="page === 'grid'" :city-name="props.user.cityName" @started="onStarted" />
|
||||||
<DownloadManagerView v-else :focus-id="focusId" />
|
<DownloadManagerView v-else :focus-id="focusId" />
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user