354 lines
10 KiB
TypeScript
354 lines
10 KiB
TypeScript
|
import * as test from './test.ts'
|
||
|
import { round } from './digit.js'
|
||
|
// import { AUTH_LOGIN } from '@/tools/auth';
|
||
|
// 检测是否登录
|
||
|
function hasLogin(callback : Function, module : any) {
|
||
|
let userInfo = uni.getStorageSync('userInfo') || '';
|
||
|
if (userInfo !== '') {
|
||
|
if (Object.prototype.toString.apply(callback) === '[object Function]') {
|
||
|
callback(userInfo);
|
||
|
} else {
|
||
|
jumpTo(callback)
|
||
|
}
|
||
|
} else {
|
||
|
if (module) {
|
||
|
if (Object.prototype.toString.apply(callback) === '[object Function]') module.initComponent(callback);
|
||
|
else module.initComponent();
|
||
|
} else {
|
||
|
tips("请先登录", () => {
|
||
|
jumpTo('/pages/client/account/login')
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
function loading(msg : string) {
|
||
|
// #ifndef APP-PLUS
|
||
|
uni.showLoading({
|
||
|
mask: true,
|
||
|
title: msg || ''
|
||
|
})
|
||
|
// #endif
|
||
|
// #ifdef APP-PLUS
|
||
|
plus.nativeUI.showWaiting(msg || 'Loding..', {
|
||
|
// background: 'rgba(255,255,255,0)',
|
||
|
background: '#fff',
|
||
|
color: '#222',
|
||
|
style: 'black',
|
||
|
loading: {
|
||
|
display: 'block',
|
||
|
height: '18',
|
||
|
icon: '/static/LOGO.png'
|
||
|
}
|
||
|
});
|
||
|
// #endif
|
||
|
}
|
||
|
function close() {
|
||
|
// #ifndef APP-PLUS
|
||
|
uni.hideLoading();
|
||
|
// #endif
|
||
|
// #ifdef APP-PLUS
|
||
|
plus.nativeUI.closeWaiting();
|
||
|
// #endif
|
||
|
}
|
||
|
function tips(msg : string, Fn : Function, time = 1550) {
|
||
|
console.log(time);
|
||
|
// #ifdef APP-PLUS
|
||
|
plus.nativeUI.toast(msg);
|
||
|
// #endif
|
||
|
// #ifndef APP-PLUS
|
||
|
uni.showToast({
|
||
|
title: msg,
|
||
|
icon: 'none',
|
||
|
mask: true,
|
||
|
duration: time
|
||
|
});
|
||
|
// #endif
|
||
|
if (Object.prototype.toString.apply(Fn) === '[object Function]') {
|
||
|
setTimeout(() => {
|
||
|
Fn();
|
||
|
}, time);
|
||
|
}
|
||
|
}
|
||
|
function jumpTo(url : string, query = {}, type : number) {
|
||
|
let userInfo = uni.getStorageSync('userInfo') || '';
|
||
|
//AUTH_LOGIN.indexOf(url) > -1 && !userInfo
|
||
|
if (false) { //过滤登录权限
|
||
|
console.log("需要登录权限 但是没有", toolsMethods);
|
||
|
uni.$u.debounce(() => {
|
||
|
tips('暂未登录,请先登录', () => {
|
||
|
uni.reLaunch({ url: '/pages/client/account/login' })
|
||
|
})
|
||
|
}, 1050)
|
||
|
} else { //正常访问
|
||
|
if (!url || url.toString().indexOf('/') === -1) {
|
||
|
let pages = getCurrentPages();
|
||
|
if (pages.length === 1) {
|
||
|
jumpTo('/pages/main/home')
|
||
|
} else {
|
||
|
uni.navigateBack({
|
||
|
delta: Number(url) > 0 ? Number(url) : 1
|
||
|
});
|
||
|
}
|
||
|
} else {
|
||
|
// let tabBar = ['/pages/tabbar/home', '/pages/tabbar/shop', '/pages/tabbar/checkTest', '/pages/tabbar/share', '/pages/tabbar/mine'];
|
||
|
// let iftabBar = tabBar.filter(item => item === url);
|
||
|
let jumpType = type || 0;
|
||
|
let quertStr = '?';
|
||
|
if (query) {
|
||
|
let keys = Object.keys(query);
|
||
|
let values = Object.values(query);
|
||
|
keys.forEach((item, index) => {
|
||
|
if (index > 0) {
|
||
|
quertStr += '&' + item + '=' + values[index];
|
||
|
} else {
|
||
|
quertStr += item + '=' + values[index];
|
||
|
}
|
||
|
})
|
||
|
debugger
|
||
|
}
|
||
|
if (url.indexOf('/pages/main/') > -1) {
|
||
|
uni.switchTab({
|
||
|
url: query ? url + '' + quertStr : url,
|
||
|
})
|
||
|
} else {
|
||
|
//关闭当前页面
|
||
|
if (jumpType === 1) {
|
||
|
uni.redirectTo({
|
||
|
url: query ? url + '' + quertStr : url,
|
||
|
})
|
||
|
} else if (jumpType === 2) {
|
||
|
//关闭所有页面
|
||
|
uni.reLaunch({
|
||
|
url: query ? url + '' + quertStr : url,
|
||
|
})
|
||
|
} else {
|
||
|
uni.navigateTo({
|
||
|
url: query ? url + '' + quertStr : url,
|
||
|
animationType: "slide-in-right",
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
function deepCopy(obj : EmptyObjectType, cache = new WeakMap()) {
|
||
|
if (obj === null || typeof obj !== 'object') return obj;
|
||
|
if (cache.has(obj)) return cache.get(obj);
|
||
|
let clone;
|
||
|
if (obj instanceof Date) {
|
||
|
clone = new Date(obj.getTime());
|
||
|
} else if (obj instanceof RegExp) {
|
||
|
clone = new RegExp(obj);
|
||
|
} else if (obj instanceof Map) {
|
||
|
clone = new Map(Array.from(obj, ([key, value]) => [key, deepCopy(value, cache)]));
|
||
|
} else if (obj instanceof Set) {
|
||
|
clone = new Set(Array.from(obj, value => deepCopy(value, cache)));
|
||
|
} else if (Array.isArray(obj)) {
|
||
|
clone = obj.map(value => deepCopy(value, cache));
|
||
|
} else if (Object.prototype.toString.call(obj) === '[object Object]') {
|
||
|
clone = Object.create(Object.getPrototypeOf(obj));
|
||
|
cache.set(obj, clone);
|
||
|
for (const [key, value] of Object.entries(obj)) {
|
||
|
clone[key] = deepCopy(value, cache);
|
||
|
}
|
||
|
} else {
|
||
|
clone = Object.assign({}, obj);
|
||
|
}
|
||
|
cache.set(obj, clone);
|
||
|
return clone;
|
||
|
}
|
||
|
function priceFormat(number, decimals = 2, decimalPoint = '.', thousandsSeparator = ',') {
|
||
|
number = (`${number}`).replace(/[^0-9+-Ee.]/g, '');
|
||
|
const n = !isFinite(+number) ? 0 : +number
|
||
|
const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
|
||
|
const sep = (typeof thousandsSeparator === 'undefined') ? ',' : thousandsSeparator
|
||
|
const dec = (typeof decimalPoint === 'undefined') ? '.' : decimalPoint
|
||
|
let s = ''
|
||
|
s = (prec ? round(n, prec) + '' : `${Math.round(n)}`).split('.')
|
||
|
const re = /(-?\d+)(\d{3})/
|
||
|
while (re.test(s[0])) {
|
||
|
s[0] = s[0].replace(re, `$1${sep}$2`)
|
||
|
}
|
||
|
if ((s[1] || '').length < prec) {
|
||
|
s[1] = s[1] || ''
|
||
|
s[1] += new Array(prec - s[1].length + 1).join('0')
|
||
|
}
|
||
|
let result = s.join(dec).split('.');
|
||
|
if (result.length > 1) result[result.length - 1] = `.${result[result.length - 1]}`
|
||
|
return result
|
||
|
}
|
||
|
function ruleParams(ruleForm : EmptyObjectType, rules : EmptyArrayType) {
|
||
|
let flag = true;
|
||
|
return new Promise((resolve, reject) => {
|
||
|
//对数据进行校验
|
||
|
let keys = Object.keys(ruleForm);
|
||
|
let values = Object.values(ruleForm);
|
||
|
if (rules && rules.length > 0) {
|
||
|
for (let i = 0; i < rules.length; i++) {
|
||
|
if (!validation(rules[i], ruleForm)) {
|
||
|
tips(rules[i].msg || '请先补全信息');
|
||
|
flag = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (flag) resolve(deepCopy(ruleForm));
|
||
|
else reject(flag);
|
||
|
} else {
|
||
|
resolve();
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
function validation(rule : EmptyObjectType, params : EmptyObjectType) {
|
||
|
console.log(rule, params[rule.prop], 'validation');
|
||
|
switch (rule.type) {
|
||
|
case 'string':
|
||
|
return patternTest(rule, params) && commonTest(rule, params);
|
||
|
break;
|
||
|
case 'number':
|
||
|
return patternTest(rule, params) && numberTest(rule, params);
|
||
|
break;
|
||
|
case 'array':
|
||
|
return arrayTest(rule, params);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
//基础校验
|
||
|
function commonTest(rule : EmptyObjectType, params : EmptyObjectType) {
|
||
|
let commonResult = [];
|
||
|
if (params[rule.prop]) commonResult.push(true);
|
||
|
if (rule.len) commonResult.push(params[rule.prop].length === rule.len);
|
||
|
if (rule.min) commonResult.push(params[rule.prop].length >= rule.min);
|
||
|
if (rule.max) commonResult.push(params[rule.prop].length <= rule.max);
|
||
|
if (rule.father) commonResult.push(params[rule.father] === params[rule.prop])
|
||
|
return commonResult && commonResult.length > 0 ? !commonResult.includes(false) : false;
|
||
|
}
|
||
|
//正则校验
|
||
|
function patternTest(rule : EmptyObjectType, params : EmptyObjectType) {
|
||
|
if (!rule.pattern) return true;
|
||
|
if (Array.isArray(rule.pattern)) {
|
||
|
let flag = false;
|
||
|
for (let i = 0; i < rule.pattern.length; i++) {
|
||
|
console.log(new RegExp(rule.pattern[i]), 'new RegExp(rule.pattern[i])');
|
||
|
function removeFirstAndLastChar(str, charToRemove) {
|
||
|
// 检查字符串是否以指定字符开头
|
||
|
if (str.startsWith(charToRemove)) str = str.slice(1);
|
||
|
// 检查字符串是否以指定字符结尾
|
||
|
if (str.endsWith(charToRemove)) str = str.slice(0, -1);
|
||
|
return str;
|
||
|
}
|
||
|
// 1.不需要首尾 / 分号 2.\d 需要替换为 \\d才能正确识别
|
||
|
let regExp = removeFirstAndLastChar(rule.pattern[i].toString(), '/').replace("\d", "\\d");
|
||
|
if (new RegExp(regExp).test(params[rule.prop])) {
|
||
|
flag = true;
|
||
|
break;
|
||
|
};
|
||
|
}
|
||
|
return flag;
|
||
|
} else return new RegExp(rule.pattern).test(params[rule.prop]);
|
||
|
|
||
|
}
|
||
|
//数字校验
|
||
|
function numberTest(rule : EmptyObjectType, params : EmptyObjectType) {
|
||
|
let commonResult = [];
|
||
|
if (params[rule.prop]) commonResult.push(true);
|
||
|
if (rule.min) commonResult.push(params[rule.prop] >= rule.min);
|
||
|
if (rule.max) commonResult.push(params[rule.prop] <= rule.max);
|
||
|
if (rule.father) commonResult.push(params[rule.father] === params[rule.prop])
|
||
|
return commonResult && commonResult.length > 0 ? !commonResult.includes(false) : false;
|
||
|
}
|
||
|
//数组校验
|
||
|
function arrayTest(rule : EmptyObjectType, params : EmptyObjectType) {
|
||
|
if (rule.keys) {
|
||
|
console.log(rule);
|
||
|
debugger
|
||
|
} else {
|
||
|
return params[rule.prop] && params[rule.prop].length > 0
|
||
|
}
|
||
|
}
|
||
|
//初始化对象赋值
|
||
|
function initRow(row : EmptyObjectType, params : EmptyObjectType) {
|
||
|
if (params) {
|
||
|
let rowKeys = Object.keys(row);
|
||
|
let paramsKeys = Object.keys(params);
|
||
|
let data = {};
|
||
|
rowKeys.forEach((item, index) => {
|
||
|
if (paramsKeys.indexOf(item) > -1) {
|
||
|
data[item] = params[item];
|
||
|
}
|
||
|
});
|
||
|
return data;
|
||
|
} else {
|
||
|
return deepCopy(row);
|
||
|
}
|
||
|
}
|
||
|
//对象里面是数组的转字符串拼接
|
||
|
function arrayToString(row : EmptyObjectType, split = ',') {
|
||
|
let paramsKeys = Object.keys(row);
|
||
|
let paramsValues = Object.values(row);
|
||
|
let params = deepCopy(row);
|
||
|
paramsValues.forEach((item, index) => {
|
||
|
if (Array.isArray(item)) {
|
||
|
params[paramsKeys[index]] = item.join(split);
|
||
|
}
|
||
|
});
|
||
|
return params;
|
||
|
}
|
||
|
//对象里面符合条件的转为数组
|
||
|
function stringToArray(row : EmptyObjectType, rule = [], split = ',') {
|
||
|
let paramsKeys = Object.keys(row);
|
||
|
let paramsValues = Object.values(row);
|
||
|
let params = deepCopy(row);
|
||
|
paramsKeys.forEach((item, index) => {
|
||
|
if (rule.indexOf(item) > -1) {
|
||
|
params[paramsKeys[index]] = params[paramsKeys[index]] ? params[paramsKeys[index]].split(split) : []
|
||
|
}
|
||
|
});
|
||
|
return params;
|
||
|
}
|
||
|
//过滤两个数组内容
|
||
|
function arrayfilter(arr1 : EmptyArrayType, arr2 : EmptyArrayType, flag = 1) {
|
||
|
// flag 1两个数组不同的内容 2 两个数组相同的内容
|
||
|
if (flag === 1) {
|
||
|
return [...new Set(arr1.map(item => JSON.stringify(item)))].filter(x => !new Set(arr2.map(item => JSON.stringify(item))).has(x)).map(item => JSON.parse(item));
|
||
|
} else {
|
||
|
return [...new Set(arr1.map(item => JSON.stringify(item)))].filter(x => new Set(arr2.map(item => JSON.stringify(item))).has(x)).map(item => JSON.parse(item));
|
||
|
}
|
||
|
}
|
||
|
function deepMerge(target = {}, source = {}) {
|
||
|
target = deepCopy(target)
|
||
|
if (typeof target !== 'object' || typeof source !== 'object') return false
|
||
|
for (const prop in source) {
|
||
|
if (!source.hasOwnProperty(prop)) continue
|
||
|
if (prop in target) {
|
||
|
if (typeof target[prop] !== 'object') {
|
||
|
target[prop] = source[prop]
|
||
|
} else if (typeof source[prop] !== 'object') {
|
||
|
target[prop] = source[prop]
|
||
|
} else if (target[prop].concat && source[prop].concat) {
|
||
|
target[prop] = target[prop].concat(source[prop])
|
||
|
} else {
|
||
|
target[prop] = deepMerge(target[prop], source[prop])
|
||
|
}
|
||
|
} else {
|
||
|
target[prop] = source[prop]
|
||
|
}
|
||
|
}
|
||
|
return target
|
||
|
}
|
||
|
export default {
|
||
|
hasLogin,
|
||
|
loading,
|
||
|
close,
|
||
|
tips,
|
||
|
jumpTo,
|
||
|
deepCopy,
|
||
|
priceFormat,
|
||
|
ruleParams,
|
||
|
validation,
|
||
|
initRow,
|
||
|
arrayToString,
|
||
|
stringToArray,
|
||
|
arrayfilter,
|
||
|
deepMerge,
|
||
|
...test
|
||
|
}
|