import { config, cdnBase } from '../../config/index'; /** 获取首页数据 */ function mockFetchHome() { const { delay } = require('../_utils/delay'); const { genSwiperImageList } = require('../../model/swiper'); return delay().then(() => { return { swiper: genSwiperImageList(), tabList: [ { text: '精选推荐', key: 0, }, { text: '夏日防晒', key: 1, }, { text: '二胎大作战', key: 2, }, { text: '人气榜', key: 3, }, { text: '好评榜', key: 4, }, { text: 'RTX 30', key: 5, }, { text: '手机也疯狂', key: 6, }, ], activityImg: `${cdnBase}/activity/banner.png`, }; }); } /** 获取首页数据 */ export function fetchHome() { if (config.useMock) { return mockFetchHome(); } return new Promise((resolve) => { resolve('real api'); }); } const CONFIG_TTL = 60000; let configCache = null; let configTask = null; // 与后端 site.platform_commission_ratio 默认值一致,接口失败时兜底 const DEFAULT_COMMISSION_RATIO = 0.1; let commissionRatioCache = null; const CASE_VIEW_STORAGE_KEY = 'goodsCaseView'; let caseViewCache = null; /** * 获取小程序配置。成功结果缓存 60 秒,同时进行中的请求合并成一次 * @param {{force?: boolean}} options force 为 true 时忽略缓存(下拉刷新) */ export function fetchConfig(options = {}) { const force = options === true || options.force === true; if (configTask) { return configTask; } if (!force && configCache && Date.now() - configCache.at < CONFIG_TTL) { return Promise.resolve(configCache.res); } configTask = wx.$util.request(wx.$api.CommonGetMiniConfig, {}, 'GET', { auth: false }).then((res) => { if (res && res.code === 1) { configCache = { at: Date.now(), res }; const ratio = Number(res.data?.platform_commission_ratio); if (ratio >= 0 && ratio <= 1) { commissionRatioCache = ratio; } caseViewCache = res.data?.goods_case_view === true || res.data?.goods_case_view === 1; wx.setStorageSync(CASE_VIEW_STORAGE_KEY, caseViewCache); } return res; }).then((res) => { configTask = null; return res; }, (err) => { configTask = null; throw err; }); return configTask; } /** 获取首页轮播(分类类型 banner) */ export function fetchHomeBanners() { return wx.$util.request(wx.$api.CatalogList, { type: 'banner' }, 'GET', { auth: false }); } /** * 获取平台服务费比例(0~1),成功后缓存,失败返回默认值且不缓存,下次重新请求 * @returns {Promise} */ export async function fetchPlatformCommissionRatio() { if (commissionRatioCache !== null) { return commissionRatioCache; } try { const res = await fetchConfig(); const ratio = Number(res?.data?.platform_commission_ratio); if (res.code === 1 && ratio >= 0 && ratio <= 1) { commissionRatioCache = ratio; return ratio; } } catch (err) { console.error('fetchPlatformCommissionRatio failed:', err); } return DEFAULT_COMMISSION_RATIO; } /** * 商品是否按案例展示(不显示价格和购买入口) * 成功后缓存;失败时用上次成功的本地记录 * @returns {Promise} */ export async function fetchGoodsCaseView() { if (caseViewCache !== null) { return caseViewCache; } try { const res = await fetchConfig(); if (res.code === 1) { caseViewCache = res.data?.goods_case_view === true || res.data?.goods_case_view === 1; wx.setStorageSync(CASE_VIEW_STORAGE_KEY, caseViewCache); return caseViewCache; } } catch (err) { console.error('fetchGoodsCaseView failed:', err); } return wx.getStorageSync(CASE_VIEW_STORAGE_KEY) === true; } /** * 预计到手 = 金额 × (1 - 平台服务费 - 支付费率),保留两位小数 * @param {number|string} amount 报价/总费用(元) * @param {number} ratio 平台服务费比例 * @param {number} payFeeRate 安卓或 iOS 支付费率 * @returns {number} */ export function calcActualAccount(amount, ratio, payFeeRate = 0) { const value = Number(amount) || 0; const remain = 1 - (Number(ratio) || 0) - (Number(payFeeRate) || 0); return Math.round(value * (remain > 0 ? remain : 0) * 100) / 100; } /** * 平台服务费和两端支付费率。费率来自 getMiniConfig,失败时用 1% / 12% * @returns {Promise<{commissionRatio:number, androidPayFeeRate:number, iosPayFeeRate:number}>} */ export async function fetchIncomeRates() { const commissionRatio = await fetchPlatformCommissionRatio(); let androidPayFeeRate = 0.01; let iosPayFeeRate = 0.12; try { const res = await fetchConfig(); if (res && res.code === 1) { const android = Number(res.data?.android_pay_fee_rate); const ios = Number(res.data?.ios_pay_fee_rate); if (android >= 0 && android < 1) { androidPayFeeRate = android; } if (ios >= 0 && ios < 1) { iosPayFeeRate = ios; } } } catch (err) { console.error('fetchIncomeRates failed:', err); } return { commissionRatio, androidPayFeeRate, iosPayFeeRate }; } /** 比例转百分比,1% 显示为 1,12% 显示为 12 */ export function toPercent(rate) { return Math.round((Number(rate) || 0) * 10000) / 100; }