| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- /**
- * 模态框配置选项接口
- */
- 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<boolean> - 用户点击确认返回 true,点击取消返回 false
- */
- showModal(params: IModalOptions): Promise<boolean>;
- /*
- * 显示模态对话框, 与showModal的差异在于,确认时命中resolve, 取消时命中reject.
- * // NOTE: 没有直接在showModal里改,是因为使用的地方太多,避免直接修改影响太广 2026.1.8
- * @param params - 模态框配置选项
- * @returns Promise<boolean> - 用户点击确认返回 true,点击取消返回 false
- * */
- showConfirm(params: IModalOptions): Promise<boolean>;
- /**
- * 复制内容
- * @param content - 要复制的内容
- */
- copy(content: string): void;
- /* 打开网址 */
- 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
- });
- });
- },
- showConfirm(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) => {
- /* 这里不要动,就是这样的。形成await showConfirm的效果 2026.1.13 */
- /* 如果有必须接收true/false的写法,请使用showModal */
- if(res.confirm)(resolve(true))
- else reject(false)
- },
- fail: reject
- });
- });
- },
- copy(content: string) {
- uni.setClipboardData({
- data: content,
- success: () => { }
- });
- },
- openBrowser(url: string) {
- // 判断平台
- // #ifdef H5
- window.open(url, '_blank')
- // #endif
- // #ifdef MP-WEIXIN
- // 微信小程序
- uni.setClipboardData({
- data: url,
- showToast: false,
- success: () => {
- uni.showModal({
- title: '提示',
- content: '链接已复制,请在浏览器中打开',
- showCancel: false
- })
- }
- })
- // #endif
- // #ifdef MP-ALIPAY
- // 支付宝小程序
- if (my.openBrowser) {
- my.openBrowser({ url: url })
- } else {
- // 降级处理
- uni.setClipboardData({
- data: url,
- success: () => {
- uni.showModal({
- title: '提示',
- content: '链接已复制,请在浏览器中打开',
- showCancel: false
- })
- }
- })
- }
- // #endif
- // 其他平台,如App、快应用等,可以根据需要补充
- // 对于不支持直接打开浏览器的平台,使用降级方案
- // #ifndef H5 || MP-WEIXIN || MP-ALIPAY
- uni.setClipboardData({
- data: url,
- success: () => {
- uni.showModal({
- title: '提示',
- content: '链接已复制,请在浏览器中打开',
- showCancel: false
- })
- }
- })
- // #endif
- }
- };
- export default tool;
|