审计代码优化

This commit is contained in:
BianLzhaoMin 2024-08-30 17:45:46 +08:00
parent f00b9310bd
commit 8cfbf163c0
7 changed files with 1478 additions and 1222 deletions

View File

@ -4,27 +4,27 @@
* @param wait 延迟执行毫秒数
* @param immediate true - 立即执行 false - 延迟执行
*/
export const debounce = function(func, wait = 1000, immediate = true) {
let timer;
console.log(1);
return function() {
console.log(123);
let context = this,
args = arguments;
if (timer) clearTimeout(timer);
if (immediate) {
let callNow = !timer;
timer = setTimeout(() => {
timer = null;
}, wait);
if (callNow) func.apply(context, args);
} else {
timer = setTimeout(() => {
func.apply(context, args);
}, wait)
}
}
}
export const debounce = function (func, wait = 1000, immediate = true) {
let timer;
// console.log(1);
return function () {
// console.log(123);
let context = this,
args = arguments;
if (timer) clearTimeout(timer);
if (immediate) {
let callNow = !timer;
timer = setTimeout(() => {
timer = null;
}, wait);
if (callNow) func.apply(context, args);
} else {
timer = setTimeout(() => {
func.apply(context, args);
}, wait);
}
};
};
/**
* @desc 函数节流
* @param func 函数
@ -32,25 +32,25 @@ export const debounce = function(func, wait = 1000, immediate = true) {
* @param type 1 使用表时间戳在时间段开始的时候触发 2 使用表定时器在时间段结束的时候触发
*/
export const throttle = (func, wait = 1000, type = 1) => {
let previous = 0;
let timeout;
return function() {
let context = this;
let args = arguments;
if (type === 1) {
let now = Date.now();
let previous = 0;
let timeout;
return function () {
let context = this;
let args = arguments;
if (type === 1) {
let now = Date.now();
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
} else if (type === 2) {
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
}
}
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
} else if (type === 2) {
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args);
}, wait);
}
}
};
};

View File

@ -1,34 +1,28 @@
// 此版本发布于2023-03-27
const version = '2.0.36'
const version = "2.0.36";
// 开发环境才提示,生产环境不会提示
if (process.env.NODE_ENV === 'development') {
console.log(`\n %c uView V${version} %c https://uviewui.com/ \n\n`, 'color: #ffffff; background: #3c9cff; padding:5px 0; border-radius: 5px;');
if (process.env.NODE_ENV === "development") {
// console.log(`\n %c uView V${version} %c https://uviewui.com/ \n\n`, 'color: #ffffff; background: #3c9cff; padding:5px 0; border-radius: 5px;');
}
export default {
v: version,
version,
// 主题名称
type: [
'primary',
'success',
'info',
'error',
'warning'
],
type: ["primary", "success", "info", "error", "warning"],
// 颜色部分本来可以通过scss的:export导出供js使用但是奈何nvue不支持
color: {
'u-primary': '#2979ff',
'u-warning': '#ff9900',
'u-success': '#19be6b',
'u-error': '#fa3534',
'u-info': '#909399',
'u-main-color': '#303133',
'u-content-color': '#606266',
'u-tips-color': '#909399',
'u-light-color': '#c0c4cc'
"u-primary": "#2979ff",
"u-warning": "#ff9900",
"u-success": "#19be6b",
"u-error": "#fa3534",
"u-info": "#909399",
"u-main-color": "#303133",
"u-content-color": "#606266",
"u-tips-color": "#909399",
"u-light-color": "#c0c4cc",
},
// 默认单位可以通过配置为rpx那么在用于传入组件大小参数为数值时就默认为rpx
unit: 'px'
}
// 默认单位可以通过配置为rpx那么在用于传入组件大小参数为数值时就默认为rpx
unit: "px",
};

View File

