This commit is contained in:
houjunxiang
2025-10-09 18:19:55 +08:00
parent f2ffc65094
commit 386f1e7466
1553 changed files with 284685 additions and 32820 deletions

View File

@@ -0,0 +1,290 @@
import { create, all, number, pow } from 'mathjs'
const math = create(all)
math.config({
number: 'BigNumber',
precision: 64
})
/*
* 计算分析值*/
export function calcAnalysisValue(group) {
try {
for (const g of group) {
for (const ele of g.fields) {
if (!ele.formula || ele.formula == '') continue
if (ele.valueTypeManual && ele.valueTypeManual == '1') continue
let formula = ele.formula
let formulas = formula.split('|')
let formulaVal = ''
let hasNullVal = false
formulas.forEach(f => {
let value = ''
if (f.charAt(0) === 'p') {
// let o = elementParamValueData.find((i) => 'p' + i.paramNo === f);
let o = findFieldInGroup(f, group, 'p')
value = o.value
} else if (f.charAt(0) === 'e') {
// let o = elementParamValueData.find((i) => 'e' + i.paramNo === f);
let o = findFieldInGroup(f, group, 'e')
value = o.value
} else if (f.charAt(0) === '<' || f.charAt(0) === '>') {
value = "'" + f + "'"
} else {
value = f
}
if (typeof value == 'undefined' || value == null) {
hasNullVal = true
return true
}
formulaVal += value
})
if (hasNullVal) {
ele.value = null
continue
}
let v
if (formulaVal.startsWith('Get')) {
//计算公式为Get开头的都是执行方法
v = eval(formulaVal)
} else {
v = math.evaluate(formulaVal).toString()
v = isFinite(v) ? v.toString() : ''
}
ele.value = handleRoundFiveNumber(v, ele.dataType)
}
}
} catch (error) {
console.log(error)
}
}
const findFieldInGroup = function (paramNo, group, p) {
for (const g of group) {
for (const f of g.fields) {
if (p + f.paramNo === paramNo) {
return f
}
}
}
return { value: null }
}
const GetRecoveryRate = function (elementFormulaCode, value) {
// //读取conRecoveryRateList
const conRecoveryRateList = uni.getStorageSync('ConRecoveryRateList')
if (!conRecoveryRateList) return
let o = conRecoveryRateList.find(i => {
//console.log("取得的" + i + "回收率:" + i);
if (value === 0) {
return i.elementFormulaCode === elementFormulaCode && i.minValue === value
}
return i.elementFormulaCode === elementFormulaCode && i.minValue < value && value <= i.maxValue
})
// console.log(o);
const rate = o ? o.rate : 100
// console.log("取得的" + elementFormulaCode + "回收率:" + rate);
return rate
// console.log("执行方法GetRecoveryRate");
// return 100;
}
/*
* 根据硫值计算:支持碳、硝酸钾(滇中)
* 当S%<22%时,需加入淀粉的量=(75-S%*22*M)/12
* 当 S%>22%时,需加入硝酸钾的量=(S%*22*M-75)/4
* 当 S%==22%时硝酸钾为0淀粉为空
* sValueKey硫值对应的dicKey
* weightKey重量对应的dicKey
* percent计算参数
*
* 举例Get_C_KNO3_bySValue|(|p14|, |p2|, |>|)
* */
const Get_C_KNO3_bySValue = function (sValue, weight, operator) {
//判断sValue是数字
if (isNaN(sValue) || isNaN(weight)) {
return ''
}
let v = number(sValue)
const w = number(weight)
if (w === 0) return ''
v = v * 0.01
//当S%<22%时,需加入淀粉的量=(75-S%*22*M)/12
if (operator === '<' && v < 0.22) {
// return ( 75 - v * 22 * w )/12;
return accDiv(accSub(75, accMul(accMul(v, 22), w)), 12)
}
//S%>22%时,需加入硝酸钾的量=(S%*22*M-75)/4
if (operator === '>=' && v >= 0.22) {
//S%==22%时硝酸钾为0淀粉为空
if (v === 22) return 0
// return ( v * 22 * w - 75 ) + 44;
return accDiv(accSub(accMul(v, accMul(22, w)), 75), 4)
}
return ''
}
/** 处理数值数据:四舍六入奇进偶不进
* 1如果取小数的最后一位为55前为奇数进位为偶不进,五后非零就进一,五后皆零看奇偶,五前为偶应舍去,五前为奇要进一
* 2.5后不为0时就入为0时看5前奇进偶不进
* 3.四舍,六入
* 传入数值和保留位数
*/
export function handleRoundFiveNumber(number, fixed = 0) {
if (number == null || number === '' || isNaN(number)) return number
if (fixed === -1) return number
number = String(number)
//可以考虑清掉末尾的0暂时限制了不会有,因为传入数值末尾0是去掉的
const index = number.indexOf('.')
if (index == -1) {
if (fixed > 0) {
number += '.'
}
for (let i = 0; i < fixed; i++) {
number += '0'
}
//补0返回
return number
}
//取到保留小数位的下一位5.5555取保留俩位,那就是小数点后第三位
const indexFixed = index + fixed + 1
if (indexFixed >= number.length) {
//如果小数位数不够, 补0直接返回
const zerolen = indexFixed - number.length
for (let i = 0; i < zerolen; i++) {
number += '0'
}
return number
}
//取保留位数的后一位做判断以下是有5的判断
const endNumber = number.substr(indexFixed, 1)
if (endNumber != '5') {
//如果做判断的数不是五就按正常的四舍五入即忽略了5后面补0的那些由于是数字传进来0已经去掉此处不做处理
return Number(number).toFixed(fixed)
}
if (indexFixed != number.length - 1) {
//由于当前判断位不是最后一位而又去除了0那么后面后的位数应该直接入位五后不为0时入,由于会有小于五的四舍五入肯定不行那么只能截取到当前保留位数然后转成数字加上10的负fixed的次方即可
number = number.substring(0, indexFixed)
if (number.indexOf('-') != -1) {
//需要考虑到负数的情况
return (Number(number) - Number(Math.pow(10, -fixed))).toFixed(fixed)
}
return (Number(number) + Number(Math.pow(10, -fixed))).toFixed(fixed)
//return this.accAdd(number,Math.pow(10,-fixed))
}
//接下来就时五后没有值也就是0的需要看前面的奇入偶不入了取当前位的上一位
let twoNumber = number.substr(indexFixed - 1, 1)
if (twoNumber == '.') {
//取到小数点,再取一次,要取到小数点左边第一位
twoNumber = number.substr(indexFixed - 2, 1)
}
if ('13579'.indexOf(twoNumber) != -1) {
//奇进
number = number.substring(0, indexFixed)
if (number.indexOf('-') != -1) {
//需要考虑到负数的情况
return (Number(number) - Math.pow(10, -fixed)).toFixed(fixed)
}
return (Number(number) + Math.pow(10, -fixed)).toFixed(fixed)
}
//偶不进,将取值的当前位数,直接截取字符即可
return number.substr(0, indexFixed)
}
/**
** 除法函数,用来得到精确的除法结果
** 说明javascript的除法结果会有误差在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
** 调用accDiv(arg1,arg2)
** 返回值arg1除以arg2的精确结果
**/
function accDiv(arg1, arg2) {
var t1 = 0,
t2 = 0,
r1,
r2
try {
t1 = arg1.toString().split('.')[1].length
} catch (e) {}
try {
t2 = arg2.toString().split('.')[1].length
} catch (e) {}
r1 = Number(arg1.toString().replace('.', ''))
r2 = Number(arg2.toString().replace('.', ''))
return (r1 / r2) * pow(10, t2 - t1)
}
/**
** 乘法函数,用来得到精确的乘法结果
** 说明javascript的乘法结果会有误差在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
** 调用accMul(arg1,arg2)
** 返回值arg1乘以 arg2的精确结果
**/
function accMul(arg1, arg2) {
var m = 0,
s1 = arg1.toString(),
s2 = arg2.toString()
try {
m += s1.split('.')[1].length
} catch (e) {}
try {
m += s2.split('.')[1].length
} catch (e) {}
return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m)
}
/**
** 减法函数,用来得到精确的减法结果
** 说明javascript的减法结果会有误差在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果。
** 调用accSub(arg1,arg2)
** 返回值arg1加上arg2的精确结果
**/
function accSub(arg1, arg2) {
var r1, r2, m, n
try {
r1 = arg1.toString().split('.')[1].length
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split('.')[1].length
} catch (e) {
r2 = 0
}
m = Math.pow(10, Math.max(r1, r2)) //last modify by deeka //动态控制精度长度
n = r1 >= r2 ? r1 : r2
return ((arg1 * m - arg2 * m) / m).toFixed(n)
}
/**
** 加法函数,用来得到精确的加法结果
** 说明javascript的加法结果会有误差在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
** 调用accAdd(arg1,arg2)
** 返回值arg1加上arg2的精确结果
**/
function accAdd(arg1, arg2) {
var r1, r2, m, c
try {
r1 = arg1.toString().split('.')[1].length
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split('.')[1].length
} catch (e) {
r2 = 0
}
c = Math.abs(r1 - r2)
m = Math.pow(10, Math.max(r1, r2))
if (c > 0) {
var cm = Math.pow(10, c)
if (r1 > r2) {
arg1 = Number(arg1.toString().replace('.', ''))
arg2 = Number(arg2.toString().replace('.', '')) * cm
} else {
arg1 = Number(arg1.toString().replace('.', '')) * cm
arg2 = Number(arg2.toString().replace('.', ''))
}
} else {
arg1 = Number(arg1.toString().replace('.', ''))
arg2 = Number(arg2.toString().replace('.', ''))
}
return (arg1 + arg2) / m
}

