// 商品浏览停留时长埋点工具 // 用法(商品详情页): // onLoad: startProductDwell() // onUnload: flushProductDwell(this.product) // 后端接口:POST /api/stats/product-view(StatsController.recordProductView) import { reportProductDwell } from './api.js' let _startTs = 0 export function startProductDwell() { _startTs = Date.now() } export function flushProductDwell(product) { var start = _startTs _startTs = 0 if (!product || !product.id) return var duration = Math.round((Date.now() - start) / 1000) if (duration <= 0) return var userId = 0 try { userId = uni.getStorageSync('userId') || 0 } catch (e) {} var payload = { userId: userId, productId: product.id, productName: product.name || '', productType: product.productType || '', durationSeconds: duration, source: 'miniprogram' } try { reportProductDwell(payload) } catch (e) {} }