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.
267 lines
5.4 KiB
267 lines
5.4 KiB
export function isDef(value) {
|
|
return value !== undefined && value !== null && value !== '';
|
|
}
|
|
|
|
export function isUndef(value) {
|
|
return value === undefined || value === null || value === '';
|
|
}
|
|
|
|
export function isDate(value) {
|
|
return /^\d{4}-\d{2}-\d{2}$/.test(value);
|
|
}
|
|
|
|
export function isTime(value) {
|
|
return /^\d{2}:\d{2}:\d{2}$/.test(value);
|
|
}
|
|
|
|
export function isObject(value) {
|
|
return value && Object.prototype.toString.call(value) === '[object Object]';
|
|
}
|
|
|
|
export function isDatetime(value) {
|
|
return /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(value);
|
|
}
|
|
|
|
export function formatParams(value) {
|
|
if (typeof value !== 'object') return;
|
|
const options = Object.entries(value).map(([key, value]) => `${key}=${value}`);
|
|
return options.join('&');
|
|
}
|
|
|
|
export function formatDatetime(format, time) {
|
|
let now = null;
|
|
if (!time) {
|
|
now = new Date();
|
|
} else {
|
|
now = new Date(time);
|
|
}
|
|
const year = now.getFullYear();
|
|
const month = now.getMonth() + 1;
|
|
const dates = now.getDate();
|
|
const hours = now.getHours();
|
|
const minute = now.getMinutes();
|
|
const second = now.getSeconds();
|
|
if (isUndef(format)) return `${year}-${month}-${dates} ${hours}:${minute}:${second}`;
|
|
return format.replace(/(yyyy|MM|dd|HH|mm|ss)/gi, val => {
|
|
switch (val) {
|
|
case 'yyyy':
|
|
return year;
|
|
case 'MM':
|
|
return month < 10 ? `0${month}` : month;
|
|
case 'dd':
|
|
return dates < 10 ? `0${dates}` : dates;
|
|
case 'HH':
|
|
return hours < 10 ? `0${hours}` : hours;
|
|
case 'mm':
|
|
return minute < 10 ? `0${minute}` : minute;
|
|
case 'ss':
|
|
return second < 10 ? `0${second}` : second;
|
|
default:
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
|
|
export function formatHourMinute(value) {
|
|
if (isTime(value)) return value.slice(5);
|
|
if (isDatetime(value)) return value.slice(11, 16);
|
|
return '00:00';
|
|
}
|
|
// 清楚掉签名的相关缓存
|
|
export function clearPDF() {
|
|
uni.removeStorageSync('PDF')
|
|
uni.removeStorageSync('userImgPath')
|
|
uni.removeStorageSync('xunImgPath')
|
|
uni.removeStorageSync('projectList')
|
|
}
|
|
|
|
// 本地数据缓存
|
|
export const storage = {
|
|
/**
|
|
* @param {String} key 键值
|
|
* @param {Object} data 数据
|
|
*/
|
|
|
|
set(key, data) {
|
|
try {
|
|
uni.setStorageSync(key, data);
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* @param {String} key 键值
|
|
*/
|
|
get(key) {
|
|
try {
|
|
return uni.getStorageSync(key);
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
},
|
|
|
|
clear() {
|
|
try {
|
|
uni.clearStorageSync();
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* @param {String} key 键值
|
|
*/
|
|
remove(key) {
|
|
try {
|
|
uni.removeStorageSync(key);
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
},
|
|
raunchToLogin() {
|
|
const Authorization = uni.getStorageSync("Authorization")
|
|
// #ifdef APP-PLUS
|
|
if (isDef(Authorization)) return plus.navigator.closeSplashscreen();
|
|
uni.reLaunch({
|
|
url: '/pages/user/login/login',
|
|
success: () => setTimeout(() => plus.navigator.closeSplashscreen(), 200),
|
|
});
|
|
// #endif
|
|
if (isDef(Authorization)) return
|
|
uni.reLaunch({
|
|
url: '/pages/user/login/login',
|
|
});
|
|
},
|
|
};
|
|
|
|
/**
|
|
* 拼接头部
|
|
* @param {Object} header 自定义头部
|
|
* @param {Number} type 内容类型
|
|
*/
|
|
export function calcHeader(header, type) {
|
|
const temp = {};
|
|
const ContentType = ['application/json;charset=utf-8', 'application/x-www-form-urlencoded','multipart/form-data'];
|
|
|
|
temp['Content-Type'] = ContentType[type];
|
|
temp['token'] = storage.get('Authorization') || '';
|
|
Object.keys(header).map(item => (temp[item] = header[item]));
|
|
return {
|
|
...temp
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @function Toast 吐司
|
|
* @param {String} title
|
|
* @param {Boolean} mask
|
|
* @param {String} icon
|
|
* @param {Number} dr
|
|
*/
|
|
export function showToast(title, icon = 'none', mask = false, time = 2000) {
|
|
uni.showToast({
|
|
title,
|
|
mask,
|
|
icon,
|
|
duration: time,
|
|
});
|
|
}
|
|
|
|
export function showModel(content = '这是一个模态弹窗', title = '提示') {
|
|
return new Promise((resolve, reject) => {
|
|
uni.showModal({
|
|
title: title,
|
|
content: content,
|
|
success: function(res) {
|
|
if (res.confirm) {
|
|
return resolve('success');
|
|
} else {
|
|
return reject('取消操作');
|
|
}
|
|
},
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 返回值固定格式
|
|
* @param {*} msg 错误信息
|
|
* @param {*} params 请求数据
|
|
*/
|
|
export function formatReturn(msg, data) {
|
|
let err = typeof msg === 'string' && msg.length > 0 ? {
|
|
data: {},
|
|
msg
|
|
} : null;
|
|
let res = data || null;
|
|
return [err, {
|
|
...res
|
|
}];
|
|
}
|
|
|
|
export function getOffset() {
|
|
let size = {};
|
|
|
|
try {
|
|
const system = uni.getSystemInfoSync();
|
|
const windowWidth = system.windowWidth;
|
|
const windowHeight = system.windowHeight;
|
|
size = {
|
|
windowWidth,
|
|
windowHeight
|
|
};
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
return size;
|
|
}
|
|
|
|
/**
|
|
* 获取屏幕尺寸
|
|
*/
|
|
export const obtainSize = {
|
|
width() {
|
|
let value = 300;
|
|
try {
|
|
const res = uni.getSystemInfoSync();
|
|
value = res.windowWidth;
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
return value;
|
|
},
|
|
height() {
|
|
let value = 300;
|
|
try {
|
|
const res = uni.getSystemInfoSync();
|
|
value = res.windowHeight;
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
return value;
|
|
},
|
|
};
|
|
|
|
export function loadScript(file) {
|
|
return new Promise((resolve, reject) => {
|
|
const script = document.createElement('script');
|
|
script.src = file;
|
|
script.onload = () => resolve(1);
|
|
document.head.appendChild(script);
|
|
});
|
|
}
|
|
|
|
export async function loadScriptList(files) {
|
|
if (Array.isArray(files)) {
|
|
for (let file of files) {
|
|
await loadScript(file);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function goPage(url) {
|
|
uni.navigateTo({
|
|
url
|
|
})
|
|
}
|