gaoluyang
2025-12-29 a175709baa7777575d054f928d4d2d029a04bd60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * uview-plus 工具函数封装
 * 提供统一的提示和模态框工具
 */
 
/**
 * 使用 Toast 提示工具
 * @returns {{showToast: function}} 
 */
export const useToast = () => {
  /**
   * 显示提示信息
   * @param {string} message 提示消息
   * @param {string} [type='info'] 提示类型:'success' | 'error' | 'warning' | 'info'
   * @param {number} [duration=2000] 显示时长(毫秒)
   */
  const showToast = (message, type = 'info', duration = 2000) => {
    // 映射 uview-plus 的图标类型
    const iconMap = {
      success: 'success',
      error: 'error', 
      warning: 'warning',
      info: 'info'
    };
    
    uni.showToast({
      title: message,
      icon: iconMap[type] || 'none',
      duration: duration
    });
  };
 
  return {
    showToast
  };
};
 
/**
 * 使用 Modal 模态框工具
 * @returns {{showModal: function}}
 */
export const useModal = () => {
  /**
   * 显示确认模态框
   * @param {Object} options 配置选项
   * @param {string} options.title 标题
   * @param {string} options.content 内容
   * @param {boolean} [options.showCancel=true] 是否显示取消按钮
   * @param {string} [options.confirmText='确认'] 确认按钮文本
   * @param {string} [options.cancelText='取消'] 取消按钮文本
   * @returns {Promise<boolean>} 用户确认结果
   */
  const showModal = (options) => {
    return new Promise((resolve) => {
      uni.showModal({
        title: options.title || '提示',
        content: options.content || '',
        showCancel: options.showCancel !== false,
        confirmText: options.confirmText || '确认',
        cancelText: options.cancelText || '取消',
        success: (res) => {
          resolve(res.confirm);
        },
        fail: () => {
          resolve(false);
        }
      });
    });
  };
 
  return {
    showModal
  };
};
 
/**
 * 默认导出
 */
export default {
  useToast,
  useModal
};