168
nx/helper/digit.js Normal file
View File

@@ -0,0 +1,168 @@
let _boundaryCheckingState = true; // 是否进行越界检查的全局开关
/**
* 把错误的数据转正
* @private
* @example strip(0.09999999999999998)=0.1
*/
function strip(num, precision = 15) {
return +parseFloat(Number(num).toPrecision(precision));
}
/**
* Return digits length of a number
* @private
* @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;
}
/**
* 把小数转成整数,如果是小数则放大成整数
* @private
* @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);
}
/**
* 检测数字是否越界,如果越界给出提示
* @private
* @param {*number} num 输入数
*/
function checkBoundary(num) {
if (_boundaryCheckingState) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(`${num} 超出了精度限制,结果可能不正确`);
}
}
}
/**
* 把递归操作扁平迭代化
* @param {number[]} arr 要操作的数字数组
* @param {function} operation 迭代操作
* @private
*/
function iteratorOperation(arr, operation) {
const [num1, num2, ...others] = arr;
let res = operation(num1, num2);
others.forEach((num) => {
res = operation(res, num);
});
return res;
}
/**
* 高精度乘法
* @export
*/
export function times(...nums) {
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;
checkBoundary(leftValue);
return leftValue / Math.pow(10, baseNum);
}
/**
* 高精度加法
* @export
*/
export function plus(...nums) {
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;
}
/**
* 高精度减法
* @export
*/
export function minus(...nums) {
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;
}
/**
* 高精度除法
* @export
*/
export function divide(...nums) {
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))),
);
}
/**
* 四舍五入
* @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;
}
/**
* 是否进行边界检查,默认开启
* @param flag 标记开关true 为开启false 为关闭,默认为 true
* @export
*/
export function enableBoundaryChecking(flag = true) {
_boundaryCheckingState = flag;
}
export default {
times,
plus,
minus,
divide,
round,
enableBoundaryChecking,
};

