/** * 模态框配置选项接口 */ export interface IModalOptions { /** 标题 */ title?: string; /** 内容 */ content: string; /** 是否显示取消按钮 */ showCancel?: boolean; /** 是否显示确认按钮 */ showConfirm?: boolean; /** 确认按钮文字 */ confirmText?: string; /** 取消按钮文字 */ cancelText?: string; /** 取消按钮颜色 */ cancelColor?: string; /** 确认按钮颜色 */ confirmColor?: string; } /** * 工具函数接口 */ export interface IeTool { /** 是否显示加载提示 */ loading: boolean, /** 加载提示开始时间 */ loadingStartTime: number, /** 最小显示时间 */ minLoadingTime: number, /** * 显示普通提示 * @param title - 提示内容,默认为空字符串 */ showToast(title?: string): void; /** * 显示成功提示 * @param title - 提示内容,默认为空字符串 */ showSuccess(title?: string): void; /** * 显示错误提示 * @param title - 提示内容,默认为空字符串 */ showError(title?: string): void; /** * 显示加载提示 * @param title - 提示内容,默认为空字符串 */ showLoading(title?: string): void; /** * 隐藏加载提示 */ hideLoading(): void; /** * 显示模态对话框 * @param params - 模态框配置选项 * @returns Promise - 用户点击确认返回 true,点击取消返回 false */ showModal(params: IModalOptions): Promise; /** * 复制内容 * @param content - 要复制的内容 */ copy(content: string): void; /** * 格式化价格 * @param price - 价格 * @returns 格式化后的价格 */ formatPrice(price: number): number; /** * 格式化时间 * @param time - 时间 * @returns 格式化后的时间 */ formatTime(time: number | string, format: string): string; openBrowser(url: string): void; } const defaultModalOptions: IModalOptions = { title: '', content: '', showCancel: true, showConfirm: true, cancelColor: '#000000', confirmColor: '#31a0fc', cancelText: '取消', confirmText: '确认' }; const tool: IeTool = { loading: false, loadingStartTime: 0, minLoadingTime: 500, // 最小显示时间,单位毫秒 showToast(title: string = '') { // 先立即隐藏,避免上一个 toast存在导致下次的 toast 很快关闭 uni.hideToast(); setTimeout(() => { uni.showToast({ title, icon: 'none' }); }, 50); }, showSuccess(title: string = '') { uni.showToast({ title, icon: 'success' }); }, showError(title: string = '') { uni.showToast({ title, icon: 'error' }); }, showLoading(title: string = '') { this.loadingStartTime = Date.now(); uni.showLoading({ title, mask: true, success: () => { } }); }, hideLoading() { const currentTime = Date.now(); const elapsedTime = currentTime - this.loadingStartTime; const remainingTime = Math.max(0, this.minLoadingTime - elapsedTime); if (remainingTime > 0) { setTimeout(() => { uni.hideLoading(); }, remainingTime); } else { uni.hideLoading(); } }, showModal(params: IModalOptions) { const { title, content, showCancel, confirmText, cancelText, cancelColor, confirmColor } = Object.assign(defaultModalOptions, params); return new Promise((resolve, reject) => { uni.showModal({ title, content, showCancel, confirmText, cancelText, cancelColor, confirmColor, success: (res) => { resolve(res.confirm); }, fail: reject }); }); }, copy(content: string) { uni.setClipboardData({ data: content, success: () => { } }); }, formatPrice(price: number) { return price / 100; }, formatTime(time: number | string, format: string = 'yyyy-mm-dd hh:MM:ss') { if (!time) { return ''; } // @ts-ignore return uni.$uv.timeFormat(time, format) }, openBrowser(url: string) { const handleCopy = function () { uni.setClipboardData({ data: url, success: () => { uni.showModal({ title: '提示', content: '链接已复制,请在浏览器中打开', showCancel: false }) } }) } // 判断平台 // #ifdef APP-PLUS plus.runtime.openURL(url) // #endif // #ifdef H5 window.open(url, '_blank') // #endif // #ifdef MP-WEIXIN // 微信小程序 try { if (url.toLowerCase().startsWith('https://mp.weixin.qq.com/')) { uni.openOfficialAccountArticle({ url, fail: handleCopy }) } else { handleCopy() } } catch (e){ console.log('小程序打开链接降级处理', e) handleCopy() } // #endif // 其他平台,如App、快应用等,可以根据需要补充 // 对于不支持直接打开浏览器的平台,使用降级方案 // #ifndef H5 || MP-WEIXIN handleCopy() // #endif } }; export default tool;