Files
intc-vue-h5/src/utils/common.ts
2024-06-06 18:42:02 +08:00

84 lines
2.2 KiB
TypeScript

/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content:string) {
uni.showToast({
icon: 'none',
title: content
})
}
/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content:string):Promise<any> {
return new Promise((resolve, reject) => {
uni.showModal({
title: '提示',
content: content,
cancelText: '取消',
confirmText: '确定',
success: function(res) {
resolve(res)
}
})
})
}
/**
* 参数处理
* @param params 参数
*/
export function tansParams(params:any) {
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
}
// 时间返回 yyyy-mm-dd hh:mm:ss
export function timeHandler(d, unit1, unit2) {
const year = d.getFullYear()
const month = addZero(d.getMonth() + 1)
const day = addZero(d.getDate())
const hours = addZero(d.getHours())
const minute = addZero(d.getMinutes())
const second = addZero(d.getSeconds())
const time = year + unit1 + month + unit1 + day + ' ' + hours + unit2 + minute + unit2 + second
return time
}
// 时间返回 mm-dd hh:mm
export function timeHandler2(d, unit1, unit2) {
const year = d.getFullYear()
const month = addZero(d.getMonth() + 1)
const day = addZero(d.getDate())
const hours = addZero(d.getHours())
const minute = addZero(d.getMinutes())
const second = addZero(d.getSeconds())
const time = month + unit1 + day + ' ' + hours + unit2 + minute
return time
}
export function addZero(num) {
if (num < 10) {
return '0' + num
} else {
return num
}
}