749
nx/helper/index.js Normal file
View File

@@ -0,0 +1,749 @@
import test from './test.js'
import { round } from './digit.js'
/**
* @description 如果value小于min取min如果value大于max取max
* @param {number} min
* @param {number} max
* @param {number} value
*/
function range(min = 0, max = 0, value = 0) {
return Math.max(min, Math.min(max, Number(value)))
}
/**
* @description 用于获取用户传递值的px值 如果用户传递了"xxpx"或者"xxrpx",取出其数值部分,如果是"xxxrpx"还需要用过uni.upx2px进行转换
* @param {number|string} value 用户传递值的px值
* @param {boolean} unit
* @returns {number|string}
*/
export function getPx(value, unit = false) {
if (test.number(value)) {
return unit ? `${value}px` : Number(value)
}
// 如果带有rpx先取出其数值部分再转为px值
if (/(rpx|upx)$/.test(value)) {
return unit ? `${uni.upx2px(parseInt(value))}px` : Number(uni.upx2px(parseInt(value)))
}
return unit ? `${parseInt(value)}px` : parseInt(value)
}
/**
* @description 进行延时,以达到可以简写代码的目的
* @param {number} value 堵塞时间 单位ms 毫秒
* @returns {Promise} 返回promise
*/
export function sleep(value = 30) {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, value)
})
}
/**
* @description 运行期判断平台
* @returns {string} 返回所在平台(小写)
* @link 运行期判断平台 https://uniapp.dcloud.io/frame?id=判断平台
*/
export function os() {
return uni.getSystemInfoSync().platform.toLowerCase()
}
/**
* @description 获取系统信息同步接口
* @link 获取系统信息同步接口 https://uniapp.dcloud.io/api/system/info?id=getsysteminfosync
*/
export function sys() {
return uni.getSystemInfoSync()
}
/**
* @description 取一个区间数
* @param {Number} min 最小值
* @param {Number} max 最大值
*/
function random(min, max) {
if (min >= 0 && max > 0 && max >= min) {
const gab = max - min + 1
return Math.floor(Math.random() * gab + min)
}
return 0
}
/**
* @param {Number} len uuid的长度
* @param {Boolean} firstU 将返回的首字母置为"u"
* @param {Nubmer} radix 生成uuid的基数(意味着返回的字符串都是这个基数),2-二进制,8-八进制,10-十进制,16-十六进制
*/
export function guid(len = 32, firstU = true, radix = null) {
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
const uuid = []
radix = radix || chars.length
if (len) {
// 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
} else {
let r
// rfc4122标准要求返回的uuid中,某些位为固定的字符
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
uuid[14] = '4'
for (let i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | (Math.random() * 16)
uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
}
}
}
// 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
if (firstU) {
uuid.shift()
return `u${uuid.join('')}`
}
return uuid.join('')
}
/**
* @description 获取父组件的参数因为支付宝小程序不支持provide/inject的写法
this.$parent在非H5中可以准确获取到父组件但是在H5中需要多次this.$parent.$parent.xxx
这里默认值等于undefined有它的含义因为最顶层元素(组件)的$parent就是undefined意味着不传name
值(默认为undefined),就是查找最顶层的$parent
* @param {string|undefined} name 父组件的参数名
*/
export function $parent(name = undefined) {
let parent = this.$parent
// 通过while历遍这里主要是为了H5需要多层解析的问题
while (parent) {
// 父组件
if (parent.$options && parent.$options.name !== name) {
// 如果组件的name不相等继续上一级寻找
parent = parent.$parent
} else {
return parent
}
}
return false
}
/**
* @description 样式转换
* 对象转字符串,或者字符串转对象
* @param {object | string} customStyle 需要转换的目标
* @param {String} target 转换的目的object-转为对象string-转为字符串
* @returns {object|string}
*/
export function addStyle(customStyle, target = 'object') {
// 字符串转字符串,对象转对象情形,直接返回
if (
test.empty(customStyle) ||
(typeof customStyle === 'object' && target === 'object') ||
(target === 'string' && typeof customStyle === 'string')
) {
return customStyle
}
// 字符串转对象
if (target === 'object') {
// 去除字符串样式中的两端空格(中间的空格不能去掉比如padding: 20px 0如果去掉了就错了),空格是无用的
customStyle = trim(customStyle)
// 根据";"将字符串转为数组形式
const styleArray = customStyle.split(';')
const style = {}
// 历遍数组,拼接成对象
for (let i = 0; i < styleArray.length; i++) {
// 'font-size:20px;color:red;',如此最后字符串有";"的话会导致styleArray最后一个元素为空字符串这里需要过滤
if (styleArray[i]) {
const item = styleArray[i].split(':')
style[trim(item[0])] = trim(item[1])
}
}
return style
}
// 这里为对象转字符串形式
let string = ''
for (const i in customStyle) {
// 驼峰转为中划线的形式否则css内联样式无法识别驼峰样式属性名
const key = i.replace(/([A-Z])/g, '-$1').toLowerCase()
string += `${key}:${customStyle[i]};`
}
// 去除两端空格
return trim(string)
}
/**
* @description 添加单位如果有rpxupx%px等单位结尾或者值为auto直接返回否则加上px单位结尾
* @param {string|number} value 需要添加单位的值
* @param {string} unit 添加的单位名 比如px
*/
export function addUnit(value = 'auto', unit = 'px') {
value = String(value)
return test.number(value) ? `${value}${unit}` : value
}
/**
* @description 深度克隆
* @param {object} obj 需要深度克隆的对象
* @returns {*} 克隆后的对象或者原值(不是对象)
*/
function deepClone(obj) {
// 对常见的“非”值,直接返回原来值
if ([null, undefined, NaN, false].includes(obj)) return obj
if (typeof obj !== 'object' && typeof obj !== 'function') {
// 原始类型直接返回
return obj
}
const o = test.array(obj) ? [] : {}
for (const i in obj) {
if (obj.hasOwnProperty(i)) {
o[i] = typeof obj[i] === 'object' ? deepClone(obj[i]) : obj[i]
}
}
return o
}
/**
* @description JS对象深度合并
* @param {object} target 需要拷贝的对象
* @param {object} source 拷贝的来源对象
* @returns {object|boolean} 深度合并后的对象或者false入参有不是对象
*/
export function deepMerge(target = {}, source = {}) {
target = deepClone(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
}
/**
* @description error提示
* @param {*} err 错误内容
*/
function error(err) {
// 开发环境才提示,生产环境不会提示
if (process.env.NODE_ENV === 'development') {
console.error(`SheepJS:${err}`)
}
}
/**
* @description 打乱数组
* @param {array} array 需要打乱的数组
* @returns {array} 打乱后的数组
*/
function randomArray(array = []) {
// 原理是sort排序,Math.random()产生0<= x < 1之间的数,会导致x-0.05大于或者小于0
return array.sort(() => Math.random() - 0.5)
}
// padStart 的 polyfill因为某些机型或情况还无法支持es7的padStart比如电脑版的微信小程序
// 所以这里做一个兼容polyfill的兼容处理
if (!String.prototype.padStart) {
// 为了方便表示这里 fillString 用了ES6 的默认参数,不影响理解
String.prototype.padStart = function (maxLength, fillString = ' ') {
if (Object.prototype.toString.call(fillString) !== '[object String]') {
throw new TypeError('fillString must be String')
}
const str = this
// 返回 String(str) 这里是为了使返回的值是字符串字面量,在控制台中更符合直觉
if (str.length >= maxLength) return String(str)
const fillLength = maxLength - str.length
let times = Math.ceil(fillLength / fillString.length)
while ((times >>= 1)) {
fillString += fillString
if (times === 1) {
fillString += fillString
}
}
return fillString.slice(0, fillLength) + str
}
}
/**
* @description 格式化时间
* @param {String|Number} dateTime 需要格式化的时间戳
* @param {String} fmt 格式化规则 yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合 默认yyyy-mm-dd
* @returns {string} 返回格式化后的字符串
*/
function timeFormat(dateTime = null, formatStr = 'yyyy-mm-dd') {
let date
// 若传入时间为假值,则取当前时间
if (!dateTime) {
date = new Date()
}
// 若为unix秒时间戳则转为毫秒时间戳逻辑有点奇怪但不敢改以保证历史兼容
else if (/^\d{10}$/.test(dateTime?.toString().trim())) {
date = new Date(dateTime * 1000)
}
// 若用户传入字符串格式时间戳new Date无法解析需做兼容
else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) {
date = new Date(Number(dateTime))
}
// 其他都认为符合 RFC 2822 规范
else {
// 处理平台性差异在Safari/Webkit中new Date仅支持/作为分割符的字符串时间
date = new Date(typeof dateTime === 'string' ? dateTime.replace(/-/g, '/') : dateTime)
}
const timeSource = {
y: date.getFullYear().toString(), // 年
m: (date.getMonth() + 1).toString().padStart(2, '0'), // 月
d: date.getDate().toString().padStart(2, '0'), // 日
h: date.getHours().toString().padStart(2, '0'), // 时
M: date.getMinutes().toString().padStart(2, '0'), // 分
s: date.getSeconds().toString().padStart(2, '0') // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
}
for (const key in timeSource) {
const [ret] = new RegExp(`${key}+`).exec(formatStr) || []
if (ret) {
// 年可能只需展示两位
const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0
formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex))
}
}
return formatStr
}
/**
* @description 时间戳转为多久之前
* @param {String|Number} timestamp 时间戳
* @param {String|Boolean} format
* 格式化规则如果为时间格式字符串,超出一定时间范围,返回固定的时间格式;
* 如果为布尔值false无论什么时间都返回多久以前的格式
* @returns {string} 转化后的内容
*/
function timeFrom(timestamp = null, format = 'yyyy-mm-dd') {
if (timestamp == null) timestamp = Number(new Date())
timestamp = parseInt(timestamp)
// 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
if (timestamp.toString().length == 10) timestamp *= 1000
let timer = new Date().getTime() - timestamp
timer = parseInt(timer / 1000)
// 如果小于5分钟,则返回"刚刚",其他以此类推
let tips = ''
switch (true) {
case timer < 300:
tips = '刚刚'
break
case timer >= 300 && timer < 3600:
tips = `${parseInt(timer / 60)}分钟前`
break
case timer >= 3600 && timer < 86400:
tips = `${parseInt(timer / 3600)}小时前`
break
case timer >= 86400 && timer < 2592000:
tips = `${parseInt(timer / 86400)}天前`
break
default:
// 如果format为false则无论什么时间戳都显示xx之前
if (format === false) {
if (timer >= 2592000 && timer < 365 * 86400) {
tips = `${parseInt(timer / (86400 * 30))}个月前`
} else {
tips = `${parseInt(timer / (86400 * 365))}年前`
}
} else {
tips = timeFormat(timestamp, format)
}
}
return tips
}
/**
* @description 去除空格
* @param String str 需要去除空格的字符串
* @param String pos both(左右)|left|right|all 默认both
*/
function trim(str, pos = 'both') {
str = String(str)
if (pos == 'both') {
return str.replace(/^\s+|\s+$/g, '')
}
if (pos == 'left') {
return str.replace(/^\s*/, '')
}
if (pos == 'right') {
return str.replace(/(\s*$)/g, '')
}
if (pos == 'all') {
return str.replace(/\s+/g, '')
}
return str
}
/**
* @description 对象转url参数
* @param {object} data,对象
* @param {Boolean} isPrefix,是否自动加上"?"
* @param {string} arrayFormat 规则 indices|brackets|repeat|comma
*/
function queryParams(data = {}, isPrefix = true, arrayFormat = 'brackets') {
const prefix = isPrefix ? '?' : ''
const _result = []
if (['indices', 'brackets', 'repeat', 'comma'].indexOf(arrayFormat) == -1) arrayFormat = 'brackets'
for (const key in data) {
const value = data[key]
// 去掉为空的参数
if (['', undefined, null].indexOf(value) >= 0) {
continue
}
// 如果值为数组,另行处理
if (value.constructor === Array) {
// e.g. {ids: [1, 2, 3]}
switch (arrayFormat) {
case 'indices':
// 结果: ids[0]=1&ids[1]=2&ids[2]=3
for (let i = 0; i < value.length; i++) {
_result.push(`${key}[${i}]=${value[i]}`)
}
break
case 'brackets':
// 结果: ids[]=1&ids[]=2&ids[]=3
value.forEach(_value => {
_result.push(`${key}[]=${_value}`)
})
break
case 'repeat':
// 结果: ids=1&ids=2&ids=3
value.forEach(_value => {
_result.push(`${key}=${_value}`)
})
break
case 'comma':
// 结果: ids=1,2,3
let commaStr = ''
value.forEach(_value => {
commaStr += (commaStr ? ',' : '') + _value
})
_result.push(`${key}=${commaStr}`)
break
default:
value.forEach(_value => {
_result.push(`${key}[]=${_value}`)
})
}
} else {
_result.push(`${key}=${value}`)
}
}
return _result.length ? prefix + _result.join('&') : ''
}
/**
* 显示消息提示框
* @param {String} title 提示的内容,长度与 icon 取值有关。
* @param {Number} duration 提示的延迟时间单位毫秒默认2000
*/
function toast(title, duration = 2000) {
uni.showToast({
title: String(title),
icon: 'none',
duration
})
}
/**
* @description 根据主题type值,获取对应的图标
* @param {String} type 主题名称,primary|info|error|warning|success
* @param {boolean} fill 是否使用fill填充实体的图标
*/
function type2icon(type = 'success', fill = false) {
// 如果非预置值,默认为success
if (['primary', 'info', 'error', 'warning', 'success'].indexOf(type) == -1) type = 'success'
let iconName = ''
// 目前(2019-12-12),info和primary使用同一个图标
switch (type) {
case 'primary':
iconName = 'info-circle'
break
case 'info':
iconName = 'info-circle'
break
case 'error':
iconName = 'close-circle'
break
case 'warning':
iconName = 'error-circle'
break
case 'success':
iconName = 'checkmark-circle'
break
default:
iconName = 'checkmark-circle'
}
// 是否是实体类型,加上-fill,在icon组件库中,实体的类名是后面加-fill的
if (fill) iconName += '-fill'
return iconName
}
/**
* @description 数字格式化
* @param {number|string} number 要格式化的数字
* @param {number} decimals 保留几位小数
* @param {string} decimalPoint 小数点符号
* @param {string} thousandsSeparator 千分位符号
* @returns {string} 格式化后的数字
*/
function priceFormat(number, decimals = 0, 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')
}
return s.join(dec)
}
/**
* @description 获取duration值
* 如果带有ms或者s直接返回如果大于一定值认为是ms单位小于一定值认为是s单位
* 比如以30位阈值那么300大于30可以理解为用户想要的是300ms而不是想花300s去执行一个动画
* @param {String|number} value 比如: "1s"|"100ms"|1|100
* @param {boolean} unit 提示: 如果是false 默认返回number
* @return {string|number}
*/
function getDuration(value, unit = true) {
const valueNum = parseInt(value)
if (unit) {
if (/s$/.test(value)) return value
return value > 30 ? `${value}ms` : `${value}s`
}
if (/ms$/.test(value)) return valueNum
if (/s$/.test(value)) return valueNum > 30 ? valueNum : valueNum * 1000
return valueNum
}
/**
* @description 日期的月或日补零操作
* @param {String} value 需要补零的值
*/
function padZero(value) {
return `00${value}`.slice(-2)
}
/**
* @description 获取某个对象下的属性,用于通过类似'a.b.c'的形式去获取一个对象的的属性的形式
* @param {object} obj 对象
* @param {string} key 需要获取的属性字段
* @returns {*}
*/
function getProperty(obj, key) {
if (!obj) {
return
}
if (typeof key !== 'string' || key === '') {
return ''
}
if (key.indexOf('.') !== -1) {
const keys = key.split('.')
let firstObj = obj[keys[0]] || {}
for (let i = 1; i < keys.length; i++) {
if (firstObj) {
firstObj = firstObj[keys[i]]
}
}
return firstObj
}
return obj[key]
}
/**
* @description 设置对象的属性值,如果'a.b.c'的形式进行设置
* @param {object} obj 对象
* @param {string} key 需要设置的属性
* @param {string} value 设置的值
*/
function setProperty(obj, key, value) {
if (!obj) {
return
}
// 递归赋值
const inFn = function (_obj, keys, v) {
// 最后一个属性key
if (keys.length === 1) {
_obj[keys[0]] = v
return
}
// 0~length-1个key
while (keys.length > 1) {
const k = keys[0]
if (!_obj[k] || typeof _obj[k] !== 'object') {
_obj[k] = {}
}
const key = keys.shift()
// 自调用判断是否存在属性,不存在则自动创建对象
inFn(_obj[k], keys, v)
}
}
if (typeof key !== 'string' || key === '') {
} else if (key.indexOf('.') !== -1) {
// 支持多层级赋值操作
const keys = key.split('.')
inFn(obj, keys, value)
} else {
obj[key] = value
}
}
/**
* @description 获取当前页面路径
*/
function page() {
const pages = getCurrentPages()
// 某些特殊情况下(比如页面进行redirectTo时的一些时机)pages可能为空数组
return `/${pages[pages.length - 1]?.route ?? ''}`
}
/**
* @description 获取当前路由栈实例数组
*/
function pages() {
const pages = getCurrentPages()
return pages
}
/**
* 获取H5-真实根地址 兼容hash+history模式
*/
export function getRootUrl() {
let url = ''
// #ifdef H5
url = location.origin + location.pathname
if (location.hash !== '') {
url += '#/'
}
// #endif
return url
}
/**
* copyText 多端复制文本
*/
export function copyText(text) {
// #ifndef H5
uni.setClipboardData({
data: text,
success: function () {
toast('复制成功!')
},
fail: function () {
toast('复制失败!')
}
})
// #endif
// #ifdef H5
var createInput = document.createElement('textarea')
createInput.value = text
document.body.appendChild(createInput)
createInput.select()
document.execCommand('Copy')
createInput.className = 'createInput'
createInput.style.display = 'none'
toast('复制成功')
// #endif
}
// 提示
const showToast = (data = {}) => {
if (typeof data == 'string') {
uni.showToast({
title: data,
icon: 'none'
})
} else {
uni.showToast({
title: data.title,
icon: data.icon || 'none',
image: data.image || '',
mask: data.mask || false,
position: data.position || 'center',
duration: data.duration || 1500,
success: () => {
setTimeout(() => {
if (data.back) return uni.navigateBack()
data.success && data.success()
}, data.duration || 1500)
}
})
}
}
//uuid生成
const uuid = () => {
var s = []
var hexDigits = '0123456789abcdef'
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = '-'
var uuid = s.join('')
return uuid
}
export default {
range,
getPx,
sleep,
os,
sys,
random,
guid,
$parent,
addStyle,
addUnit,
deepClone,
deepMerge,
error,
randomArray,
timeFormat,
timeFrom,
trim,
queryParams,
toast,
type2icon,
priceFormat,
getDuration,
padZero,
getProperty,
setProperty,
page,
pages,
test,
getRootUrl,
copyText,
showToast,
uuid
}

