2026-01-21 17:05:30 +08:00
|
|
|
import { http } from './config';
|
2026-01-28 17:18:43 +08:00
|
|
|
import Big from 'big.js';
|
2026-02-05 16:02:34 +08:00
|
|
|
import dayjs from 'dayjs';
|
2025-11-28 16:49:36 +08:00
|
|
|
|
|
|
|
|
export const formatTime = (date: Date) => {
|
|
|
|
|
const year = date.getFullYear();
|
|
|
|
|
const month = date.getMonth() + 1;
|
|
|
|
|
const day = date.getDate();
|
|
|
|
|
const hour = date.getHours();
|
|
|
|
|
const minute = date.getMinutes();
|
|
|
|
|
const second = date.getSeconds();
|
|
|
|
|
|
2026-01-21 17:05:30 +08:00
|
|
|
return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second]
|
2026-01-14 16:54:47 +08:00
|
|
|
.map(formatNumber)
|
2026-01-21 17:05:30 +08:00
|
|
|
.join(':')}`;
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const formatNumber = (n: number | string) => {
|
|
|
|
|
n = n.toString();
|
|
|
|
|
return n[1] ? n : `0${n}`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 对话框
|
2026-01-28 17:18:43 +08:00
|
|
|
export const showModal = (option: { title?: string; content: string }) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
|
wx.showModal({
|
2026-01-28 17:18:43 +08:00
|
|
|
title: option.title || '系统提示',
|
|
|
|
|
content: option.content,
|
2025-11-28 16:49:36 +08:00
|
|
|
success(res) {
|
|
|
|
|
if (res.confirm) {
|
|
|
|
|
resolve();
|
|
|
|
|
} else if (res.cancel) {
|
|
|
|
|
reject();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 判断数据是不是数组类型 */
|
2026-01-14 16:54:47 +08:00
|
|
|
export const isArray = (data: any) => data && Array.isArray(data);
|
2025-11-28 16:49:36 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 转成数组
|
|
|
|
|
* @param {*} data
|
|
|
|
|
* @returns {Array}
|
|
|
|
|
*/
|
2026-01-14 16:54:47 +08:00
|
|
|
export const toArray = (data: any): any[] => (isArray(data) ? data : []);
|
2025-11-28 16:49:36 +08:00
|
|
|
|
|
|
|
|
/** 判断数据是不是对象类型 */
|
|
|
|
|
export const isObject = (data: any) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
return data && `${Object.prototype.toString.call(data)}`.includes('Object');
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
2026-01-14 16:54:47 +08:00
|
|
|
export const toObject = (data: any) => (isObject(data) ? data : {});
|
2025-11-28 16:49:36 +08:00
|
|
|
|
|
|
|
|
export const reloadPage = () => {
|
|
|
|
|
let pages = getCurrentPages(); //获取加载的页面
|
|
|
|
|
let currentPage = pages[pages.length - 1]; //获取当前页面的对象
|
|
|
|
|
let url = currentPage.route; //当前页面url
|
|
|
|
|
// 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.redirectTo({ url: '/' + url });
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 判断是json数据 */
|
|
|
|
|
export const isJson = (str: any) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
if (str && typeof str == 'string') {
|
2025-11-28 16:49:36 +08:00
|
|
|
try {
|
|
|
|
|
const obj = JSON.parse(str);
|
2026-01-21 17:05:30 +08:00
|
|
|
return obj && typeof obj == 'object';
|
2025-11-28 16:49:36 +08:00
|
|
|
} catch (_e) {
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 解码json数据
|
|
|
|
|
* @param data 数据
|
|
|
|
|
* @returns array | object | null
|
|
|
|
|
*/
|
|
|
|
|
export const jsonParse = (data: any): any[] | object | null => {
|
|
|
|
|
if (data) {
|
2026-01-21 17:05:30 +08:00
|
|
|
if (typeof data == 'string') {
|
2025-11-28 16:49:36 +08:00
|
|
|
try {
|
|
|
|
|
const obj = JSON.parse(data);
|
2026-01-21 17:05:30 +08:00
|
|
|
if (['Array', 'Object'].includes(Object.prototype.toString.call(obj).slice(8, -1))) {
|
2025-11-28 16:49:36 +08:00
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
//
|
|
|
|
|
}
|
2026-01-21 17:05:30 +08:00
|
|
|
} else if (['Array', 'Object'].includes(Object.prototype.toString.call(data).slice(8, -1))) {
|
2025-11-28 16:49:36 +08:00
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断是数字
|
|
|
|
|
* @param {*} value
|
|
|
|
|
* @returns Boolean
|
|
|
|
|
*/
|
|
|
|
|
export const isNumber = (value: any) => {
|
|
|
|
|
return !isNaN(parseFloat(value)) && isFinite(value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 转成数字
|
|
|
|
|
* @param {*} str
|
|
|
|
|
* @returns Number
|
|
|
|
|
*/
|
|
|
|
|
export const toNumber = (str: any) => {
|
|
|
|
|
return isNumber(str) ? Number(str) : 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保留小数
|
|
|
|
|
* @param {*} str 文本
|
|
|
|
|
* @param {*} num 位数
|
|
|
|
|
*/
|
|
|
|
|
export const formatToDecimals = (str: any, num: number = 2) => {
|
|
|
|
|
num = num === undefined ? 2 : toNumber(num);
|
|
|
|
|
return toNumber(str).toFixed(num);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 获取权限 */
|
2026-01-21 17:05:30 +08:00
|
|
|
export const getAuthInfo = () => toObject(wx.getStorageSync('auth_info'));
|
2025-11-28 16:49:36 +08:00
|
|
|
|
|
|
|
|
export const goIndexPage = () => {
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.navigateTo({ url: '/pages/index/index' });
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isImageFile = (path: string) => {
|
|
|
|
|
return /\.(jpe?g|png|gif|svg|webp|bmp)$/i.test(path);
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-04 11:42:19 +08:00
|
|
|
export const isPDFFile = (path: string) => {
|
|
|
|
|
return /\.(pdf)$/i.test(path);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 判断是不是Excel */
|
|
|
|
|
export const isExcelFile = (path: string) => {
|
|
|
|
|
return /\.(xlsx|xls)$/i.test(path);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 获取文件类型 */
|
|
|
|
|
export const getFileType = (path: string): 'file' | 'image' | 'excel' | 'pdf' => {
|
|
|
|
|
if (typeof path == 'string') {
|
|
|
|
|
if (isExcelFile(path)) {
|
|
|
|
|
return 'excel';
|
|
|
|
|
} else if (isImageFile(path)) {
|
|
|
|
|
return 'image';
|
|
|
|
|
} else if (isPDFFile(path)) {
|
|
|
|
|
return 'pdf';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 'file';
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-28 16:49:36 +08:00
|
|
|
/** 滚动到顶部 */
|
|
|
|
|
export const scrollToTop = () => {
|
|
|
|
|
wx.pageScrollTo({ scrollTop: 0 });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 设置tabs配置 */
|
|
|
|
|
export const tabsConfigSet = (key: string, value: any) => {
|
|
|
|
|
const tabsConfig = toObject(tabsConfigGet());
|
|
|
|
|
tabsConfig[key] = value;
|
2026-01-21 17:05:30 +08:00
|
|
|
setStorage('tabsConfig', tabsConfig);
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 获取tabs配置 */
|
|
|
|
|
export const tabsConfigGet = () => {
|
2026-01-21 17:05:30 +08:00
|
|
|
return getStorage('tabsConfig');
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 媒体图片预览
|
|
|
|
|
* @param {*} imageUrls 图片地址数据
|
|
|
|
|
* @param {*} currentUrl 当前数据地址
|
|
|
|
|
*/
|
|
|
|
|
export const mediaPreview = (imageUrls: any[], currentUrl: string) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
console.log('媒体图片预览方法');
|
2026-01-14 16:54:47 +08:00
|
|
|
const lastLen =
|
2026-01-21 17:05:30 +08:00
|
|
|
`${currentUrl}`.indexOf('?') > -1 ? `${currentUrl}`.indexOf('?') : `${currentUrl}`.length;
|
2025-11-28 16:49:36 +08:00
|
|
|
|
2026-01-14 16:54:47 +08:00
|
|
|
const suffix: any = currentUrl
|
2026-01-21 17:05:30 +08:00
|
|
|
.substring(currentUrl.lastIndexOf('.') + 1, lastLen)
|
2026-01-14 16:54:47 +08:00
|
|
|
.toLocaleLowerCase();
|
2025-11-28 16:49:36 +08:00
|
|
|
|
2026-01-21 17:05:30 +08:00
|
|
|
if (isImageFile('.' + suffix)) {
|
2026-01-14 16:54:47 +08:00
|
|
|
wx.previewImage({ urls: imageUrls, current: currentUrl, showmenu: true });
|
2026-01-21 17:05:30 +08:00
|
|
|
} else if (['mp3', 'mp4', 'm4a'].includes(suffix)) {
|
2025-11-28 16:49:36 +08:00
|
|
|
wx.previewMedia({
|
2026-01-21 17:05:30 +08:00
|
|
|
sources: [{ url: currentUrl, type: 'video' }],
|
2025-11-28 16:49:36 +08:00
|
|
|
fail() {
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.showToast({ title: '播放失败' });
|
2025-11-28 16:49:36 +08:00
|
|
|
},
|
|
|
|
|
});
|
2026-01-21 17:05:30 +08:00
|
|
|
} else if (['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'].includes(suffix)) {
|
2025-11-28 16:49:36 +08:00
|
|
|
const downloadTask = wx.downloadFile({
|
|
|
|
|
url: currentUrl,
|
|
|
|
|
header: http.header,
|
2026-01-14 16:54:47 +08:00
|
|
|
success: (res) => {
|
2025-11-28 16:49:36 +08:00
|
|
|
const filePath = res.tempFilePath;
|
|
|
|
|
wx.openDocument({
|
|
|
|
|
filePath: filePath,
|
|
|
|
|
showMenu: true,
|
|
|
|
|
fileType: suffix,
|
2026-01-14 16:54:47 +08:00
|
|
|
success: () => {},
|
2025-11-28 16:49:36 +08:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
fail() {},
|
|
|
|
|
complete() {
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
downloadTask.onProgressUpdate((res) => {
|
|
|
|
|
wx.showLoading({
|
|
|
|
|
title: `${res.progress}%`,
|
|
|
|
|
});
|
|
|
|
|
if (res.progress == 100) {
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.showToast({ title: '请前往网页端下载查看' });
|
2025-11-28 16:49:36 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 文件上传封装
|
|
|
|
|
* @param {*} option {filePath: '文件路径', name: 'files', url:'上传url',formData: {额外data},success: (data)=>{}, complete:()=>{},fail: ()=>{} }
|
|
|
|
|
*/
|
|
|
|
|
export const uploadFile2 = (option: any) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
console.log('文件上传封装');
|
2025-11-28 16:49:36 +08:00
|
|
|
option = toObject(option);
|
2026-01-21 17:05:30 +08:00
|
|
|
option.name = option.name || 'files';
|
2025-11-28 16:49:36 +08:00
|
|
|
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.showLoading({ title: '文件上传中...' });
|
2025-11-28 16:49:36 +08:00
|
|
|
wx.uploadFile({
|
|
|
|
|
filePath: option.filePath,
|
|
|
|
|
name: option.name,
|
|
|
|
|
header: {
|
|
|
|
|
...http.header,
|
2026-01-21 17:05:30 +08:00
|
|
|
cookie: 'FREESESSID=' + wx.getStorageSync('session_id'),
|
2025-11-28 16:49:36 +08:00
|
|
|
},
|
|
|
|
|
formData: option.formData,
|
|
|
|
|
url: option.url,
|
|
|
|
|
success(res) {
|
|
|
|
|
const resData = jsonParse(res.data);
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.showToast({ title: '上传成功' });
|
2025-11-28 16:49:36 +08:00
|
|
|
if (option.success) {
|
|
|
|
|
option.success(resData);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail() {
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.showToast({ title: '上传失败' });
|
2025-11-28 16:49:36 +08:00
|
|
|
if (option.fail) {
|
|
|
|
|
option.fail();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
complete() {
|
|
|
|
|
wx.hideLoading();
|
|
|
|
|
if (option.complete) {
|
|
|
|
|
option.complete();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取dataset
|
|
|
|
|
* @param {*} e
|
|
|
|
|
*/
|
|
|
|
|
export const getDataSet = (e: any) => {
|
|
|
|
|
return e.currentTarget.dataset;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const toastSuccess = (title: string) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.showToast({ title: `${title}`, icon: 'success' });
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const toastError = (title: string) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.showToast({ title: `${title}`, icon: 'error' });
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const toastSuccessNotIcon = (title: string) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
wx.showToast({ title: `${title}`, icon: 'none' });
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getEnvVersion = wx.getAccountInfoSync().miniProgram.envVersion;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取缓存数据根据小程序版本
|
|
|
|
|
* @param key string
|
|
|
|
|
*/
|
|
|
|
|
export const getStorage = (key: string) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
// return wx.getStorageSync(`${getEnvVersion}_${key}`);
|
|
|
|
|
return wx.getStorageSync(key);
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置缓存数据根据小程序版本
|
|
|
|
|
* @param key string
|
|
|
|
|
* @param data any
|
|
|
|
|
*/
|
|
|
|
|
export const setStorage = (key: string, data: any) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
// return wx.setStorageSync(`${getEnvVersion}_${key}`, data);
|
|
|
|
|
return wx.setStorageSync(key, data);
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移除缓存数据根据小程序版本
|
|
|
|
|
* @param key string
|
|
|
|
|
*/
|
|
|
|
|
export const removeStorage = (key: string) => {
|
2026-01-21 17:05:30 +08:00
|
|
|
// return wx.removeStorageSync(`${getEnvVersion}_${key}`);
|
|
|
|
|
return wx.removeStorageSync(key);
|
2025-11-28 16:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前页面路由
|
|
|
|
|
*/
|
|
|
|
|
export const getCurrentPageRoute = () => {
|
|
|
|
|
const pages = getCurrentPages(); // 获取页面栈
|
|
|
|
|
const currentPage = pages[pages.length - 1]; // 获取当前页面对象
|
|
|
|
|
return `/${currentPage.route}`; // 获取当前页面路径
|
|
|
|
|
};
|
2026-01-28 17:18:43 +08:00
|
|
|
|
|
|
|
|
/** 价格保留位数 */
|
|
|
|
|
export const priceRetentionDigits = (value?: number) => {
|
|
|
|
|
return toNumber(
|
|
|
|
|
Big(toNumber(value)).toFixed(toNumber(wx.getStorageSync('PRICE_HOLD_POINT') || 2)),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 价格保留位数字符串类型 */
|
|
|
|
|
export const priceRetentionDigitsString = (value?: number) => {
|
|
|
|
|
return Big(toNumber(value)).toFixed(toNumber(wx.getStorageSync('PRICE_HOLD_POINT') || 2));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 数量保留位数 */
|
|
|
|
|
export const numRetentionDigits = (value?: number) => {
|
|
|
|
|
return toNumber(
|
|
|
|
|
Big(toNumber(value)).toFixed(toNumber(wx.getStorageSync('NUMS_HOLD_POINT') || 2)),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 数量保留位数字符串类型 */
|
|
|
|
|
export const numRetentionDigitsString = (value?: number) => {
|
|
|
|
|
return Big(toNumber(value)).toFixed(toNumber(wx.getStorageSync('NUMS_HOLD_POINT') || 2));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* sleep
|
|
|
|
|
* @param callback 回调函数
|
|
|
|
|
* @param ms 毫秒, 默认300ms
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const sleep = (callback?: () => void, ms = 300): Promise<boolean> => {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
callback?.();
|
|
|
|
|
resolve(true);
|
|
|
|
|
}, ms);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const cloneLite = (data: any) => JSON.parse(JSON.stringify(data));
|
2026-02-04 11:42:19 +08:00
|
|
|
|
|
|
|
|
/** 转换文件大小 */
|
|
|
|
|
export const formatFileSize = (fileSize: any): string => {
|
|
|
|
|
const file_size = toNumber(fileSize);
|
|
|
|
|
if (file_size == 0) {
|
|
|
|
|
return '0B';
|
|
|
|
|
}
|
|
|
|
|
const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
|
|
|
let index = 0;
|
|
|
|
|
index = Math.floor(Math.log(file_size) / Math.log(1024));
|
|
|
|
|
const size = file_size / 1024 ** index;
|
|
|
|
|
return size.toFixed(0) + unitArr[index];
|
|
|
|
|
};
|
2026-02-05 16:02:34 +08:00
|
|
|
|
|
|
|
|
export const getDay = (option?: { value: number; unit: 'day' | 'month' | 'year' }) => {
|
|
|
|
|
const { value = 0, unit = 'day' } = option || {};
|
|
|
|
|
return dayjs().subtract(value, unit).format('YYYY-MM-DD');
|
|
|
|
|
};
|