diff --git a/frontend/src/views/ProductListView.vue b/frontend/src/views/ProductListView.vue index a7d66ee..e4e83bf 100644 --- a/frontend/src/views/ProductListView.vue +++ b/frontend/src/views/ProductListView.vue @@ -21,6 +21,9 @@ import { useDialog, useMessage } from 'naive-ui' import { EventsOff, EventsOn } from '../../wailsjs/runtime/runtime' import { DownloadProductData, + GetCachedShops, + GetShopsUpdatedAt, + RefreshShops, OpenInBrowser, DownloadVideos, ListProducts, @@ -31,12 +34,6 @@ const message = useMessage() const dialog = useDialog() const emit = defineEmits(['open-logs']) -// 新增绑定会由 Wails 在正常开发流程中生成,但生成目录不纳入人工修改范围。 -// 这里通过同一条 Wails 桥接调用,避免手改自动生成文件。 -const GetCachedShops = () => window.go.main.App.GetCachedShops() -const GetShopsUpdatedAt = () => window.go.main.App.GetShopsUpdatedAt() -const RefreshShops = () => window.go.main.App.RefreshShops() - // ---- 筛选条件 ---- // 字段名和 Go 侧 store.ProductQuery 的 json tag 一一对应,改名要两边一起改。 const emptyQuery = () => ({ @@ -76,6 +73,23 @@ const shopsUpdatedAt = ref('') // 平台店铺ID -> 地区码,例 { "1664202094": "TW" } const shopRegions = ref({}) +// 展开店铺下拉时按需刷新。 +// +// 为什么不是每次展开都刷:刷新要先完成货憨憨登录,一次一两秒, +// 每点一下下拉框就卡一下很烦人。所以只有距上次刷新超过下面这个 +// 间隔才真的去请求,其余时候直接用缓存。 +const 店铺刷新间隔毫秒 = 5 * 60 * 1000 +let 上次刷新时刻 = 0 + +function onShopDropdown(展开) { + if (!展开) return + if (shopRefreshing.value || shopLoading.value) return + if (Date.now() - 上次刷新时刻 < 店铺刷新间隔毫秒) return + // 静默刷新:失败不弹窗,下拉框继续用缓存。 + // 启动时的刷新失败才弹窗,那是使用者需要立刻知道的。 + refreshShops({ silent: true }) +} + const shopsUpdatedText = computed(() => shopsUpdatedAt.value ? `更新于 ${formatCachedAt(shopsUpdatedAt.value)}` : '尚未同步' ) @@ -139,14 +153,23 @@ function onShopsRefreshFailed(payload) { showRefreshFailure(payload) } -async function refreshShops() { +async function refreshShops({ silent = false } = {}) { shopRefreshing.value = true + 上次刷新时刻 = Date.now() try { const shops = await RefreshShops() applyShops(shops) shopsUpdatedAt.value = shops?.[0]?.updatedAt || (await GetShopsUpdatedAt()) || '' - message.success(`店铺列表已更新,共 ${shops?.length || 0} 个店铺`) + if (!silent) { + message.success(`店铺列表已更新,共 ${shops?.length || 0} 个店铺`) + } } catch (err) { + // 展开下拉时的自动刷新失败不打扰使用者:下拉框继续用缓存, + // 旁边的「更新于 …」会显示成旧时间,看得出来数据不是最新的。 + // 启动时的刷新失败才弹窗,那是需要立刻知道的。 + if (silent) { + return + } let cachedAt = shopsUpdatedAt.value try { cachedAt = (await GetShopsUpdatedAt()) || '' @@ -163,6 +186,7 @@ async function refreshShops() { } } + // ---- 表格 ---- const rows = ref([]) const total = ref(0) @@ -373,20 +397,13 @@ onUnmounted(() => { {{ shopsUpdatedText }} - - 刷新店铺 - 蝦皮ID