h5/tool/index.js

89 lines
1.9 KiB
JavaScript

export const setNumSize = (n) => { // 0 =》 00
n = n.toString()
return n[1] ? n : '0' + n
}
export const formatTime = (time, types) => { // 设置时间格式
const date = new Date(time)
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(setNumSize).join(types || '-') + ' ' + [hour, minute, second].map(setNumSize)
.join(':')
}
export const toast = (text = '') => {
uni.showToast({
title: String(text),
icon: 'none',
duration: 2000
})
}
export const showLoading = (text = '') => {
uni.showLoading({
title: String(text),
mask: true
})
}
export const hideLoading = () => {
setTimeout(() => {
uni.hideLoading()
}, 200)
}
export const goPage = (l, o, s, f, c, t, e) => { // 跳转页面
let obj = {}
let isArr = (typeof l == 'object' && l.length > 1)
let url = ['/pages']
if (isArr) {
l.forEach((item, index) => {
url.push(`/${item}`)
})
url.push(`/${l[l.length-1]}`)
} else {
url.push(`/${l}/${l}`)
}
url = url.join('')
if (o) {
let i, key = new Array();
for (i in o) {
let item = o[i];
key.push(i + '=' + (typeof item == 'object' ? JSON.stringify(item) : item));
}
url += ('?' + key.join("&"));
}
obj.url = url;
s && (obj.success = s);
f && (obj.fail = f);
c && (obj.complete = c);
e && (obj.events = e);
uni[t ? 'reLaunch' : 'navigateTo'](obj)
}
export const alert = (c, o) => {
let obj = {
content: String(c),
showCancel: false
}
if (o)
for (let i in o) obj[i] = o[i]
uni.showModal(obj)
}
export const setUrlObj = (str) => { // url body 转obj
const arr = str.split('&')
let obj = {}
arr.map(item => {
let data = item.split('=')
obj[data[0]] = data[1]
})
return obj
}
export const setObjUrl = (obj) => { // obj转url
let str = []
for (let i in obj) {
str.push(i + '=' + obj[i])
}
return str.join('&')
}