@ -6,7 +6,7 @@ let _boundaryCheckingState = true; // 是否进行越界检查的全局开关
* @example strip(0.09999999999999998)=0.1
*/
function strip(num, precision = 15) {
return +parseFloat(Number(num).toPrecision(precision));
return +parseFloat(Number(num).toPrecision(precision));
}
/**
@ -15,10 +15,10 @@ function strip(num, precision = 15) {
* @param {*number} num Input number
*/
function digitLength(num) {
// Get digit length of e
const eSplit = num.toString().split(/[eE]/);
const len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
return len > 0 ? len : 0;
// Get digit length of e
const eSplit = num.toString().split(/[eE]/);
const len = (eSplit[0].split(".")[1] || "").length - +(eSplit[1] || 0);
return len > 0 ? len : 0;
}
/**
@ -27,11 +27,11 @@ function digitLength(num) {
* @param {*number} num 输入数
*/
function float2Fixed(num) {
if (num.toString().indexOf('e') === -1) {
return Number(num.toString().replace('.', ''));
}
const dLen = digitLength(num);
return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);
if (num.toString().indexOf("e") === -1) {
return Number(num.toString().replace(".", ""));
}
const dLen = digitLength(num);
return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);
}
/**
@ -40,11 +40,11 @@ function float2Fixed(num) {
* @param {*number} num 输入数
*/
function checkBoundary(num) {
if (_boundaryCheckingState) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(`${num} 超出了精度限制,结果可能不正确`);
if (_boundaryCheckingState) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
// console.warn(`${num} 超出了精度限制,结果可能不正确`);
}
}
}
}
/**
@ -54,14 +54,14 @@ function checkBoundary(num) {
* @private
*/
function iteratorOperation(arr, operation) {
const [num1, num2, ...others] = arr;
let res = operation(num1, num2);
const [num1, num2, ...others] = arr;
let res = operation(num1, num2);
others.forEach((num) => {
res = operation(res, num);
});
others.forEach((num) => {
res = operation(res, num);
});
return res;
return res;
}
/**
@ -69,19 +69,19 @@ function iteratorOperation(arr, operation) {
* @export
*/
export function times(...nums) {
if (nums.length > 2) {
return iteratorOperation(nums, times);
}
if (nums.length > 2) {
return iteratorOperation(nums, times);
}
const [num1, num2] = nums;
const num1Changed = float2Fixed(num1);
const num2Changed = float2Fixed(num2);
const baseNum = digitLength(num1) + digitLength(num2);
const leftValue = num1Changed * num2Changed;
const [num1, num2] = nums;
const num1Changed = float2Fixed(num1);
const num2Changed = float2Fixed(num2);
const baseNum = digitLength(num1) + digitLength(num2);
const leftValue = num1Changed * num2Changed;
checkBoundary(leftValue);
checkBoundary(leftValue);
return leftValue / Math.pow(10, baseNum);
return leftValue / Math.pow(10, baseNum);
}
/**
@ -89,15 +89,18 @@ export function times(...nums) {
* @export
*/
export function plus(...nums) {
if (nums.length > 2) {
return iteratorOperation(nums, plus);
}
if (nums.length > 2) {
return iteratorOperation(nums, plus);
}
const [num1, num2] = nums;
// 取最大的小数位
const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
// 把小数都转为整数然后再计算
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
const [num1, num2] = nums;
// 取最大的小数位
const baseNum = Math.pow(
10,
Math.max(digitLength(num1), digitLength(num2))
);
// 把小数都转为整数然后再计算
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
}
/**
@ -105,13 +108,16 @@ export function plus(...nums) {
* @export
*/
export function minus(...nums) {
if (nums.length > 2) {
return iteratorOperation(nums, minus);
}
if (nums.length > 2) {
return iteratorOperation(nums, minus);
}
const [num1, num2] = nums;
const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
const [num1, num2] = nums;
const baseNum = Math.pow(
10,
Math.max(digitLength(num1), digitLength(num2))
);
return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
}
/**
@ -119,17 +125,20 @@ export function minus(...nums) {
* @export
*/
export function divide(...nums) {
if (nums.length > 2) {
return iteratorOperation(nums, divide);
}
if (nums.length > 2) {
return iteratorOperation(nums, divide);
}
const [num1, num2] = nums;
const num1Changed = float2Fixed(num1);
const num2Changed = float2Fixed(num2);
checkBoundary(num1Changed);
checkBoundary(num2Changed);
// 重要这里必须用strip进行修正
return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
const [num1, num2] = nums;
const num1Changed = float2Fixed(num1);
const num2Changed = float2Fixed(num2);
checkBoundary(num1Changed);
checkBoundary(num2Changed);
// 重要这里必须用strip进行修正
return times(
num1Changed / num2Changed,
strip(Math.pow(10, digitLength(num2) - digitLength(num1)))
);
}
/**
@ -137,13 +146,13 @@ export function divide(...nums) {
* @export
*/
export function round(num, ratio) {
const base = Math.pow(10, ratio);
let result = divide(Math.round(Math.abs(times(num, base))), base);
if (num < 0 && result !== 0) {
result = times(result, -1);
}
// 位数不足则补0
return result;
const base = Math.pow(10, ratio);
let result = divide(Math.round(Math.abs(times(num, base))), base);
if (num < 0 && result !== 0) {
result = times(result, -1);
}
// 位数不足则补0
return result;
}
/**
@ -152,16 +161,14 @@ export function round(num, ratio) {
* @export
*/
export function enableBoundaryChecking(flag = true) {
_boundaryCheckingState = flag;
_boundaryCheckingState = flag;
}
export default {
times,
plus,
minus,
divide,
round,
enableBoundaryChecking,
times,
plus,
minus,
divide,
round,
enableBoundaryChecking,
};

