wlzboy
6 天以前 09e6dc3fb7266620fafb5e341808a8eb36e080a1
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}
 
/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        resolve(res)
      }
    })
  })
}
 
/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&"
      }
    }
  }
  return result
}
 
/**
 * 格式化日期时间(兼容iOS)
 * @param dateTime 日期时间字符串或Date对象
 * @param format 格式化模板,默认为 'YYYY-MM-DD HH:mm:ss'
 * @returns {string} 格式化后的日期时间字符串
 */
export function formatDateTime(dateTime, format = 'YYYY-MM-DD HH:mm:ss') {
  if (!dateTime) return ''
  
  let date
  if (typeof dateTime === 'string') {
    // iOS兼容性处理:将 "yyyy-MM-dd HH:mm:ss" 格式转换为 "yyyy/MM/dd HH:mm:ss"
    const iosCompatibleDate = dateTime.replace(/-/g, '/')
    date = new Date(iosCompatibleDate)
  } else {
    date = new Date(dateTime)
  }
  
  // 检查日期是否有效
  if (isNaN(date.getTime())) {
    console.warn('Invalid date:', dateTime)
    return ''
  }
  
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  
  // 根据format模板返回格式化结果
  if (format === 'YYYY-MM-DD') {
    return `${year}-${month}-${day}`
  } else if (format === 'YYYY-MM-DD HH:mm') {
    return `${year}-${month}-${day} ${hours}:${minutes}`
  } else if (format === 'MM-DD HH:mm') {
    return `${month}-${day} ${hours}:${minutes}`
  } else {
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  }
}
 
/**
 * 获取系统信息(兼容新旧API)
 * 替代已废弃的uni.getSystemInfoSync()
 * @returns {Object} 系统信息对象
 */
export function getSystemInfo() {
  // #ifdef MP-WEIXIN
  // 微信小程序使用新API
  try {
    const windowInfo = uni.getWindowInfo()
    const deviceInfo = uni.getDeviceInfo()
    const appBaseInfo = uni.getAppBaseInfo()
    
    return {
      ...windowInfo,
      ...deviceInfo,
      ...appBaseInfo,
      // 兼容旧字段名
      windowHeight: windowInfo.windowHeight,
      windowWidth: windowInfo.windowWidth,
      screenHeight: windowInfo.screenHeight,
      screenWidth: windowInfo.screenWidth,
      statusBarHeight: windowInfo.statusBarHeight,
      platform: deviceInfo.platform,
      system: deviceInfo.system,
      model: deviceInfo.model,
      brand: deviceInfo.brand,
      SDKVersion: appBaseInfo.SDKVersion,
      version: appBaseInfo.version
    }
  } catch (e) {
    // 降级使用旧API
    console.warn('新API调用失败,降级使用旧API', e)
    return uni.getSystemInfoSync()
  }
  // #endif
  
  // #ifndef MP-WEIXIN
  // 其他平台继续使用旧API
  return uni.getSystemInfoSync()
  // #endif
}