Files
code-bang/frontend/miniapp/services/home/home.js
T
2026-10-14 15:46:49 +08:00

95 lines
2.3 KiB
JavaScript

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');
}
// 与后端 site.platform_commission_ratio 默认值一致,接口失败时兜底
const DEFAULT_COMMISSION_RATIO = 0.1;
let commissionRatioCache = null;
/**
* 获取平台服务费比例(0~1),成功后缓存,失败返回默认值且不缓存,下次重新请求
* @returns {Promise<number>}
*/
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;
}
/**
* 计算扣除平台服务费后的预计到手金额(保留两位小数)
* @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;
}