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'); }); } /** 获取配置数据 */ export function fetchConfig() { return wx.$util.request(wx.$api.CommonGetMiniConfig, {}, 'GET'); } /** 获取首页轮播(分类类型 banner) */ export function fetchHomeBanners() { return wx.$util.request(wx.$api.CatalogList, { type: 'banner' }, 'GET'); } // 与后端 site.platform_commission_ratio 默认值一致,接口失败时兜底 const DEFAULT_COMMISSION_RATIO = 0.1; let commissionRatioCache = null; /** * 获取平台服务费比例(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; } const CASE_VIEW_STORAGE_KEY = 'goodsCaseView'; let caseViewCache = null; /** * 商品是否按案例展示(不显示价格和购买入口) * 成功后缓存;失败时用上次成功的本地记录 * @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; } /** * 计算扣除平台服务费后的预计到手金额(保留两位小数) * @param {number|string} amount 报价/总费用(元) * @param {number} ratio 平台服务费比例 * @returns {number} */ export function calcActualAccount(amount, ratio) { const value = Number(amount) || 0; return Math.round(value * (1 - ratio) * 100) / 100; }