179
nx/helper/measure.js Normal file
View File

@@ -0,0 +1,179 @@
import { getWebSocketUrl } from '@/defaultBaseUrl'
export default {
isOpen: false,
isOpening: false,
socketTask: false,
timeoutID: -1,
regData: '',
webSocketUrl: getWebSocketUrl(),
//开始心跳
heartbeatStart() {
if (this.timeoutID != -1) {
this.heartbeatClear()
}
this.timeoutID = setTimeout(() => {
this.socketTask.send({
data: 'ping'
})
}, 15000)
},
//清理心跳
heartbeatClear() {
clearTimeout(this.timeoutID)
},
//重置心跳
heartbeatReset() {
this.heartbeatClear()
this.heartbeatStart()
},
//注册用户
regUser() {
this.socketTask.send({
data: this.regData
})
},
//设置连接注册数据
setRegData(regData) {
this.regData = regData
},
//打开连接
open() {
if (this.regData === '') {
console.log('请先调用setRegData方法设置注册数据')
return
}
//防止重复连接
if (this.isOpening) {
console.log('WebSocket正在连接。。。')
return
}
if (this.isOpen && this.socketTask) {
console.log('WebSocket已连接。。。')
return
}
// console.log("WebSocket开始连接。。。");
this.isOpening = true
let socketTask = uni.connectSocket({
url: this.webSocketUrl,
header: {
'content-type': 'application/json'
},
success: e => {
// console.log(e);
},
complete: () => {}
})
if (!socketTask) return
// console.log(socketTask);
this.socketTask = socketTask
//监听开启
socketTask.onOpen(res => {
this.isOpen = true
this.isOpening = false
// console.log("WebSocket连接成功。。。");
this.regUser(this.regData)
})
//监听消息
socketTask.onMessage(res => {
if (res.data.replace(/^\s+|\s+$/g, '') === 'pong') {
this.heartbeatReset()
return
}
let data = JSON.parse(res.data)
this.message(data)
})
//监听关闭
socketTask.onClose(res => {
this.isOpen = false
this.isOpening = false
this.socketTask = false
// console.log("WebSocket连接关闭。。。");
uni.$emit('connClose', res)
this.heartbeatClear()
this.reOpen()
})
//监听错误
socketTask.onError(res => {
this.isOpen = false
this.isOpening = false
this.socketTask = false
console.log('WebSocket连接失败。。。')
this.heartbeatClear()
this.reOpen()
})
},
//消息处理
message(data) {
switch (data.cmd) {
case 'replyRegister':
if (data.data.success) {
console.log('注册成功!')
//注册成功开始心跳
this.heartbeatStart()
}
break
case 'replyControlMeasurePoint':
uni.$emit('controlMeasurePoint', data)
break
case 'replyControlDevice':
uni.$emit('controlDevice', data.data)
break
case 'deviceStatus':
uni.$emit('deviceStatus', data.data)
break
case 'deviceData':
uni.$emit('deviceData', data.data)
break
case 'sellerData':
uni.$emit('sellerData', data.data)
break
case 'buyerData':
uni.$emit('buyerData', data.data)
break
case 'goodsData':
uni.$emit('goodsData', data.data)
break
case 'logData':
uni.$emit('logData', data.data)
break
case 'measurePointStatus':
uni.$emit('measurePointStatus', data.data)
break
case 'replyMeasureData':
uni.$emit('replyMeasureData', data.data)
break
case 'replySaveMeasureData':
uni.$emit('replySaveMeasureData', data.data)
break
}
},
//重新打开
reOpen() {
this.timeoutID = setTimeout(() => {
this.open()
}, 5000)
},
//关闭连接
close() {
if (this.isOpen) {
this.socketTask.close()
}
},
//发送消息
send(data) {
console.log('发送消息。。。')
if (!this.isOpen) {
//关闭则打开
this.open()
}
if (this.socketTask) {
this.socketTask.send({
data: data
})
}
}
}

