import Taro from '@tarojs/taro'; import { CacheKey } from '@/constants/cache-key'; import http from '@/http'; import { API } from '@/http/api'; import store from '@/store'; import { setInviteCode } from '@/store/actions/partner'; import { GetProfitRequest, InviteUserInfo, PartnerInviteCode, PartnerProfitItem, PartnerProfitsState, } from '@/types/partner'; import { requestUserInfo } from '@/utils/user'; export const getInviteCodeFromQuery = (query: Record): string | undefined => { if (query) { if (query.scene) { return query.scene.replace('c%3D', ''); } if (query.c) { return query.c; } } return undefined; }; export const getInviteCodeFromQueryAndUpdate = (query: Record) => { const code = getInviteCodeFromQuery(query); if (!code) { return; } console.log('get code', code); requestUserInfo(code); }; export const getPartnerBannerClose = (): boolean => Taro.getStorageSync(CacheKey.CLOSE_PARTNER_BANNER); export const setPartnerBannerClose = () => Taro.setStorageSync(CacheKey.CLOSE_PARTNER_BANNER, true); export const becomePartner = async () => { await http.post(API.BECOME_PARTNER); await getInviteCode(); }; export const getPartnerQrcode = async () => { return await http.post(API.PARTNER_QRCODE, { responseType: 'arraybuffer', }); }; export const getPartnerProfitStat = async () => { return await http.post(API.GET_PROFIT_STAT); }; export const getPartnerInviteList = async () => { return await http.post(API.GET_INVITE_LIST); }; export const dispatchUpdateInviteCode = (code: string) => store.dispatch(setInviteCode(code)); export const getInviteCode = async () => { const { inviteCode } = await http.post(API.GET_INVITE_CODE); dispatchUpdateInviteCode(inviteCode); return inviteCode; }; export const getProfitList = async (data: GetProfitRequest) => { return await http.post(API.GET_PROFIT_LIST, { data }); }; export const formatMoney = (cents: number) => { if (!cents) { return '0'; } const yuan = cents / 100; return yuan.toFixed(2); }; export function formatTimestamp(timestamp: string): string { // 将字符串时间戳转换为数字类型 const time = Number(timestamp); // 创建 Date 对象 const date = new Date(time); // 获取年、月、日、时、分、秒 const YYYY = date.getFullYear(); const MM = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1 const DD = String(date.getDate()).padStart(2, '0'); const HH = String(date.getHours()).padStart(2, '0'); const mm = String(date.getMinutes()).padStart(2, '0'); const ss = String(date.getSeconds()).padStart(2, '0'); // 拼接成所需的格式 return `${YYYY}.${MM}.${DD} ${HH}:${mm}:${ss}`; } export function formatUserId(input: string): string { const length = input.length; // 如果长度小于8,隐藏最后4位 if (length < 8) { const beforeMask = input.slice(0, length - 4); // 前部分 const maskedPart = '****'; // 替换最后4位为星号 return beforeMask + maskedPart; } // 如果长度大于或等于8,隐藏倒数第五位到倒数第二位 const start = length - 8; // 倒数第八个字符的索引 const beforeMask = input.slice(0, start); // 前部分 const maskedPart = '****'; // 替换倒数第五位到倒数第二位为星号 const afterMask = input.slice(start + 4); // 后部分 // 拼接结果 return beforeMask + maskedPart + afterMask; }