183 lines
3.7 KiB
JavaScript
183 lines
3.7 KiB
JavaScript
|
|
/**
|
||
|
|
* Soul创业实验 - 工具函数
|
||
|
|
*/
|
||
|
|
|
||
|
|
// 格式化时间
|
||
|
|
const formatTime = date => {
|
||
|
|
const year = date.getFullYear()
|
||
|
|
const month = date.getMonth() + 1
|
||
|
|
const day = date.getDate()
|
||
|
|
const hour = date.getHours()
|
||
|
|
const minute = date.getMinutes()
|
||
|
|
const second = date.getSeconds()
|
||
|
|
|
||
|
|
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
|
||
|
|
}
|
||
|
|
|
||
|
|
const formatNumber = n => {
|
||
|
|
n = n.toString()
|
||
|
|
return n[1] ? n : `0${n}`
|
||
|
|
}
|
||
|
|
|
||
|
|
// 格式化日期
|
||
|
|
const formatDate = date => {
|
||
|
|
const year = date.getFullYear()
|
||
|
|
const month = date.getMonth() + 1
|
||
|
|
const day = date.getDate()
|
||
|
|
return `${year}-${formatNumber(month)}-${formatNumber(day)}`
|
||
|
|
}
|
||
|
|
|
||
|
|
// 格式化金额
|
||
|
|
const formatMoney = (amount, decimals = 2) => {
|
||
|
|
return Number(amount).toFixed(decimals)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 防抖函数
|
||
|
|
const debounce = (fn, delay = 300) => {
|
||
|
|
let timer = null
|
||
|
|
return function (...args) {
|
||
|
|
if (timer) clearTimeout(timer)
|
||
|
|
timer = setTimeout(() => {
|
||
|
|
fn.apply(this, args)
|
||
|
|
}, delay)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 节流函数
|
||
|
|
const throttle = (fn, delay = 300) => {
|
||
|
|
let last = 0
|
||
|
|
return function (...args) {
|
||
|
|
const now = Date.now()
|
||
|
|
if (now - last >= delay) {
|
||
|
|
fn.apply(this, args)
|
||
|
|
last = now
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 生成唯一ID
|
||
|
|
const generateId = () => {
|
||
|
|
return 'id_' + Date.now().toString(36) + Math.random().toString(36).substr(2)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查手机号格式
|
||
|
|
const isValidPhone = phone => {
|
||
|
|
return /^1[3-9]\d{9}$/.test(phone)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查微信号格式
|
||
|
|
const isValidWechat = wechat => {
|
||
|
|
return wechat && wechat.length >= 6 && wechat.length <= 20
|
||
|
|
}
|
||
|
|
|
||
|
|
// 深拷贝
|
||
|
|
const deepClone = obj => {
|
||
|
|
if (obj === null || typeof obj !== 'object') return obj
|
||
|
|
if (obj instanceof Date) return new Date(obj)
|
||
|
|
if (obj instanceof Array) return obj.map(item => deepClone(item))
|
||
|
|
if (obj instanceof Object) {
|
||
|
|
const copy = {}
|
||
|
|
Object.keys(obj).forEach(key => {
|
||
|
|
copy[key] = deepClone(obj[key])
|
||
|
|
})
|
||
|
|
return copy
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取URL参数
|
||
|
|
const getQueryParams = url => {
|
||
|
|
const params = {}
|
||
|
|
const queryString = url.split('?')[1]
|
||
|
|
if (queryString) {
|
||
|
|
queryString.split('&').forEach(pair => {
|
||
|
|
const [key, value] = pair.split('=')
|
||
|
|
params[decodeURIComponent(key)] = decodeURIComponent(value || '')
|
||
|
|
})
|
||
|
|
}
|
||
|
|
return params
|
||
|
|
}
|
||
|
|
|
||
|
|
// 存储操作
|
||
|
|
const storage = {
|
||
|
|
get(key) {
|
||
|
|
try {
|
||
|
|
return wx.getStorageSync(key)
|
||
|
|
} catch (e) {
|
||
|
|
console.error('获取存储失败:', e)
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
},
|
||
|
|
set(key, value) {
|
||
|
|
try {
|
||
|
|
wx.setStorageSync(key, value)
|
||
|
|
return true
|
||
|
|
} catch (e) {
|
||
|
|
console.error('设置存储失败:', e)
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
remove(key) {
|
||
|
|
try {
|
||
|
|
wx.removeStorageSync(key)
|
||
|
|
return true
|
||
|
|
} catch (e) {
|
||
|
|
console.error('删除存储失败:', e)
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
clear() {
|
||
|
|
try {
|
||
|
|
wx.clearStorageSync()
|
||
|
|
return true
|
||
|
|
} catch (e) {
|
||
|
|
console.error('清除存储失败:', e)
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 显示Toast
|
||
|
|
const showToast = (title, icon = 'none', duration = 2000) => {
|
||
|
|
wx.showToast({ title, icon, duration })
|
||
|
|
}
|
||
|
|
|
||
|
|
// 显示Loading
|
||
|
|
const showLoading = (title = '加载中...') => {
|
||
|
|
wx.showLoading({ title, mask: true })
|
||
|
|
}
|
||
|
|
|
||
|
|
// 隐藏Loading
|
||
|
|
const hideLoading = () => {
|
||
|
|
wx.hideLoading()
|
||
|
|
}
|
||
|
|
|
||
|
|
// 显示确认框
|
||
|
|
const showConfirm = (title, content) => {
|
||
|
|
return new Promise((resolve) => {
|
||
|
|
wx.showModal({
|
||
|
|
title,
|
||
|
|
content,
|
||
|
|
success: res => resolve(res.confirm)
|
||
|
|
})
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
formatTime,
|
||
|
|
formatDate,
|
||
|
|
formatMoney,
|
||
|
|
formatNumber,
|
||
|
|
debounce,
|
||
|
|
throttle,
|
||
|
|
generateId,
|
||
|
|
isValidPhone,
|
||
|
|
isValidWechat,
|
||
|
|
deepClone,
|
||
|
|
getQueryParams,
|
||
|
|
storage,
|
||
|
|
showToast,
|
||
|
|
showLoading,
|
||
|
|
hideLoading,
|
||
|
|
showConfirm
|
||
|
|
}
|