File diff suppressed because it is too large Load Diff

View File

@ -11,98 +11,98 @@
* HBuilderX: beat-3.0.4 alpha-3.0.4
*/
import dispatchRequest from './dispatchRequest'
import InterceptorManager from './InterceptorManager'
import mergeConfig from './mergeConfig'
import defaults from './defaults'
import { isPlainObject } from '../utils'
import clone from '../utils/clone'
import dispatchRequest from "./dispatchRequest";
import InterceptorManager from "./InterceptorManager";
import mergeConfig from "./mergeConfig";
import defaults from "./defaults";
import { isPlainObject } from "../utils";
import clone from "../utils/clone";
export default class Request {
/**
* @param {Object} arg - 全局配置
* @param {String} arg.baseURL - 全局根路径
* @param {Object} arg.header - 全局header
* @param {String} arg.method = [GET|POST|PUT|DELETE|CONNECT|HEAD|OPTIONS|TRACE] - 全局默认请求方式
* @param {String} arg.dataType = [json] - 全局默认的dataType
* @param {String} arg.responseType = [text|arraybuffer] - 全局默认的responseType支付宝小程序不支持
* @param {Object} arg.custom - 全局默认的自定义参数
* @param {Number} arg.timeout - 全局默认的超时时间单位 ms默认60000H5(HBuilderX 2.9.9+)APP(HBuilderX 2.9.9+)微信小程序2.10.0支付宝小程序
* @param {Boolean} arg.sslVerify - 全局默认的是否验证 ssl 证书默认true.仅App安卓端支持HBuilderX 2.3.3+
* @param {Boolean} arg.withCredentials - 全局默认的跨域请求时是否携带凭证cookies默认false仅H5支持HBuilderX 2.6.15+
* @param {Boolean} arg.firstIpv4 - 全DNS解析时优先使用ipv4默认false App-Android 支持 (HBuilderX 2.8.0+)
* @param {Function(statusCode):Boolean} arg.validateStatus - 全局默认的自定义验证器默认statusCode >= 200 && statusCode < 300
*/
* @param {Object} arg - 全局配置
* @param {String} arg.baseURL - 全局根路径
* @param {Object} arg.header - 全局header
* @param {String} arg.method = [GET|POST|PUT|DELETE|CONNECT|HEAD|OPTIONS|TRACE] - 全局默认请求方式
* @param {String} arg.dataType = [json] - 全局默认的dataType
* @param {String} arg.responseType = [text|arraybuffer] - 全局默认的responseType支付宝小程序不支持
* @param {Object} arg.custom - 全局默认的自定义参数
* @param {Number} arg.timeout - 全局默认的超时时间单位 ms默认60000H5(HBuilderX 2.9.9+)APP(HBuilderX 2.9.9+)微信小程序2.10.0支付宝小程序
* @param {Boolean} arg.sslVerify - 全局默认的是否验证 ssl 证书默认true.仅App安卓端支持HBuilderX 2.3.3+
* @param {Boolean} arg.withCredentials - 全局默认的跨域请求时是否携带凭证cookies默认false仅H5支持HBuilderX 2.6.15+
* @param {Boolean} arg.firstIpv4 - 全DNS解析时优先使用ipv4默认false App-Android 支持 (HBuilderX 2.8.0+)
* @param {Function(statusCode):Boolean} arg.validateStatus - 全局默认的自定义验证器默认statusCode >= 200 && statusCode < 300
*/
constructor(arg = {}) {
if (!isPlainObject(arg)) {
arg = {}
console.warn('设置全局参数必须接收一个Object')
arg = {};
// console.warn('设置全局参数必须接收一个Object')
}
this.config = clone({ ...defaults, ...arg })
this.config = clone({ ...defaults, ...arg });
this.interceptors = {
request: new InterceptorManager(),
response: new InterceptorManager()
}
response: new InterceptorManager(),
};
}
/**
* @Function
* @param {Request~setConfigCallback} f - 设置全局默认配置
*/
* @Function
* @param {Request~setConfigCallback} f - 设置全局默认配置
*/
setConfig(f) {
this.config = f(this.config)
this.config = f(this.config);
}
middleware(config) {
config = mergeConfig(this.config, config)
const chain = [dispatchRequest, undefined]
let promise = Promise.resolve(config)
config = mergeConfig(this.config, config);
const chain = [dispatchRequest, undefined];
let promise = Promise.resolve(config);
this.interceptors.request.forEach((interceptor) => {
chain.unshift(interceptor.fulfilled, interceptor.rejected)
})
chain.unshift(interceptor.fulfilled, interceptor.rejected);
});
this.interceptors.response.forEach((interceptor) => {
chain.push(interceptor.fulfilled, interceptor.rejected)
})
chain.push(interceptor.fulfilled, interceptor.rejected);
});
while (chain.length) {
promise = promise.then(chain.shift(), chain.shift())
promise = promise.then(chain.shift(), chain.shift());
}
return promise
return promise;
}
/**
* @Function
* @param {Object} config - 请求配置项
* @prop {String} options.url - 请求路径
* @prop {Object} options.data - 请求参数
* @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型
* @prop {Object} [options.dataType = config.dataType] - 如果设为 json会尝试对返回的数据做一次 JSON.parse
* @prop {Object} [options.header = config.header] - 请求header
* @prop {Object} [options.method = config.method] - 请求方法
* @returns {Promise<unknown>}
*/
* @Function
* @param {Object} config - 请求配置项
* @prop {String} options.url - 请求路径
* @prop {Object} options.data - 请求参数
* @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型
* @prop {Object} [options.dataType = config.dataType] - 如果设为 json会尝试对返回的数据做一次 JSON.parse
* @prop {Object} [options.header = config.header] - 请求header
* @prop {Object} [options.method = config.method] - 请求方法
* @returns {Promise<unknown>}
*/
request(config = {}) {
return this.middleware(config)
return this.middleware(config);
}
get(url, options = {}) {
return this.middleware({
url,
method: 'GET',
...options
})
method: "GET",
...options,
});
}
post(url, data, options = {}) {
return this.middleware({
url,
data,
method: 'POST',
...options
})
method: "POST",
...options,
});
}
// #ifndef MP-ALIPAY
@ -110,9 +110,9 @@ export default class Request {
return this.middleware({
url,
data,
method: 'PUT',
...options
})
method: "PUT",
...options,
});
}
// #endif
@ -122,9 +122,9 @@ export default class Request {
return this.middleware({
url,
data,
method: 'DELETE',
...options
})
method: "DELETE",
...options,
});
}
// #endif
@ -134,9 +134,9 @@ export default class Request {
return this.middleware({
url,
data,
method: 'CONNECT',
...options
})
method: "CONNECT",
...options,
});
}
// #endif
@ -146,9 +146,9 @@ export default class Request {
return this.middleware({
url,
data,
method: 'HEAD',
...options
})
method: "HEAD",
...options,
});
}
// #endif
@ -158,9 +158,9 @@ export default class Request {
return this.middleware({
url,
data,
method: 'OPTIONS',
...options
})
method: "OPTIONS",
...options,
});
}
// #endif
@ -170,23 +170,23 @@ export default class Request {
return this.middleware({
url,
data,
method: 'TRACE',
...options
})
method: "TRACE",
...options,
});
}
// #endif
upload(url, config = {}) {
config.url = url
config.method = 'UPLOAD'
return this.middleware(config)
config.url = url;
config.method = "UPLOAD";
return this.middleware(config);
}
download(url, config = {}) {
config.url = url
config.method = 'DOWNLOAD'
return this.middleware(config)
config.url = url;
config.method = "DOWNLOAD";
return this.middleware(config);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
export class Mcaptcha {
constructor(options) {
this.options = options;
this.fontSize = options.height * 3 / 6;
this.fontSize = (options.height * 3) / 6;
this.init();
this.refresh();
}
@ -13,17 +13,80 @@ export class Mcaptcha {
this.ctx.setFillStyle(this.randomColor(180, 240));
}
refresh() {
var code = '';
var txtArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var code = "";
var txtArr = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
];
for (var i = 0; i < 4; i++) {
code += txtArr[this.randomNum(0, txtArr.length)];
}
this.options.createCodeImg = code;
let arr = (code + '').split('');
let arr = (code + "").split("");
if (arr.length === 0) {
arr = ['e', 'r', 'r', 'o', 'r'];
};
let offsetLeft = this.options.width * 0.6 / (arr.length - 1);
arr = ["e", "r", "r", "o", "r"];
}
let offsetLeft = (this.options.width * 0.6) / (arr.length - 1);
let marginLeft = this.options.width * 0.2;
arr.forEach((item, index) => {
this.ctx.setFillStyle(this.randomColor(0, 180));
@ -32,22 +95,34 @@ export class Mcaptcha {
let dis = offsetLeft * index + marginLeft - size * 0.3;
let deg = this.randomNum(-30, 30);
this.ctx.translate(dis, this.options.height * 0.5);
this.ctx.rotate(deg * Math.PI / 180);
this.ctx.rotate((deg * Math.PI) / 180);
this.ctx.fillText(item, 0, 0);
this.ctx.rotate(-deg * Math.PI / 180);
this.ctx.rotate((-deg * Math.PI) / 180);
this.ctx.translate(-dis, -this.options.height * 0.5);
})
});
for (var i = 0; i < 4; i++) {
this.ctx.strokeStyle = this.randomColor(40, 180);
this.ctx.beginPath();
this.ctx.moveTo(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height));
this.ctx.lineTo(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height));
this.ctx.moveTo(
this.randomNum(0, this.options.width),
this.randomNum(0, this.options.height)
);
this.ctx.lineTo(
this.randomNum(0, this.options.width),
this.randomNum(0, this.options.height)
);
this.ctx.stroke();
}
for (var i = 0; i < this.options.width / 4; i++) {
this.ctx.fillStyle = this.randomColor(0, 255);
this.ctx.beginPath();
this.ctx.arc(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height), 1, 0, 2 * Math.PI);
this.ctx.arc(
this.randomNum(0, this.options.width),
this.randomNum(0, this.options.height),
1,
0,
2 * Math.PI
);
this.ctx.fill();
}
this.ctx.draw();
@ -55,8 +130,8 @@ export class Mcaptcha {
validate(code) {
var code = code.toLowerCase();
var v_code = this.options.createCodeImg.toLowerCase();
console.log(code)
console.log(v_code.substring(v_code.length - 4))
// console.log(code)
// console.log(v_code.substring(v_code.length - 4))
if (code == v_code.substring(v_code.length - 4)) {
return true;
} else {
@ -72,4 +147,4 @@ export class Mcaptcha {
let b = this.randomNum(min, max);
return "rgb(" + r + "," + g + "," + b + ")";
}
}
}