/** * 公共函数库 */ import CryptoJS from "crypto-js"; const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF"); //十六位十六进制数作为密钥 const iv = CryptoJS.enc.Utf8.parse("ABCDEF1234123412"); //十六位十六进制数作为密钥偏移量 /* 加密 */ const Encrypt = (word) => { let srcs = CryptoJS.enc.Utf8.parse(word); let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, }); return encrypted.ciphertext.toString().toUpperCase(); }; /* 解密 - PRIVATE_KEY - 验证 */ const Decrypt = (word) => { let encryptedHexStr = CryptoJS.enc.Hex.parse(word); let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, }); let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); }; //随机id生成 const buildGuid = function (options) { var text = ""; var mar = options || "default"; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 18; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return mar + "_" + new Date().getTime().toString() + text; }; /** * 根据数组属性排序 */ const compare = function (property) { //property:根据什么属性排序 return function (a, b) { var value1 = a[property]; var value2 = b[property]; /* * value2 - value1; ——> 降序 * value1 - value2; ——> 升序 */ return value1 - value2; //升序排序 }; }; /** * 获取随机颜色 */ const getRandomColor = function () { let rgb = []; for (let i = 0; i < 3; ++i) { let color = Math.floor(Math.random() * 256).toString(16); color = color.length == 1 ? "0" + color : color; rgb.push(color); } return "#" + rgb.join(""); }; /** * 生成随机色 * rootNumber 色彩基数 默认200 * colorsNumber 跳跃基数 默认16 * forLength 避免死循环(限制迭代次数) */ const getRGBColor = (rootNumber, colorsNumber) => { // if (forLength < 10) { // forLength++; // 色彩基数 let _rootNumber = 0; // 跳跃基数 let _colorsNumber = 50; // rgb格式定义 let rgbColor = [0, 150, 0]; let rgbColorStr = "rgb("; if (rootNumber) { _rootNumber = rootNumber; } if (colorsNumber) { _colorsNumber = colorsNumber; } rgbColor.forEach((item, index) => { if (index === 0) { rgbColorStr += _rootNumber + item + Math.round((Math.random() * (255 - _rootNumber - item)) / _colorsNumber) * _colorsNumber; } else { let rgbColor = _rootNumber + item + Math.round((Math.random() * (255 - _rootNumber - item)) / _colorsNumber) * _colorsNumber; rgbColorStr += "," + rgbColor; } }); rgbColorStr += ")"; return rgbColorStr; // 去重 // if (this.rgbColorList.indexOf(rgbColorStr) >= 0) { // this.getRGBColor(rootNumber, colorsNumber, forLength); // } else { // this.rgbColorList.push(rgbColorStr); // } // } else { // this.$message.info("getColors 请重试!"); // } }; /** * 获取geoJSON格式的数据 -- 目前仅供标记疑点相关内容使用(圆也存为面); * * 类型: 0 点;1 线;2 面;3 矩形;4 圆形;5 其他; */ const generateGeoJSON = (title, desc, featureType, coords, geoName) => { let dataType = "LineString"; let propertiesType = "面"; switch (featureType) { case 0: dataType = "Point"; propertiesType = "点"; break; case 1: dataType = "LineString"; propertiesType = "线"; break; case 2: dataType = "Polygon"; propertiesType = "面"; break; case 4: dataType = "Polygon"; propertiesType = "圆"; } let data = { type: "Feature", properties: { title: title || "", desc: desc || "", featureType: propertiesType, featureTypeIndex: featureType, name: geoName, }, geometry: { coordinates: coords, // coordinates: [[ // [114.1199232448792, 35.33694447387694], // [113.75754748638985, 31.716871012257627], // [109.28122464218609, 32.06198896315057], // [109.70940919390267, 34.543087977545525], // ]], type: dataType, }, }; return data; }; /** * 供Leaflet直接读取Geojson数据的标准数据格式; * @coordinates 坐标,格式要求 [[ [lng1, lat1],[lng2, lat2]]] * @type 用于何种应用场景,如 overlayAnalysis * * 应用于叠置分析 */ const standardGeojson = (coordinates, properties) => { let data = { type: "Feature", properties: properties, geometry: { type: "Polygon", coordinates: coordinates, }, }; return data; }; /** * 转化为树结构 * @param {*} params * @param {*} arr * @param {*} depth * @returns */ let convertTree = (params, arr, depth = 0) => { if (arr.length == 0 || depth >= params.length) return arr; let res = []; let v = params[depth]; let map = group(arr, v.field); map.forEach((val, key) => { // 生成当前层的节点 let o = v.obj(val[0]); // 生成所有的子节点 let child = val.map((cv) => v.child(cv)); o.child = child; // 递归生成 if (child.length > 0) { o.child = convertTree(params, child, depth + 1); if (depth + 1 === params.length) { delete o.child; } } res.push(o); }); return res; }; /** * 根据字段分类 * @param {*} arr * @param {*} field * @returns */ let group = (arr, field) => { let map = new Map(); arr.forEach((v) => { // 没有的话就创建一个新数组 if (!map.has(v[field])) { map.set(v[field], [v]); return; } // 放入同类中 map.get(v[field]).push(v); }); return map; }; /** * 多维数组偏移调整 */ let latLngsCorrection = (latlngsAry)=>{ // 深拷贝 var tempLatlngsAry = JSON.parse(JSON.stringify(latlngsAry)); if (typeof tempLatlngsAry[0] === "number") { if (tempLatlngsAry.length < 3 && tempLatlngsAry.length > 0) { // systemConfig.lonCorrectParams -- 经度偏移校正值 tempLatlngsAry[0]-= systemConfig.lonCorrectParams tempLatlngsAry[1]-=systemConfig.latCorrectParams return tempLatlngsAry; } if (tempLatlngsAry.length === 3) { tempLatlngsAry[0]-= systemConfig.lonCorrectParams tempLatlngsAry[1]-=systemConfig.latCorrectParams return tempLatlngsAry; } } else { for (var i = 0, l = tempLatlngsAry.length; i < l; i++) { tempLatlngsAry[i] = latLngsCorrection(tempLatlngsAry[i]); } } return tempLatlngsAry; } /** * 多维数组转置 */ let latLngsToReverse = (latlngsAry) => { // 深拷贝 var tempLatlngsAry = JSON.parse(JSON.stringify(latlngsAry)); if (typeof tempLatlngsAry[0] === "number") { if (tempLatlngsAry.length < 3 && tempLatlngsAry.length > 0) { return tempLatlngsAry.reverse(); } if (tempLatlngsAry.length === 3) { tempLatlngsAry = tempLatlngsAry.slice(0, 2); return tempLatlngsAry.reverse(); } } else { for (var i = 0, l = tempLatlngsAry.length; i < l; i++) { tempLatlngsAry[i] = latLngsToReverse(tempLatlngsAry[i]); } } return tempLatlngsAry; }; export default { buildGuid, compare, getRandomColor, Encrypt, Decrypt, generateGeoJSON, standardGeojson, convertTree, latLngsCorrection, latLngsToReverse, getRGBColor, };