数据库文档编写,开发规范文档,数据库接入

This commit is contained in:
comlibmb
2026-02-02 18:09:30 +08:00
parent 19970db288
commit 21149dd3fe
36 changed files with 3245 additions and 89 deletions

View File

@@ -0,0 +1,20 @@
import { getUserIdOrNull } from './auth.uts'
import { toLoginWithRedirect, getCurrentPageUrlWithQuery } from '@/utils/authRedirect.uts'
export type EnsureLoginOptions = {
redirectUrl?: string
toastTitle?: string
}
export function ensureAnalyticsLogin(opts?: EnsureLoginOptions): boolean {
const uid = getUserIdOrNull()
if (uid != null && uid !== '') return true
const target = (opts?.redirectUrl != null && opts?.redirectUrl?.length > 0)
? opts?.redirectUrl as string
: getCurrentPageUrlWithQuery()
uni.showToast({ title: opts?.toastTitle ?? '请先登录', icon: 'none' })
toLoginWithRedirect(target)
return false
}

View File

@@ -9,8 +9,19 @@ export type CouponAnalysisData = {
conversionList: Array<UTSJSONObject>
}
export async function fetchCouponAnalysis(period: string): Promise<CouponAnalysisData> {
const { startIso, endIso } = computeDateRange(period)
export async function fetchCouponAnalysis(period: string, range?: { start: string; end: string } | null): Promise<CouponAnalysisData> {
let startIso: string;
let endIso: string;
if (range != null && range.start && range.end) {
startIso = range.start;
endIso = range.end;
} else {
const computedRange = computeDateRange(period)
startIso = computedRange.startIso
endIso = computedRange.endIso
}
const p_start_date = toDateOnly(startIso)
const p_end_date = toDateOnly(endIso)

View File

@@ -29,6 +29,7 @@ function safeString(v: any): string {
// 改造:不再直查 analytics_reports 表,统一通过 RPC 获取当前用户的报表列表
export async function listCustomReports(ownerUserId: string): Promise<Array<CustomReportListItem>> {
// rpc_get_custom_reports 基于 auth.uid() 过滤,无需额外参数,这里保留签名用于兼容调用方
const rows = await rpcOrEmptyArray('rpc_get_custom_reports', {} as any)
const list: Array<CustomReportListItem> = []
for (let i = 0; i < rows.length; i++) {
@@ -37,8 +38,7 @@ export async function listCustomReports(ownerUserId: string): Promise<Array<Cust
id: safeString(r.getAny?.('id') ?? r.getString?.('id')),
title: safeString(r.getAny?.('title') ?? r.getString?.('title')),
description: safeString(r.getAny?.('description') ?? r.getString?.('description')),
// 兼容旧 UI 字段custom-report 页面里可能还在用 period 字段
period: '',
period: safeString(r.getAny?.('period') ?? r.getString?.('period')),
updated_at: safeString(r.getAny?.('updated_at') ?? r.getString?.('updated_at'))
})
}

View File

@@ -8,8 +8,18 @@ export type DeliveryAnalysisData = {
endIso: string
}
export async function fetchDeliveryAnalysis(period: string): Promise<DeliveryAnalysisData> {
const { startIso, endIso } = computeDateRange(period)
export async function fetchDeliveryAnalysis(period: string, range?: { start: string; end: string } | null): Promise<DeliveryAnalysisData> {
let startIso: string;
let endIso: string;
if (range != null && range.start && range.end) {
startIso = range.start;
endIso = range.end;
} else {
const computedRange = computeDateRange(period)
startIso = computedRange.startIso
endIso = computedRange.endIso
}
const trendList = await rpcOrEmptyArray('rpc_delivery_efficiency_daily', {
p_start: startIso,

View File

@@ -11,8 +11,19 @@ export type MarketTrendsData = {
endIso: string
}
export async function fetchMarketTrends(period: string): Promise<MarketTrendsData> {
const { startIso, endIso } = computeDateRange(period)
export async function fetchMarketTrends(period: string, range?: { start: string; end: string } | null): Promise<MarketTrendsData> {
let startIso: string;
let endIso: string;
if (range != null && range.start && range.end) {
startIso = range.start;
endIso = range.end;
} else {
const computedRange = computeDateRange(period)
startIso = computedRange.startIso
endIso = computedRange.endIso
}
const startDate = toDateOnly(startIso)
const endDate = toDateOnly(endIso)

View File

@@ -20,8 +20,19 @@ function safeNumber(v: any): number {
return isFinite(n) ? n : 0
}
export async function fetchProductOverview(period: string): Promise<ProductOverview> {
const { startIso, endIso } = computeDateRange(period)
export async function fetchProductOverview(period: string, range?: { start: string; end: string } | null): Promise<ProductOverview> {
let startIso: string;
let endIso: string;
if (range != null && range.start && range.end) {
startIso = range.start;
endIso = range.end;
} else {
const computedRange = computeDateRange(period)
startIso = computedRange.startIso
endIso = computedRange.endIso
}
const row = await rpcOrNull('rpc_product_insights_overview', {
p_start: toDateOnly(startIso),
p_end: toDateOnly(endIso)
@@ -39,8 +50,19 @@ export async function fetchProductOverview(period: string): Promise<ProductOverv
}
}
export async function fetchTopProducts(period: string, limit: number = 10): Promise<Array<ProductRank>> {
const { startIso, endIso } = computeDateRange(period)
export async function fetchTopProducts(period: string, limit: number = 10, range?: { start: string; end: string } | null): Promise<Array<ProductRank>> {
let startIso: string;
let endIso: string;
if (range != null && range.start && range.end) {
startIso = range.start;
endIso = range.end;
} else {
const computedRange = computeDateRange(period)
startIso = computedRange.startIso
endIso = computedRange.endIso
}
const rows = await rpcOrEmptyArray('rpc_analytics_top_products', {
p_start_date: toDateOnly(startIso),
p_end_date: toDateOnly(endIso),
@@ -61,8 +83,19 @@ export async function fetchTopProducts(period: string, limit: number = 10): Prom
return list
}
export async function fetchProductTrend(period: string, productId: string): Promise<Array<ProductTrendRow>> {
const { startIso, endIso } = computeDateRange(period)
export async function fetchProductTrend(period: string, productId: string, range?: { start: string; end: string } | null): Promise<Array<ProductTrendRow>> {
let startIso: string;
let endIso: string;
if (range != null && range.start && range.end) {
startIso = range.start;
endIso = range.end;
} else {
const computedRange = computeDateRange(period)
startIso = computedRange.startIso
endIso = computedRange.endIso
}
const rows = await rpcOrEmptyArray('rpc_analytics_product_trend', {
p_start_date: toDateOnly(startIso),
p_end_date: toDateOnly(endIso),
@@ -83,8 +116,19 @@ export async function fetchProductTrend(period: string, productId: string): Prom
return out
}
export async function fetchCategorySales(period: string): Promise<Array<UTSJSONObject>> {
const { startIso, endIso } = computeDateRange(period)
export async function fetchCategorySales(period: string, range?: { start: string; end: string } | null): Promise<Array<UTSJSONObject>> {
let startIso: string;
let endIso: string;
if (range != null && range.start && range.end) {
startIso = range.start;
endIso = range.end;
} else {
const computedRange = computeDateRange(period)
startIso = computedRange.startIso
endIso = computedRange.endIso
}
return await rpcOrEmptyArray('rpc_analytics_category_sales', {
p_start_date: toDateOnly(startIso),
p_end_date: toDateOnly(endIso)
@@ -95,8 +139,19 @@ export async function fetchStockInsights(period: string): Promise<Array<UTSJSONO
return await rpcOrEmptyArray('rpc_product_insights_stock', {} as any)
}
export async function fetchPriceTrend(period: string): Promise<Array<UTSJSONObject>> {
const { startIso, endIso } = computeDateRange(period)
export async function fetchPriceTrend(period: string, range?: { start: string; end: string } | null): Promise<Array<UTSJSONObject>> {
let startIso: string;
let endIso: string;
if (range != null && range.start && range.end) {
startIso = range.start;
endIso = range.end;
} else {
const computedRange = computeDateRange(period)
startIso = computedRange.startIso
endIso = computedRange.endIso
}
return await rpcOrEmptyArray('rpc_analytics_price_trend', {
p_start: startIso,
p_end: endIso