233
nx/helper/print.js Normal file
View File

@@ -0,0 +1,233 @@
import { getConAssayTaskWithReportTemplateContent, queryTaskDetailListByAssayTaskId } from '@/nx/api/auncel'
export default {
//打印机Map对象
printMap: new Map(),
//检测连接
checkConnect(printer) {
printer.intervalID = setInterval(() => {
let iv = new Date().getTime() - printer.lastReceiveMessageTime //多久没接收到消息
if (iv > 30000) {
//如果超过30秒未接收到服务器消息则关闭连接
console.log('已经' + iv + 'ms没接收到消息了服务器估计没在了')
clearInterval(printer.intervalID)
printer.socketTask.close()
}
}, 20000)
},
//开始心跳
heartbeatStart(printer) {
printer.timeoutID = setTimeout(() => {
let heartbeatData = {
cmd: 'heartbeat',
data: 'ping'
}
console.log(printer.printServerIp + ': 发送心跳。。。')
printer.socketTask.send({
data: JSON.stringify(heartbeatData)
})
}, 10000)
},
//清理心跳
heartbeatClear(printer) {
clearTimeout(printer.timeoutID)
},
//重置心跳
heartbeatReset(printer) {
this.heartbeatClear(printer)
this.heartbeatStart(printer)
},
//打开连接
open(printServerIp, printServerPort = 22333) {
//判断是否存在
let printer = this.printMap.get(printServerIp)
if (!printer) {
printer = {
isOpen: false,
isOpening: false, //打开中
socketTask: false,
timeoutID: -1,
intervalID: -1,
reConnectCount: 0, //重新连接次数
printServerIp: printServerIp,
printServerPort: printServerPort,
lastReceiveMessageTime: new Date().getTime()
}
}
//如果为正在打开,则返回
if (printer.isOpening) {
// console.log("已经正在打开中。。。");
return
}
//防止重复连接
if (printer.isOpen && printer.socketTask) {
console.log('websocket连接已打开。。。')
return
}
// console.log(printServerIp + ": WebSocket开始连接。。。");
printer.isOpening = true
let socketTask = uni.connectSocket({
url: 'ws://' + printServerIp + ':' + printServerPort + '/',
header: {
'content-type': 'application/json'
},
success: e => {
//连接成功是需要监听onOpen。这里的成功只是创建接口调用成功
//console.log(e);
},
complete: () => {}
})
if (!socketTask) return
// console.log(socketTask);
printer.socketTask = socketTask
this.printMap.set(printServerIp, printer)
//监听开启
socketTask.onOpen(res => {
printer.isOpen = true
printer.isOpening = false
printer.reConnectCount = 0
uni.$emit('printStatus', {})
console.log(socketTask)
console.log(printServerIp + ': WebSocket连接成功。。。')
this.heartbeatStart(printer)
this.checkConnect(printer) //检测连接是否正常
})
//监听消息
socketTask.onMessage(res => {
printer.lastReceiveMessageTime = new Date().getTime()
let data = JSON.parse(res.data)
this.message(printer, data)
})
//监听关闭
socketTask.onClose(res => {
printer.isOpen = false
printer.isOpening = false
printer.socketTask = false
uni.$emit('printStatus', {})
// console.log(printer.printServerIp + ": WebSocket连接关闭。。。");
this.heartbeatClear(printer)
this.printMap.delete(printServerIp)
if (printer.reConnectCount <= 100) {
printer.reConnectCount += 1
this.reOpen(printer.printServerIp, printer.printServerPort)
}
})
//监听错误
socketTask.onError(res => {
printer.isOpen = false
printer.isOpening = false
printer.socketTask = false
uni.$emit('printStatus', {})
// console.log(printServerIp + ": WebSocket连接失败。。。");
// console.log(res);
this.heartbeatClear(printer)
this.printMap.delete(printServerIp)
if (printer.reConnectCount <= 100) {
printer.reConnectCount += 1
this.reOpen(printer.printServerIp, printer.printServerPort)
}
})
},
//重新打开
reOpen(printServerIp, printServerPort) {
this.timeoutID = setTimeout(() => {
// console.log("开始重连。。。");
this.open(printServerIp, printServerPort)
}, 5000)
},
//消息处理
message(printer, data) {
console.log(data)
switch (data.cmd) {
case 'heartbeat':
this.heartbeatReset(printer)
break
}
// switch (data.type){
// case "ycbm":
// console.log("一次编密");
// break;
// case "ecbm":
// console.log("二次编密");
// break;
// case "jmsy":
// console.log("简码收样");
// break;
// case "heartbeat":
// console.log("接收到心跳包回复:" + data.sampleCode);
// this.heartbeatReset(printer);
// break;
// }
},
//关闭连接
close(printServerIp) {
let printer = this.printMap.get(printServerIp)
if (!printer) return //不存在直接返回
if (printer.isOpen) {
printer.socketTask.close()
printer.isOpen = false
}
},
//发送消息
send(printServerIp, data) {
let printer = this.printMap.get(printServerIp)
if (!printer) {
//不存在则打开
this.open(printServerIp)
}
if (printer.socketTask) {
printer.socketTask.send({
data: data
})
}
},
//获取打印模板并执行打印
getPrintTemplateAndPrint(currentTask) {
const id = currentTask.taskTypeId
getConAssayTaskWithReportTemplateContent(id)
.then(res => {
this.print(currentTask, res)
})
.catch(err => {
console.log(err)
})
},
//调用send方法执行打印
print(currentTask, conAssayTask) {
const me = this
let id = currentTask.id
const reportTemplate = conAssayTask.reportTemplateDataContent
me.getReportData(id, function (res) {
const data = res.data || []
const reportData = {
data: data
}
let printData = {
// type: "preview",
type: 'print',
reportTemplate: reportTemplate,
reportData: JSON.stringify(reportData)
}
//连接打印服务
let printList = uni.getStorageSync('KEY_PRINT_LIST')
if (printList && printList.length > 0) {
let print = printList[0]
let printServerIp = print.printIp
me.send(printServerIp, JSON.stringify(printData))
}
})
},
//获取打印模板
getReportData(id, onComplete) {
const param = {
taskId: id,
hideResultFlag: 'true'
}
queryTaskDetailListByAssayTaskId(param).then(res => {
onComplete(res)
})
}
}

