You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
3.3 KiB
155 lines
3.3 KiB
/**
|
|
* 工具函数索引文件
|
|
* 统一管理项目中的可复用工具函数
|
|
*/
|
|
|
|
import util from './util.js'
|
|
|
|
// 导出所有工具函数
|
|
export const {
|
|
// 登录退出相关
|
|
loginOut,
|
|
openHomeView,
|
|
|
|
// 时间格式化
|
|
formatTime,
|
|
formatDateTime,
|
|
formatToDateTime,
|
|
dateUtils,
|
|
getCurrentDate,
|
|
|
|
// 通用格式化
|
|
formatLocation,
|
|
formatFileSize,
|
|
formatAge,
|
|
formatGender,
|
|
|
|
// 对象处理
|
|
safeGet,
|
|
|
|
// 样式相关
|
|
hexToRgba,
|
|
img,
|
|
getResourceUrl,
|
|
|
|
// 数据获取
|
|
getDict,
|
|
|
|
// 文件上传
|
|
uploadFile,
|
|
|
|
// 通信相关
|
|
makePhoneCall,
|
|
|
|
// 页面跳转
|
|
navigateToPage
|
|
} = util
|
|
|
|
// 工具函数使用说明
|
|
export const UtilsUsage = {
|
|
// 对象安全访问
|
|
safeGet: {
|
|
description: '安全访问对象属性,避免undefined错误',
|
|
params: {
|
|
obj: 'Object - 目标对象',
|
|
path: 'String - 属性路径,如 "a.b.c"',
|
|
defaultValue: 'Any - 默认值'
|
|
},
|
|
returns: 'Any - 属性值或默认值',
|
|
example: `
|
|
const name = safeGet(clientInfo, 'customerResource.name', '未知客户')
|
|
`
|
|
},
|
|
|
|
// 文件大小格式化
|
|
formatFileSize: {
|
|
description: '格式化文件大小为可读格式',
|
|
params: {
|
|
bytes: 'Number - 字节数'
|
|
},
|
|
returns: 'String - 格式化后的大小',
|
|
example: `
|
|
formatFileSize(1024000) // "1 MB"
|
|
`
|
|
},
|
|
|
|
// 年龄格式化
|
|
formatAge: {
|
|
description: '格式化年龄显示',
|
|
params: {
|
|
age: 'Number - 年龄(小数格式,如9.05表示9岁5个月)'
|
|
},
|
|
returns: 'String - 格式化后的年龄',
|
|
example: `
|
|
formatAge(9.05) // "9岁5个月"
|
|
`
|
|
},
|
|
|
|
// 性别格式化
|
|
formatGender: {
|
|
description: '格式化性别显示',
|
|
params: {
|
|
gender: 'Number - 性别值(1男,2女)'
|
|
},
|
|
returns: 'String - 性别字符串',
|
|
example: `
|
|
formatGender(1) // "男"
|
|
`
|
|
},
|
|
|
|
// 拨打电话
|
|
makePhoneCall: {
|
|
description: '拨打电话的通用方法',
|
|
params: {
|
|
phoneNumber: 'String - 电话号码',
|
|
successCallback: 'Function - 成功回调(可选)',
|
|
failCallback: 'Function - 失败回调(可选)'
|
|
},
|
|
example: `
|
|
makePhoneCall('13800138000',
|
|
() => console.log('拨打成功'),
|
|
(err) => console.error('拨打失败', err)
|
|
)
|
|
`
|
|
},
|
|
|
|
// 页面跳转
|
|
navigateToPage: {
|
|
description: '页面跳转的通用方法',
|
|
params: {
|
|
url: 'String - 跳转路径',
|
|
params: 'Object - 跳转参数对象(可选)'
|
|
},
|
|
example: `
|
|
navigateToPage('/pages/detail/index', {
|
|
id: 123,
|
|
name: '测试'
|
|
})
|
|
// 结果: /pages/detail/index?id=123&name=测试
|
|
`
|
|
},
|
|
|
|
// 获取当前日期
|
|
getCurrentDate: {
|
|
description: '获取当前日期 YYYY-MM-DD 格式',
|
|
returns: 'String - 当前日期字符串',
|
|
example: `
|
|
getCurrentDate() // "2024-01-15"
|
|
`
|
|
},
|
|
|
|
// 时间格式化
|
|
formatToDateTime: {
|
|
description: '时间格式转换',
|
|
params: {
|
|
dateTime: 'String - 时间字符串,如 "2024-05-01 01:10:21"',
|
|
fmt: 'String - 格式模板,默认 "Y-m-d H:i:s"'
|
|
},
|
|
returns: 'String - 格式化后的时间',
|
|
example: `
|
|
formatToDateTime('2024-05-01 01:10:21', 'Y-m-d H:i') // "2024-05-01 01:10"
|
|
`
|
|
}
|
|
}
|
|
|
|
export default util
|