Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 16x 16x 16x 16x 47x 36x 36x 36x 22x 11x 11x 11x 11x 1x 10x 1x 9x 28x 168x 117x 8x 8x 8x 14x 14x 14x 8x 8x 8x 7x 7x 7x 8x 8x 8x 1x 12x 7x 7x 7x 7x 42x 13x 9x 4x 3x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 14x 1x 13x 19x 7x 1x 14x 14x 14x 14x 14x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 11x 11x 11x 11x 2x 2x 9x 9x 9x 9x 9x 9x 9x 8x 8x 1x 5x 5x 3x 2x 9x 7x 8x | import { computed, reactive, ref } from "vue";
import { defineStore } from "pinia";
import {
createAnalyticsApi,
createApiClient,
type AnalyticsApi,
type AnalyticsDashboard,
type AnalyticsDailySummary,
type AnalyticsOverviewTotals,
type AnalyticsProductSummary,
type AnalyticsProjectionFreshness,
type BusinessId,
} from "@plain-journal/foundation";
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL?.trim() ?? "";
const PRODUCT_LIMIT = 8;
const MAXIMUM_RANGE_DAYS = 366;
const DAY_MILLISECONDS = 86_400_000;
const COUNT_KEYS = [
"createdOrderCount",
"paymentCount",
"completedOrderCount",
"closedOrderCount",
"afterSaleCount",
"refundCount",
] as const;
const MONEY_KEYS = [
"createdOrderAmount",
"paymentAmount",
"completedOrderAmount",
"afterSaleAmount",
"refundAmount",
] as const;
export interface AdminAnalyticsAccessContext {
authorized: boolean;
operatorId: BusinessId | null;
accessToken: string | null;
}
interface ActiveAccess {
operatorId: BusinessId;
accessToken: string;
revision: number;
api: AnalyticsApi;
}
export class AnalyticsAccessChangedError extends Error {
constructor() {
super("员工账户或会话已切换,旧的运营投影不会写入当前页面。");
this.name = "AnalyticsAccessChangedError";
}
}
export class AnalyticsProjectionContractError extends Error {
constructor(message: string) {
super(message);
this.name = "AnalyticsProjectionContractError";
}
}
function localDate(daysBefore = 0): string {
const value = new Date();
value.setDate(value.getDate() - daysBefore);
const offset = value.getTimezoneOffset() * 60_000;
return new Date(value.getTime() - offset).toISOString().slice(0, 10);
}
function isBusinessId(value: unknown): value is BusinessId {
return typeof value === "string" && /^[0-9]+$/u.test(value);
}
function isDate(value: unknown): value is string {
Iif (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/u.test(value)) {
return false;
}
const timestamp = Date.parse(`${value}T00:00:00Z`);
return Number.isFinite(timestamp)
&& new Date(timestamp).toISOString().slice(0, 10) === value;
}
function dateTimestamp(value: string): number {
return Date.parse(`${value}T00:00:00Z`);
}
function rangeDays(from: string, to: string): number {
return Math.floor(
(dateTimestamp(to) - dateTimestamp(from)) / DAY_MILLISECONDS,
) + 1;
}
function validateRange(from: string, to: string): number {
Iif (!isDate(from) || !isDate(to)) {
throw new AnalyticsProjectionContractError(
"运营统计日期必须使用有效的 YYYY-MM-DD 日历日期。",
);
}
const days = rangeDays(from, to);
if (days < 1) {
throw new AnalyticsProjectionContractError(
"运营统计开始日期不能晚于结束日期。",
);
}
if (days > MAXIMUM_RANGE_DAYS) {
throw new AnalyticsProjectionContractError(
`运营统计单次读取不能超过 ${MAXIMUM_RANGE_DAYS} 天。`,
);
}
return days;
}
function isInstant(value: unknown): value is string {
return typeof value === "string"
&& value.length > 0
&& Number.isFinite(Date.parse(value));
}
function isNonNegativeCount(value: unknown): value is number {
return Number.isSafeInteger(value) && Number(value) >= 0;
}
function isMoney(value: unknown): value is number {
return typeof value === "number"
&& Number.isFinite(value)
&& value >= 0;
}
function validTotals(value: unknown): value is AnalyticsOverviewTotals {
Iif (!value || typeof value !== "object") {
return false;
}
const candidate = value as Partial<AnalyticsOverviewTotals>;
return COUNT_KEYS.every((key) => isNonNegativeCount(candidate[key]))
&& MONEY_KEYS.every((key) => isMoney(candidate[key]))
&& isNonNegativeCount(candidate.uniqueCustomers);
}
function validDailySummary(value: unknown): value is AnalyticsDailySummary {
Iif (!value || typeof value !== "object") {
return false;
}
const candidate = value as Partial<AnalyticsDailySummary>;
return isDate(candidate.businessDate)
&& COUNT_KEYS.every((key) => isNonNegativeCount(candidate[key]))
&& MONEY_KEYS.every((key) => isMoney(candidate[key]))
&& isInstant(candidate.updatedAt);
}
function validProductSummary(
value: unknown,
): value is AnalyticsProductSummary {
Iif (!value || typeof value !== "object") {
return false;
}
const candidate = value as Partial<AnalyticsProductSummary>;
return isBusinessId(candidate.productId)
&& typeof candidate.productTitle === "string"
&& candidate.productTitle.trim().length > 0
&& isNonNegativeCount(candidate.completedOrderCount)
&& isNonNegativeCount(candidate.unitsSold)
&& isMoney(candidate.netRevenue)
&& isNonNegativeCount(candidate.revenueCoveredOrderCount)
&& Number(candidate.revenueCoveredOrderCount)
<= Number(candidate.completedOrderCount);
}
function validFreshness(
value: unknown,
): value is AnalyticsProjectionFreshness {
Iif (!value || typeof value !== "object") {
return false;
}
const candidate = value as Partial<AnalyticsProjectionFreshness>;
return isNonNegativeCount(candidate.sourceEventCount)
&& (
candidate.lastConsumedAt === null
|| isInstant(candidate.lastConsumedAt)
)
&& isInstant(candidate.generatedAt);
}
function validateDashboard(
value: AnalyticsDashboard,
expectedFrom: string,
expectedTo: string,
expectedDays: number,
): AnalyticsDashboard {
Iif (!value || typeof value !== "object") {
throw new AnalyticsProjectionContractError(
"Analytics 运营投影缺少统一对象结构。",
);
}
const candidate = value as Partial<AnalyticsDashboard>;
if (
candidate.from !== expectedFrom
|| candidate.to !== expectedTo
|| !validTotals(candidate.totals)
|| !Array.isArray(candidate.daily)
|| !candidate.daily.every(validDailySummary)
|| candidate.daily.length > expectedDays
|| !Array.isArray(candidate.topProducts)
|| !candidate.topProducts.every(validProductSummary)
|| candidate.topProducts.length > PRODUCT_LIMIT
|| !validFreshness(candidate.freshness)
) {
throw new AnalyticsProjectionContractError(
"Analytics 运营投影与当前日期、计数、金额或字符串身份契约不一致。",
);
}
const dailyDates = candidate.daily.map((summary) => summary.businessDate);
const sortedDailyDates = [...dailyDates].sort();
Iif (
new Set(dailyDates).size !== dailyDates.length
|| dailyDates.some((date) => date < expectedFrom || date > expectedTo)
|| dailyDates.some((date, index) => date !== sortedDailyDates[index])
|| new Set(candidate.topProducts.map((product) => product.productId)).size
!== candidate.topProducts.length
) {
throw new AnalyticsProjectionContractError(
"Analytics 日汇总或商品汇总包含重复、乱序或越界事实。",
);
}
return candidate as AnalyticsDashboard;
}
function isActiveContext(
context: AdminAnalyticsAccessContext,
): context is {
authorized: true;
operatorId: BusinessId;
accessToken: string;
} {
return context.authorized
&& isBusinessId(context.operatorId)
&& typeof context.accessToken === "string"
&& context.accessToken.length > 0;
}
function createApi(accessToken: string): AnalyticsApi {
return createAnalyticsApi(createApiClient({
baseUrl: apiBaseUrl,
timeoutMs: 10000,
tokenProvider: () => accessToken,
}));
}
function errorMessage(cause: unknown): string {
return cause instanceof Error
? cause.message
: "运营统计暂时无法读取。";
}
export const useAdminAnalyticsStore = defineStore("admin-analytics", () => {
const dashboard = ref<AnalyticsDashboard | null>(null);
const range = reactive({
from: localDate(29),
to: localDate(),
});
const loading = ref(false);
const error = ref<string | null>(null);
const refreshedAt = ref<string | null>(null);
const activeOperatorId = ref<BusinessId | null>(null);
let activeAccessToken: string | null = null;
let accessRevision = 0;
let requestRevision = 0;
const hasKnownProjection = computed(() => dashboard.value !== null);
const recentDaily = computed(() =>
dashboard.value?.daily.slice(-7).reverse() ?? []);
function activeAccess(
context: AdminAnalyticsAccessContext,
): ActiveAccess | null {
if (!isActiveContext(context)) {
return null;
}
return {
operatorId: context.operatorId,
accessToken: context.accessToken,
revision: accessRevision,
api: createApi(context.accessToken),
};
}
function accessIsCurrent(access: ActiveAccess): boolean {
return access.revision === accessRevision
&& access.operatorId === activeOperatorId.value
&& access.accessToken === activeAccessToken;
}
function requireCurrent(access: ActiveAccess) {
if (!accessIsCurrent(access)) {
throw new AnalyticsAccessChangedError();
}
}
function synchronizeAccess(context: AdminAnalyticsAccessContext) {
const nextOperatorId = isActiveContext(context) ? context.operatorId : null;
const nextAccessToken = isActiveContext(context) ? context.accessToken : null;
const operatorChanged = activeOperatorId.value !== nextOperatorId;
const accessChanged = operatorChanged || activeAccessToken !== nextAccessToken;
if (!accessChanged) {
return activeAccess(context);
}
activeOperatorId.value = nextOperatorId;
activeAccessToken = nextAccessToken;
accessRevision += 1;
requestRevision += 1;
loading.value = false;
Eif (operatorChanged) {
dashboard.value = null;
error.value = null;
refreshedAt.value = null;
}
return activeAccess(context);
}
async function load(
context: AdminAnalyticsAccessContext,
): Promise<void> {
const access = synchronizeAccess(context);
Iif (!access) {
error.value = "当前会话无权读取平台运营统计。";
return;
}
let expectedDays: number;
try {
expectedDays = validateRange(range.from, range.to);
} catch (cause) {
error.value = errorMessage(cause);
return;
}
const expectedFrom = range.from;
const expectedTo = range.to;
const currentRequest = ++requestRevision;
loading.value = true;
error.value = null;
try {
const value = validateDashboard(
await access.api.overview(
expectedFrom,
expectedTo,
PRODUCT_LIMIT,
),
expectedFrom,
expectedTo,
expectedDays,
);
requireCurrent(access);
if (currentRequest !== requestRevision) {
return;
}
dashboard.value = value;
refreshedAt.value = new Date().toISOString();
} catch (cause) {
if (accessIsCurrent(access) && currentRequest === requestRevision) {
error.value = errorMessage(cause);
}
} finally {
if (accessIsCurrent(access) && currentRequest === requestRevision) {
loading.value = false;
}
}
}
return {
dashboard,
range,
loading,
error,
refreshedAt,
hasKnownProjection,
recentDaily,
synchronizeAccess,
load,
};
});
|