285
nx/helper/test.js Normal file
View File

@@ -0,0 +1,285 @@
/**
* 验证电子邮箱格式
*/
function email(value) {
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value);
}
/**
* 验证手机格式
*/
function mobile(value) {
return /^1[23456789]\d{9}$/.test(value);
}
/**
* 验证URL格式
*/
function url(value) {
return /^((https|http|ftp|rtsp|mms):\/\/)(([0-9a-zA-Z_!~*'().&=+$%-]+: )?[0-9a-zA-Z_!~*'().&=+$%-]+@)?(([0-9]{1,3}.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z].[a-zA-Z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+\/?)$/.test(
value,
);
}
/**
* 验证日期格式
*/
function date(value) {
if (!value) return false;
// 判断是否数值或者字符串数值(意味着为时间戳)转为数值否则new Date无法识别字符串时间戳
if (number(value)) value = +value;
return !/Invalid|NaN/.test(new Date(value).toString());
}
/**
* 验证ISO类型的日期格式
*/
function dateISO(value) {
return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value);
}
/**
* 验证十进制数字
*/
function number(value) {
return /^[\+-]?(\d+\.?\d*|\.\d+|\d\.\d+e\+\d+)$/.test(value);
}
/**
* 验证字符串
*/
function string(value) {
return typeof value === 'string';
}
/**
* 验证整数
*/
function digits(value) {
return /^\d+$/.test(value);
}
/**
* 验证身份证号码
*/
function idCard(value) {
return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(value);
}
/**
* 是否车牌号
*/
function carNo(value) {
// 新能源车牌
const xreg =
/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
// 旧车牌
const creg =
/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
if (value.length === 7) {
return creg.test(value);
}
if (value.length === 8) {
return xreg.test(value);
}
return false;
}
/**
* 金额,只允许2位小数
*/
function amount(value) {
// 金额,只允许保留两位小数
return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value);
}
/**
* 中文
*/
function chinese(value) {
const reg = /^[\u4e00-\u9fa5]+$/gi;
return reg.test(value);
}
/**
* 只能输入字母
*/
function letter(value) {
return /^[a-zA-Z]*$/.test(value);
}
/**
* 只能是字母或者数字
*/
function enOrNum(value) {
// 英文或者数字
const reg = /^[0-9a-zA-Z]*$/g;
return reg.test(value);
}
/**
* 验证是否包含某个值
*/
function contains(value, param) {
return value.indexOf(param) >= 0;
}
/**
* 验证一个值范围[min, max]
*/
function range(value, param) {
return value >= param[0] && value <= param[1];
}
/**
* 验证一个长度范围[min, max]
*/
function rangeLength(value, param) {
return value.length >= param[0] && value.length <= param[1];
}
/**
* 是否固定电话
*/
function landline(value) {
const reg = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/;
return reg.test(value);
}
/**
* 判断是否为空
*/
function empty(value) {
switch (typeof value) {
case 'undefined':
return true;
case 'string':
if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
break;
case 'boolean':
if (!value) return true;
break;
case 'number':
if (value === 0 || isNaN(value)) return true;
break;
case 'object':
if (value === null || value.length === 0) return true;
for (const i in value) {
return false;
}
return true;
}
return false;
}
/**
* 是否json字符串
*/
function jsonString(value) {
if (typeof value === 'string') {
try {
const obj = JSON.parse(value);
if (typeof obj === 'object' && obj) {
return true;
}
return false;
} catch (e) {
return false;
}
}
return false;
}
/**
* 是否数组
*/
function array(value) {
if (typeof Array.isArray === 'function') {
return Array.isArray(value);
}
return Object.prototype.toString.call(value) === '[object Array]';
}
/**
* 是否对象
*/
function object(value) {
return Object.prototype.toString.call(value) === '[object Object]';
}
/**
* 是否短信验证码
*/
function code(value, len = 6) {
return new RegExp(`^\\d{${len}}$`).test(value);
}
/**
* 是否函数方法
* @param {Object} value
*/
function func(value) {
return typeof value === 'function';
}
/**
* 是否promise对象
* @param {Object} value
*/
function promise(value) {
return object(value) && func(value.then) && func(value.catch);
}
/** 是否图片格式
* @param {Object} value
*/
function image(value) {
const newValue = value.split('?')[0];
const IMAGE_REGEXP = /\.(jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg)/i;
return IMAGE_REGEXP.test(newValue);
}
/**
* 是否视频格式
* @param {Object} value
*/
function video(value) {
const VIDEO_REGEXP = /\.(mp4|mpg|mpeg|dat|asf|avi|rm|rmvb|mov|wmv|flv|mkv|m3u8)/i;
return VIDEO_REGEXP.test(value);
}
/**
* 是否为正则对象
* @param {Object}
* @return {Boolean}
*/
function regExp(o) {
return o && Object.prototype.toString.call(o) === '[object RegExp]';
}
export default {
email,
mobile,
url,
date,
dateISO,
number,
digits,
idCard,
carNo,
amount,
chinese,
letter,
enOrNum,
contains,
range,
rangeLength,
empty,
isEmpty: empty,
isNumber: number,
jsonString,
landline,
object,
array,
code,
};