164 lines
4.4 KiB
JavaScript
164 lines
4.4 KiB
JavaScript
/**
|
||
* 参数处理
|
||
* @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
|
||
}
|
||
|
||
/**
|
||
* 参数处理,避免小程序跳转10层后无法跳转
|
||
* @param url
|
||
*/
|
||
export function navigateBackTo (url) {
|
||
const backUrl = url.slice(1);
|
||
const pages = getCurrentPages();
|
||
const num = pages.length; // 当前页面栈总数
|
||
const index = pages.findIndex(item => item.route === backUrl);
|
||
const backnum = num - index - 1;
|
||
uni.navigateBack({ delta: backnum });
|
||
}
|
||
|
||
/**
|
||
* 深复制
|
||
* @param {any} obj 需要复制的对象
|
||
*/
|
||
export function deepClone (obj) {
|
||
if (typeof obj == 'object') {
|
||
const objClone = Array.isArray(obj) ? [] : {}
|
||
for (const key in obj) {
|
||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||
if (obj[key] && typeof obj[key] == 'object') {
|
||
objClone[key] = deepClone(obj[key])
|
||
} else {
|
||
objClone[key] = obj[key]
|
||
}
|
||
}
|
||
}
|
||
return objClone
|
||
}
|
||
return obj
|
||
}
|
||
|
||
/**
|
||
* @desc 时间戳转日期毫秒
|
||
* @param {Date} format 时间格式 Y:年,M:月,D:天 h:小时,m:分钟,s:秒 默认:'Y/M/D h:m'
|
||
*/
|
||
export function formatDate (arg, format) {
|
||
let now
|
||
if (!arg) {
|
||
return null
|
||
} else {
|
||
now = new Date(arg)
|
||
}
|
||
format = format || 'Y.M.D h:m'
|
||
|
||
const year = now.getFullYear()
|
||
let month = now.getMonth() + 1
|
||
month = month >= 10 ? month : '0' + month
|
||
let date = now.getDate()
|
||
date = date >= 10 ? date : '0' + date
|
||
let hour = now.getHours()
|
||
hour = hour >= 10 ? hour : '0' + hour
|
||
let minute = now.getMinutes()
|
||
minute = minute >= 10 ? minute : '0' + minute
|
||
let second = now.getSeconds()
|
||
second = second >= 10 ? second : '0' + second
|
||
return format
|
||
.replace('Y', year)
|
||
.replace('M', month)
|
||
.replace('D', date)
|
||
.replace('h', hour)
|
||
.replace('m', minute)
|
||
.replace('s', second)
|
||
}
|
||
|
||
/**
|
||
* 复制文本(uni-app 剪贴板)
|
||
* @param {string} copytext
|
||
*/
|
||
export function copyText (copytext) {
|
||
uni.setClipboardData({
|
||
data: copytext,
|
||
success: () => {},
|
||
})
|
||
return { type: 'success', message: '复制成功' }
|
||
}
|
||
|
||
/**
|
||
* hex转10进制
|
||
* @param hexStr
|
||
*/
|
||
export function hex2int (hexStr) {
|
||
let twoStr = parseInt(hexStr, 16).toString(2) // 将十六转十进制,再转2进制
|
||
let bitNum = hexStr.length * 4 // 1个字节 = 8bit ,0xff 一个 "f"就是4位
|
||
if (twoStr.length < bitNum) {
|
||
while (twoStr.length < bitNum) {
|
||
twoStr = "0" + twoStr
|
||
}
|
||
}
|
||
if (twoStr.substring(0, 1) == "0") {
|
||
// 正数
|
||
const two = parseInt(twoStr, 2) // 二进制转十进制
|
||
return two
|
||
} else {
|
||
// 负数
|
||
let twoStr_unsign = ""
|
||
let two = parseInt(twoStr, 2) - 1 // 补码:(负数)反码+1,符号位不变;相对十进制来说也是 +1,但这里是负数,+1就是绝对值数据-1
|
||
twoStr = two.toString(2)
|
||
twoStr_unsign = twoStr.substring(1, bitNum) // 舍弃首位(符号位)
|
||
// 去除首字符,将0转为1,将1转为0 反码
|
||
twoStr_unsign = twoStr_unsign.replace(/0/g, "z")
|
||
twoStr_unsign = twoStr_unsign.replace(/1/g, "0")
|
||
twoStr_unsign = twoStr_unsign.replace(/z/g, "1")
|
||
two = -parseInt(twoStr_unsign, 2)
|
||
return two
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 10进制转16进制
|
||
* @param hexStr
|
||
*/
|
||
export function int2hex (hexStr) {
|
||
let hexInt = typeof hexStr == 'string' ? parseInt(hexStr, 10) : hexStr
|
||
let hex = ''
|
||
const LENGTH_04 = 4
|
||
const LENGTH_15 = 15
|
||
if (hexInt >= 0) {
|
||
hex = hexInt.toString(16).toLocaleLowerCase()
|
||
} else {
|
||
let twoStr = (-hexInt - 1).toString(2)
|
||
let sb = '000000000000000'
|
||
twoStr = sb.slice(0, LENGTH_15 - twoStr.length) + twoStr
|
||
// 将0转为1,将1转为0 反码
|
||
twoStr = twoStr.replace(/0/g, "z")
|
||
twoStr = twoStr.replace(/1/g, "0")
|
||
twoStr = twoStr.replace(/z/g, "1")
|
||
hex = parseInt(('1' + twoStr), 2).toString(16).toLocaleLowerCase()
|
||
}
|
||
// 长度不等于4 补全
|
||
if (hex.length != LENGTH_04) {
|
||
let sb = "0000"
|
||
hex = sb.slice(0, LENGTH_04 - hex.length) + hex
|
||
}
|
||
return hex.toUpperCase()
|
||
} |