From 559df60f0d4bd7dc589c0b138dee3c181ce100e7 Mon Sep 17 00:00:00 2001 From: qwqcode Date: Sat, 3 Feb 2024 23:12:12 +0800 Subject: [PATCH] fix(ui/pv): missing `pageTitle` field in the PV request --- ui/artalk/src/artalk.ts | 1 - ui/artalk/src/plugins/stat.ts | 15 +++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/ui/artalk/src/artalk.ts b/ui/artalk/src/artalk.ts index fc6a88d2b..ff1d2361a 100644 --- a/ui/artalk/src/artalk.ts +++ b/ui/artalk/src/artalk.ts @@ -115,7 +115,6 @@ export default class Artalk { Stat.initCountWidget({ getApi: () => new Api(convertApiOptions(conf)), siteName: conf.site, - pageKey: conf.pageKey, countEl: conf.countEl, pvEl: conf.pvEl, pvAdd: false diff --git a/ui/artalk/src/plugins/stat.ts b/ui/artalk/src/plugins/stat.ts index eea1eb09a..6225f8063 100644 --- a/ui/artalk/src/plugins/stat.ts +++ b/ui/artalk/src/plugins/stat.ts @@ -5,7 +5,8 @@ export interface CountOptions { getApi(): Api siteName: string - pageKey: string + pageKey?: string + pageTitle?: string countEl: string pvEl: string @@ -15,12 +16,13 @@ export interface CountOptions { export const PvCountWidget: ArtalkPlugin = (ctx: ContextApi) => { ctx.watchConf([ - 'site', 'pageKey', 'countEl', 'pvEl', + 'site', 'pageKey', 'pageTitle', 'countEl', 'pvEl', ], (conf) => { initCountWidget({ getApi: () => ctx.getApi(), siteName: conf.site, pageKey: conf.pageKey, + pageTitle: conf.pageTitle, countEl: conf.countEl, pvEl: conf.pvEl, pvAdd: (typeof ctx.conf.pvAdd === 'boolean' ? ctx.conf.pvAdd : true), @@ -36,10 +38,10 @@ export async function initCountWidget(opt: CountOptions) { } // PV - const initialData = opt.pvAdd ? { + const initialData = (opt.pvAdd && opt.pageKey) ? { [opt.pageKey]: (await opt.getApi().pages.logPv({ page_key: opt.pageKey, - page_title: opt.pageKey, + page_title: opt.pageTitle, site_name: opt.siteName, })).data.pv // pv+1 and get pv count } : undefined @@ -68,7 +70,7 @@ async function refreshStatCount( // Get page keys which will be queried let queryPageKeys = Array.from(document.querySelectorAll(args.numEl)) .map((e) => e.getAttribute('data-page-key') || opt.pageKey) - .filter((k) => typeof data[k] !== 'number') // filter out keys that already have data + .filter((k) => k && typeof data[k] !== 'number') // filter out keys that already have data queryPageKeys = [...new Set(queryPageKeys)] // deduplicate @@ -81,7 +83,8 @@ async function refreshStatCount( data = { ...data, ...res } } - applyCountData(args.numEl, data, data[opt.pageKey]) + const defaultCount = opt.pageKey ? data[opt.pageKey] : 0 + applyCountData(args.numEl, data, defaultCount) } function applyCountData(selector: string, data: CountData, defaultCount: number) {