123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- /**
- * 工具类
- */
- import Config from "./config";
- const LOG_SHOW = Config.get('bluetoothConsole') //是否开启蓝牙调试
- /**
- * 对象转URL
- * @param {object} obj
- */
- export const urlEncode = (obj = {}) => {
- const result = []
- for (const key in obj) {
- const item = obj[key]
- if (!item) {
- continue
- }
- if (isArray(item)) {
- item.forEach(val => {
- result.push(key + '=' + val)
- })
- } else {
- result.push(key + '=' + item)
- }
- }
- return result.join('&')
- }
- /**
- * 遍历对象
- */
- export const objForEach = (obj, callback) => {
- Object.keys(obj).forEach((key) => {
- callback(obj[key], key)
- });
- }
- /**
- * 是否在数组内
- */
- export const inArray = (search, array) => {
- for (var i in array) {
- if (array[i] == search) return true
- }
- return false
- }
- /**
- * 判断是否为空对象
- * @param {*} object 源对象
- */
- export const isEmptyObject = (object) => {
- return Object.keys(object).length === 0
- }
- /**
- * 判断是否为对象
- * @param {*} object
- */
- export const isObject = (object) => {
- return Object.prototype.toString.call(object) === '[object Object]'
- }
- /**
- * 判断是否为数组
- * @param {*} array
- */
- export const isArray = (array) => {
- return Object.prototype.toString.call(array) === '[object Array]'
- }
- /**
- * 判断是否为空
- * @param {*} object 源对象
- */
- export const isEmpty = (value) => {
- if (isArray(value)) {
- return value.length === 0
- }
- if (isObject(value)) {
- return isEmptyObject(value)
- }
- return !value
- }
- /**
- * 对象深拷贝
- * @param {*} obj 源对象
- */
- export const cloneObj = (obj) => {
- let newObj = obj.constructor === Array ? [] : {};
- if (typeof obj !== 'object') {
- return;
- }
- for (let i in obj) {
- newObj[i] = typeof obj[i] === 'object' ? cloneObj(obj[i]) : obj[i];
- }
- return newObj
- }
- /**
- * 数组交集
- * @param {Array} 数组1
- * @param {Array} 数组2
- * @return {Array}
- */
- export const arrayIntersect = (array1, array2) => {
- return array1.filter(val => array2.indexOf(val) > -1)
- }
- export const arrayFindData = (data, value, keyName, itemName = null, label = 1) => {
- let info = [];
- let itemIsArray = false
- if (isEmpty(data)) return info
- if (data && isArray(data[0][keyName])) itemIsArray = true
- let valueIsArray = false
- if (isArray(value)) valueIsArray = true
- data.some((item, index) => {
- if (
- (!itemIsArray && !valueIsArray && value == item[keyName]) ||
- (itemIsArray && !valueIsArray && inArray(value, item[keyName])) ||
- (!itemIsArray && valueIsArray && inArray(item[keyName], value))
- ) {
- info.push({
- index: index,
- value: item
- })
- return info;
- } else if (label - 1 > 0 && item[itemName] instanceof Array) {
- let aaa = arrayFindData(item[itemName], value, keyName, itemName, label - 1)
- if (aaa.length > 0) {
- info.push({
- index: index,
- value: item
- })
- info.push(...aaa);
- return info;
- }
- }
- });
- return info;
- }
- export const array_column = (arr = [], column, indexKey = null) => {
- let data = {};
- if (column && indexKey) {
- arr.forEach((item) => {
- data[item[indexKey]] = item[column]
- })
- } else if (column && !indexKey) {
- data = []
- arr.forEach((item) => {
- data.push(item[column])
- })
- } else {
- arr.forEach((item) => {
- data[item[indexKey]] = item
- })
- }
- return data
- }
- export const secondsConvert = (seconds) => {
- if (!seconds) return '00:00'
- return formatZero(parseInt(seconds / 60), 2) + ':' + formatZero(seconds % 60, 2)
- }
- export const msConvert = (seconds) => {
- if (!seconds) return "00′00"
- return formatZero(parseInt(seconds / 1000), 2) + "′" + formatZero(seconds % 1000, 3)
- }
- /**
- * 数字不够x位,前面补0
- * @param {*} num 数字
- * @param {*} len 补零长度
- * @returns 0001
- */
- export function formatZero(num, len) {
- if (String(num).length > len) {
- return num;
- }
- return (Array(len).join(0) + num).slice(-len)
- }
- /**
- * 字符串按指定长度切割
- * @param str
- * @param long
- * @returns {*[]}
- */
- export function sliceChunk(str, long = 2) {
- let arr = [];
- for (let i = 0; i < str.length;) {
- arr.push(str.slice(i, i + long))
- i += long
- }
- return arr
- }
- export const sleep = (interval) => {
- return new Promise((resolve) => {
- setTimeout(resolve, interval);
- }
- )
- }
- /**
- * 对微信接口的promise封装
- * @param {function} fn
- * @param {object} args
- */
- export const promisify = (fn, args) => {
- return new Promise((resolve, reject) => {
- fn({
- ...(args || {}),
- success: (res) => resolve(res),
- fail: (err) => reject(err),
- });
- });
- }
- export const logs = (...arr) => {
- LOG_SHOW && console.info('调试打印', ...arr)
- }
- /**
- * 格式化数字
- * @param value
- * @param detault
- * @param maxLenght
- * @param decimalLength
- * @returns {number|string}
- */
- export const formatterConvert = (value, detault = '', maxLenght = 5, decimalLength = 0, isMinus = false) => {
- if (value[0] == '-') {
- value = value.slice(1)
- }
- value = value.toString()
- let result = value.split(".")
- if (value.charAt(value.length - 1) === '.' && decimalLength > 0) {
- let num = Math.abs(result[0])
- return num + '.' || detault
- }
- if (result.length > 2) result = result.slice(0, 2)
- if (result[0].length > maxLenght) {
- result[0] = result[0].slice(0, maxLenght)
- }
- if (result[0].length > 0) result[0] = Math.abs(result[0])
- if (decimalLength > 0) {
- if (result[1]) result[1] = result[1].slice(0, decimalLength)
- } else {
- if (result[1]) result = result.slice(1, 1)
- }
- let res = result.join('.') || detault
- return res
- }
- /**
- * 获取结果值在数组内容
- * @param data
- * @param value
- * @param keyName
- * @param itemName
- * @param label
- * @returns {Promise<*>}
- */
- export const findArrayInfo = async (data, value, keyName, itemName = null, label = 1) => {
- let info = await arrayFindData(data, value, keyName, itemName, label)
- return Promise.resolve(info);
- }
- /**
- * 获取结果值在数组中的下标
- * @param arr
- * @param val
- * @param itemName
- */
- export const findArrayIndex = async (arr, val, itemName) => {
- if (!arr.length) return -1;
- let resultIndex = arr.findIndex(function (value, index, arr) {
- let values = itemName ? value[itemName] : value;
- return values == val;
- })
- return resultIndex
- }
- export const array_chunk = (arr, size) => {
- let newArr = []
- for (let i = 0, len = arr.length; i < len; i += size) {
- newArr.push(arr.slice(i, i + size));
- }
- return newArr
- }
- export const arraySearchAndSort = async (list, keyword = '', keyName = 'name') => {
- list.forEach((item) => {
- item.arraySearchName = item[keyName]
- item.arraySearchKey = 0
- if (!keyword) return true
- let reg = item[keyName].match(RegExp(`${keyword}`));
- item.arraySearchKey = reg ? reg.length : 0
- if (item.arraySearchKey > 0) {
- item.arraySearchName = item.arraySearchName.replace(new RegExp(`(${keyword})`, 'g'), `${keyword}`)
- }
- })
- return list
- }
- export const strToObject = (str) => {
- let arr = str.split(";");
- let data = {}
- arr.forEach((item) => {
- if (!item) return true
- let itemArr = item.split(':')
- data[itemArr[0]] = itemArr[1]
- })
- return data
- }
- export const objectToStr = (arr) => {
- let data = []
- Object.keys(arr).forEach((item) => {
- data.push(`${item}:${arr[item]}`)
- })
- return data.join(';') + ';'
- }
- export const countdownTimer = (callback, duration = 5) => {
- let timer = setInterval(function () {
- duration--;
- if (duration < 0) {
- clearInterval(timer);
- }
- }, 1000);
- }
- /**
- *
- * */
- export const pxTorpx = (px) => {
- let deviceWidth = uni.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
- let rpx = (750 / deviceWidth) * Number(px)
- return Math.floor(rpx);
- }
|