refactor: 修改结构 功能动态从后端获取

This commit is contained in:
2026-08-16 19:41:44 +08:00
parent 8a8e36602f
commit 481518500e
27 changed files with 760 additions and 107 deletions
+150 -93
View File
@@ -1,40 +1,53 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { computed, onMounted, ref } from 'vue';
import { listPddChannelShops, type PddChannelShop } from '@/api/open/pddShop';
import {
downloadAndVerifyFeature,
fetchFeatureManifest,
} from '@/api/open/extensionFeatures';
import type { ExtensionFeatureItem } from '@/features/runtime/types';
import { urlMatchesPatterns } from '@/features/runtime/types';
import { EXTENSION_NAME, OPEN_SCOPES } from '@/shared/constants';
import { toUserMessage } from '@/shared/errors';
import { getSettings, saveSettings } from '@/storage/settings';
import { getFloatingPanelState, openFloatingPanel, toggleFloatingPanel } from './messaging';
import {
getFloatingPanelState,
mountFeatureOnPage,
unmountFeatureOnPage,
} from './messaging';
const apiBaseUrl = ref('');
const apiKey = ref('');
const channelShopId = ref<number | undefined>(undefined);
const channelShopName = ref('');
const shops = ref<PddChannelShop[]>([]);
const shopsLoading = ref(false);
const panelVisible = ref(false);
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 matchedFeatures = computed(() => {
const list = Array.isArray(features.value) ? features.value : [];
if (!currentTabUrl.value) return list;
return list.filter((f) => urlMatchesPatterns(currentTabUrl.value, f?.matchUrlPatterns ?? []));
});
async function loadLocalSettings() {
const settings = await getSettings();
apiBaseUrl.value = settings.apiBaseUrl;
apiKey.value = settings.apiKey;
channelShopId.value = settings.channelShopId;
channelShopName.value = settings.channelShopName ?? '';
}
/** 只保存 URL / Key,不改动悬浮层里的店铺配置 */
async function persistSettings() {
errorText.value = '';
try {
await saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
channelShopId: channelShopId.value,
channelShopName: channelShopName.value,
});
statusText.value = '配置已保存';
} catch (error) {
@@ -42,54 +55,79 @@ async function persistSettings() {
}
}
async function loadShops() {
shopsLoading.value = true;
async function checkConnectivity() {
checking.value = true;
errorText.value = '';
statusText.value = '';
try {
await persistSettings();
const result = await listPddChannelShops({ page: 1, pageSize: 100 });
shops.value = result.records ?? [];
if (channelShopId.value != null) {
const hit = shops.value.find((s) => s.id === channelShopId.value);
if (hit) channelShopName.value = hit.mallName ?? '';
}
await saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
});
const manifest = await fetchFeatureManifest();
const count = manifest.features?.length ?? 0;
statusText.value = `连通正常(清单 ${count} 项${manifest.updatedAt ? `,更新于 ${manifest.updatedAt}` : ''})`;
} catch (error) {
errorText.value = toUserMessage(error);
} finally {
shopsLoading.value = false;
checking.value = false;
}
}
function onShopChange(event: Event) {
const value = Number((event.target as HTMLSelectElement).value);
if (!Number.isFinite(value) || value <= 0) {
channelShopId.value = undefined;
channelShopName.value = '';
return;
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 saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
});
const manifest = await fetchFeatureManifest();
features.value = manifest.features ?? [];
featuresUpdatedAt.value = manifest.updatedAt ?? '';
statusText.value = `已加载功能清单(${features.value.length})`;
} catch (error) {
errorText.value = toUserMessage(error);
} finally {
featuresLoading.value = false;
}
channelShopId.value = value;
const hit = shops.value.find((s) => s.id === value);
channelShopName.value = hit?.mallName ?? '';
}
async function refreshPanelState() {
try {
const state = await getFloatingPanelState();
panelVisible.value = state.visible;
activeFeatureId.value = state.visible ? 'pdd-ad-fee' : null;
} catch {
panelVisible.value = false;
activeFeatureId.value = null;
}
}
async function handleOpenPanel() {
async function handleOpenFeature(item: ExtensionFeatureItem) {
busy.value = true;
errorText.value = '';
statusText.value = '';
try {
await persistSettings();
const state = await openFloatingPanel();
panelVisible.value = state.visible;
statusText.value = '已打开页内悬浮层';
await saveSettings({
apiBaseUrl: apiBaseUrl.value.trim(),
apiKey: apiKey.value.trim(),
});
const remote = await downloadAndVerifyFeature(item);
statusText.value = remote.verified
? `远程脚本已校验(${item.version})`
: `已下载远程脚本(未配置 sha256)`;
await mountFeatureOnPage(item.id);
activeFeatureId.value = item.id;
statusText.value = `已打开:${item.name}`;
} catch (error) {
errorText.value = toUserMessage(error);
} finally {
@@ -97,13 +135,13 @@ async function handleOpenPanel() {
}
}
async function handleTogglePanel() {
async function handleCloseFeature() {
busy.value = true;
errorText.value = '';
try {
const state = await toggleFloatingPanel();
panelVisible.value = state.visible;
statusText.value = state.visible ? '悬浮层已显示' : '悬浮层已关闭';
await unmountFeatureOnPage();
activeFeatureId.value = null;
statusText.value = '已关闭当前功能';
} catch (error) {
errorText.value = toUserMessage(error);
} finally {
@@ -113,7 +151,11 @@ async function handleTogglePanel() {
onMounted(async () => {
await loadLocalSettings();
await refreshTabUrl();
await refreshPanelState();
if (apiBaseUrl.value && apiKey.value) {
await loadFeatures();
}
});
</script>
@@ -122,7 +164,7 @@ onMounted(async () => {
<header>
<h1 class="text-base font-semibold tracking-tight text-antd-text">{{ EXTENSION_NAME }}</h1>
<p class="mt-0.5 text-xs text-antd-text-secondary">
Popup 负责配置与开关;业务操作在页内悬浮层完成。
Popup 负责配置与功能入口;店铺选择在页内悬浮层完成。
</p>
</header>
@@ -146,68 +188,83 @@ onMounted(async () => {
/>
</label>
<p class="text-[11px] leading-relaxed text-antd-text-tertiary">
需要 scope:{{ OPEN_SCOPES.pddShopRead }}、{{ OPEN_SCOPES.pddGoodsAdFeeImport }}
连通性 / 清单需要 {{ OPEN_SCOPES.extensionFeatureRead }};推广费另需店铺/导入 scope。
</p>
<div class="space-y-1">
<div class="flex items-center justify-between">
<span class="text-xs text-antd-text-secondary">默认渠道店铺</span>
<button
class="text-xs text-antd-primary hover:text-antd-primary-hover hover:underline"
type="button"
:disabled="shopsLoading || busy"
@click="loadShops"
>
{{ shopsLoading ? '加载中…' : '拉取列表' }}
</button>
</div>
<select
class="w-full rounded-antd border border-antd-border px-2.5 py-1.5 text-sm outline-none transition focus:border-antd-primary"
:value="channelShopId ?? ''"
@change="onShopChange"
<div class="flex gap-2">
<button
class="flex-1 rounded-antd border border-antd-border bg-white px-3 py-1.5 text-sm text-antd-text transition hover:border-antd-primary hover:text-antd-primary disabled:opacity-50"
type="button"
:disabled="busy || checking"
@click="persistSettings"
>
<option value="">可选,打开悬浮层后仍可改</option>
<option v-for="shop in shops" :key="shop.id" :value="shop.id">
{{ shop.mallName || '未命名' }} (#{{ shop.id }})
</option>
</select>
保存配置
</button>
<button
class="flex-1 rounded-antd bg-antd-primary px-3 py-1.5 text-sm font-medium text-white transition hover:bg-antd-primary-hover disabled:opacity-50"
type="button"
:disabled="busy || checking || !apiBaseUrl.trim() || !apiKey.trim()"
@click="checkConnectivity"
>
{{ checking ? '检查中…' : '检查连通性' }}
</button>
</div>
<button
class="w-full rounded-antd border border-antd-border bg-white px-3 py-1.5 text-sm text-antd-text transition hover:border-antd-primary hover:text-antd-primary disabled:opacity-50"
type="button"
:disabled="busy"
@click="persistSettings"
>
保存配置
</button>
</section>
<section class="space-y-2 rounded-antd-lg border border-antd-border-secondary bg-white p-3 shadow-sm">
<h2 class="text-xs font-semibold text-antd-text-secondary">页内悬浮层</h2>
<p class="text-xs text-antd-text-secondary">
当前状态:
<span class="font-medium text-antd-text">{{ panelVisible ? '已打开' : '未打开' }}</span>
</p>
<div class="flex gap-2">
<div class="flex items-center justify-between">
<h2 class="text-xs font-semibold text-antd-text-secondary">功能列表</h2>
<button
class="flex-1 rounded-antd bg-antd-primary px-3 py-2 text-sm font-medium text-white transition hover:bg-antd-primary-hover disabled:opacity-50"
class="text-xs text-antd-primary hover:underline"
type="button"
:disabled="busy"
@click="handleOpenPanel"
:disabled="featuresLoading || busy || checking"
@click="loadFeatures"
>
打开悬浮层
</button>
<button
class="flex-1 rounded-antd border border-antd-border bg-white px-3 py-2 text-sm font-medium text-antd-text transition hover:border-antd-primary hover:text-antd-primary disabled:opacity-50"
type="button"
:disabled="busy"
@click="handleTogglePanel"
>
开关切换
{{ featuresLoading ? '加载中…' : '刷新清单' }}
</button>
</div>
<p class="text-[11px] text-antd-text-tertiary">若提示无法连接页面脚本,请先刷新目标网页。</p>
<p v-if="featuresUpdatedAt" class="text-[11px] text-antd-text-tertiary">
清单更新于 {{ featuresUpdatedAt }}
</p>
<p v-if="!features.length" class="text-xs text-antd-text-tertiary">
尚未加载。配置 URL / Key 后可先「检查连通性」,再「刷新清单」。
</p>
<div
v-for="item in matchedFeatures"
:key="item.id"
class="rounded-antd border border-antd-border-secondary bg-antd-fill px-2.5 py-2"
>
<div class="flex items-start justify-between gap-2">
<div class="min-w-0">
<div class="truncate text-sm font-medium text-antd-text">{{ item.name }}</div>
<div class="text-[11px] text-antd-text-tertiary">
{{ item.id }} · v{{ item.version }}
</div>
<p v-if="item.description" class="mt-1 text-[11px] text-antd-text-secondary">
{{ item.description }}
</p>
</div>
<button
class="shrink-0 rounded-antd bg-antd-primary px-2.5 py-1 text-xs font-medium text-white hover:bg-antd-primary-hover disabled:opacity-50"
type="button"
:disabled="busy || checking"
@click="handleOpenFeature(item)"
>
{{ activeFeatureId === item.id ? '重新打开' : '打开' }}
</button>
</div>
</div>
<button
v-if="activeFeatureId"
class="w-full rounded-antd border border-antd-border bg-white px-3 py-1.5 text-sm transition hover:border-antd-primary hover:text-antd-primary disabled:opacity-50"
type="button"
:disabled="busy || checking"
@click="handleCloseFeature"
>
关闭当前功能
</button>
</section>
<p v-if="statusText" class="text-xs text-antd-success">{{ statusText }}</p>
+12 -1
View File
@@ -18,7 +18,7 @@ async function sendToActiveTab<T>(message: ExtensionMessage): Promise<T> {
} catch {
throw new ExtensionError(
'PAGE_NOT_READY',
'无法连接页面脚本。请刷新目标页面后再打开悬浮层。',
'无法连接页面脚本。请刷新目标页面后再打开功能。',
);
}
@@ -48,3 +48,14 @@ export async function toggleFloatingPanel(): Promise<{ visible: boolean }> {
export async function getFloatingPanelState(): Promise<{ visible: boolean }> {
return sendToActiveTab<{ visible: boolean }>({ type: 'GET_FLOATING_PANEL_STATE' });
}
export async function mountFeatureOnPage(featureId: string): Promise<{ featureId: string | null }> {
return sendToActiveTab<{ featureId: string | null }>({
type: 'MOUNT_FEATURE',
payload: { featureId },
});
}
export async function unmountFeatureOnPage(): Promise<{ featureId: string | null }> {
return sendToActiveTab<{ featureId: string | null }>({ type: 'UNMOUNT_FEATURE' });
}