mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 03:49:19 +08:00
refacor: 重构目录架构
This commit is contained in:
@@ -0,0 +1,258 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from 'vue';
|
||||
|
||||
import {
|
||||
downloadAndVerifyFeature,
|
||||
fetchFeatureManifest,
|
||||
} from '@/api/open/extensionFeatures';
|
||||
import type { ExtensionFeatureItem } from '@/features/runtime/types';
|
||||
import { urlMatchesPatterns } from '@/features/runtime/types';
|
||||
import { EXTENSION_NAME } from '@/shared/constants';
|
||||
import { toUserMessage } from '@/shared/errors';
|
||||
import { getSettings, saveSettings } from '@/storage/settings';
|
||||
|
||||
import {
|
||||
getFloatingPanelState,
|
||||
mountFeatureOnPage,
|
||||
unmountFeatureOnPage,
|
||||
} from './messaging';
|
||||
|
||||
const apiBaseUrl = ref('');
|
||||
const apiKey = ref('');
|
||||
const features = ref<ExtensionFeatureItem[]>([]);
|
||||
const featuresLoading = ref(false);
|
||||
const featuresUpdatedAt = ref('');
|
||||
const activeFeatureId = ref<string | null>(null);
|
||||
const currentTabUrl = ref('');
|
||||
const statusText = ref('');
|
||||
const errorText = ref('');
|
||||
const busy = ref(false);
|
||||
const checking = ref(false);
|
||||
const configOpen = ref(false);
|
||||
|
||||
const hasConfig = computed(() => Boolean(apiBaseUrl.value.trim() && apiKey.value.trim()));
|
||||
|
||||
const matchedFeatures = computed(() => {
|
||||
const list = Array.isArray(features.value) ? features.value : [];
|
||||
if (!currentTabUrl.value) return list;
|
||||
return list.filter((f) => urlMatchesPatterns(currentTabUrl.value, f?.matchUrlPatterns ?? []));
|
||||
});
|
||||
|
||||
const configSummary = computed(() => {
|
||||
if (!hasConfig.value) return '未配置';
|
||||
try {
|
||||
const host = new URL(apiBaseUrl.value.trim()).host;
|
||||
return host || '已配置';
|
||||
} catch {
|
||||
return '已配置';
|
||||
}
|
||||
});
|
||||
|
||||
async function loadLocalSettings() {
|
||||
const settings = await getSettings();
|
||||
apiBaseUrl.value = settings.apiBaseUrl;
|
||||
apiKey.value = settings.apiKey;
|
||||
// 未配置时默认展开
|
||||
configOpen.value = !(settings.apiBaseUrl && settings.apiKey);
|
||||
}
|
||||
|
||||
async function saveConfig() {
|
||||
await saveSettings({
|
||||
apiBaseUrl: apiBaseUrl.value.trim(),
|
||||
apiKey: apiKey.value.trim(),
|
||||
});
|
||||
}
|
||||
|
||||
async function persistAndCheck() {
|
||||
checking.value = true;
|
||||
errorText.value = '';
|
||||
statusText.value = '';
|
||||
try {
|
||||
await saveConfig();
|
||||
const manifest = await fetchFeatureManifest();
|
||||
features.value = manifest.features ?? [];
|
||||
featuresUpdatedAt.value = manifest.updatedAt ?? '';
|
||||
const count = features.value.length;
|
||||
statusText.value = `已连接 · 清单 ${count} 项`;
|
||||
configOpen.value = false;
|
||||
} catch (error) {
|
||||
errorText.value = toUserMessage(error);
|
||||
configOpen.value = true;
|
||||
} finally {
|
||||
checking.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshTabUrl() {
|
||||
try {
|
||||
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||
currentTabUrl.value = tabs[0]?.url ?? '';
|
||||
} catch {
|
||||
currentTabUrl.value = '';
|
||||
}
|
||||
}
|
||||
|
||||
async function loadFeatures() {
|
||||
featuresLoading.value = true;
|
||||
errorText.value = '';
|
||||
try {
|
||||
await saveConfig();
|
||||
const manifest = await fetchFeatureManifest();
|
||||
features.value = manifest.features ?? [];
|
||||
featuresUpdatedAt.value = manifest.updatedAt ?? '';
|
||||
statusText.value = `清单已刷新(${features.value.length})`;
|
||||
} catch (error) {
|
||||
errorText.value = toUserMessage(error);
|
||||
configOpen.value = true;
|
||||
} finally {
|
||||
featuresLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshPanelState() {
|
||||
try {
|
||||
const state = await getFloatingPanelState();
|
||||
activeFeatureId.value = state.visible ? 'pdd-ad-fee' : null;
|
||||
} catch {
|
||||
activeFeatureId.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleOpenFeature(item: ExtensionFeatureItem) {
|
||||
busy.value = true;
|
||||
errorText.value = '';
|
||||
statusText.value = '';
|
||||
try {
|
||||
await saveConfig();
|
||||
const remote = await downloadAndVerifyFeature(item);
|
||||
await mountFeatureOnPage(item.id, remote.code);
|
||||
activeFeatureId.value = item.id;
|
||||
statusText.value = `已打开 ${item.name}`;
|
||||
} catch (error) {
|
||||
errorText.value = toUserMessage(error);
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCloseFeature() {
|
||||
busy.value = true;
|
||||
errorText.value = '';
|
||||
try {
|
||||
await unmountFeatureOnPage();
|
||||
activeFeatureId.value = null;
|
||||
statusText.value = '已关闭';
|
||||
} catch (error) {
|
||||
errorText.value = toUserMessage(error);
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await loadLocalSettings();
|
||||
await refreshTabUrl();
|
||||
await refreshPanelState();
|
||||
if (hasConfig.value) {
|
||||
await loadFeatures();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="popup-root">
|
||||
<header class="popup-header">
|
||||
<div class="min-w-0 flex-1">
|
||||
<h1>{{ EXTENSION_NAME }}</h1>
|
||||
<p class="popup-sub">{{ configSummary }}</p>
|
||||
</div>
|
||||
<button
|
||||
class="popup-link"
|
||||
type="button"
|
||||
:disabled="busy || checking"
|
||||
@click="configOpen = !configOpen"
|
||||
>
|
||||
{{ configOpen ? '收起配置' : '配置' }}
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<section v-show="configOpen" class="popup-card space-y-2">
|
||||
<label class="popup-label">
|
||||
Base URL
|
||||
<input
|
||||
v-model="apiBaseUrl"
|
||||
class="popup-input"
|
||||
placeholder="https://host/api"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</label>
|
||||
<label class="popup-label">
|
||||
API Key
|
||||
<input
|
||||
v-model="apiKey"
|
||||
class="popup-input"
|
||||
type="password"
|
||||
placeholder="sk_live_..."
|
||||
autocomplete="off"
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
class="popup-btn popup-btn-primary w-full"
|
||||
type="button"
|
||||
:disabled="busy || checking || !apiBaseUrl.trim() || !apiKey.trim()"
|
||||
@click="persistAndCheck"
|
||||
>
|
||||
{{ checking ? '连接中…' : '保存并连接' }}
|
||||
</button>
|
||||
</section>
|
||||
|
||||
<section class="popup-card space-y-2">
|
||||
<div class="flex items-center justify-between gap-2">
|
||||
<h2 class="popup-section-title">功能</h2>
|
||||
<button
|
||||
class="popup-link"
|
||||
type="button"
|
||||
:disabled="featuresLoading || busy || checking || !hasConfig"
|
||||
:title="featuresUpdatedAt ? `更新于 ${featuresUpdatedAt}` : ''"
|
||||
@click="loadFeatures"
|
||||
>
|
||||
{{ featuresLoading ? '…' : '刷新' }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p v-if="!hasConfig" class="popup-hint">先填写上方连接配置</p>
|
||||
<p v-else-if="!matchedFeatures.length && !featuresLoading" class="popup-hint">
|
||||
{{ features.length ? '当前页无匹配功能,请打开对应后台页' : '暂无清单,点「刷新」或「保存并连接」' }}
|
||||
</p>
|
||||
|
||||
<div v-for="item in matchedFeatures" :key="item.id" class="popup-feature">
|
||||
<div class="min-w-0 flex-1">
|
||||
<div class="truncate text-sm font-medium text-antd-text">{{ item.name }}</div>
|
||||
<div class="text-[11px] text-antd-text-tertiary">v{{ item.version }}</div>
|
||||
</div>
|
||||
<div class="flex shrink-0 gap-1.5">
|
||||
<button
|
||||
v-if="activeFeatureId === item.id"
|
||||
class="popup-btn popup-btn-ghost"
|
||||
type="button"
|
||||
:disabled="busy || checking"
|
||||
@click="handleCloseFeature"
|
||||
>
|
||||
关闭
|
||||
</button>
|
||||
<button
|
||||
class="popup-btn popup-btn-primary"
|
||||
type="button"
|
||||
:disabled="busy || checking"
|
||||
@click="handleOpenFeature(item)"
|
||||
>
|
||||
{{ activeFeatureId === item.id ? '重新打开' : '打开' }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<p v-if="statusText" class="popup-status">{{ statusText }}</p>
|
||||
<p v-if="errorText" class="popup-error">{{ errorText }}</p>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user