publicFunction.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * 公共函数库
  3. */
  4. import CryptoJS from "crypto-js";
  5. const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF"); //十六位十六进制数作为密钥
  6. const iv = CryptoJS.enc.Utf8.parse("ABCDEF1234123412"); //十六位十六进制数作为密钥偏移量
  7. /* 加密 */
  8. const Encrypt = (word) => {
  9. let srcs = CryptoJS.enc.Utf8.parse(word);
  10. let encrypted = CryptoJS.AES.encrypt(srcs, key, {
  11. iv: iv,
  12. mode: CryptoJS.mode.CBC,
  13. padding: CryptoJS.pad.Pkcs7,
  14. });
  15. return encrypted.ciphertext.toString().toUpperCase();
  16. };
  17. /* 解密 - PRIVATE_KEY - 验证 */
  18. const Decrypt = (word) => {
  19. let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  20. let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  21. let decrypt = CryptoJS.AES.decrypt(srcs, key, {
  22. iv: iv,
  23. mode: CryptoJS.mode.CBC,
  24. padding: CryptoJS.pad.Pkcs7,
  25. });
  26. let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  27. return decryptedStr.toString();
  28. };
  29. //随机id生成
  30. const buildGuid = function (options) {
  31. var text = "";
  32. var mar = options || "default";
  33. var possible =
  34. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  35. for (var i = 0; i < 18; i++)
  36. text += possible.charAt(Math.floor(Math.random() * possible.length));
  37. return mar + "_" + new Date().getTime().toString() + text;
  38. };
  39. /**
  40. * 根据数组属性排序
  41. */
  42. const compare = function (property) {
  43. //property:根据什么属性排序
  44. return function (a, b) {
  45. var value1 = a[property];
  46. var value2 = b[property];
  47. /*
  48. * value2 - value1; ——> 降序
  49. * value1 - value2; ——> 升序
  50. */
  51. return value1 - value2; //升序排序
  52. };
  53. };
  54. /**
  55. * 获取随机颜色
  56. */
  57. const getRandomColor = function () {
  58. let rgb = [];
  59. for (let i = 0; i < 3; ++i) {
  60. let color = Math.floor(Math.random() * 256).toString(16);
  61. color = color.length == 1 ? "0" + color : color;
  62. rgb.push(color);
  63. }
  64. return "#" + rgb.join("");
  65. };
  66. /**
  67. * 生成随机色
  68. * rootNumber 色彩基数 默认200
  69. * colorsNumber 跳跃基数 默认16
  70. * forLength 避免死循环(限制迭代次数)
  71. */
  72. const getRGBColor = (rootNumber, colorsNumber) => {
  73. // if (forLength < 10) {
  74. // forLength++;
  75. // 色彩基数
  76. let _rootNumber = 0;
  77. // 跳跃基数
  78. let _colorsNumber = 50;
  79. // rgb格式定义
  80. let rgbColor = [0, 150, 0];
  81. let rgbColorStr = "rgb(";
  82. if (rootNumber) {
  83. _rootNumber = rootNumber;
  84. }
  85. if (colorsNumber) {
  86. _colorsNumber = colorsNumber;
  87. }
  88. rgbColor.forEach((item, index) => {
  89. if (index === 0) {
  90. rgbColorStr +=
  91. _rootNumber + item +
  92. Math.round((Math.random() * (255 - _rootNumber - item)) / _colorsNumber) *
  93. _colorsNumber;
  94. } else {
  95. let rgbColor =
  96. _rootNumber + item +
  97. Math.round((Math.random() * (255 - _rootNumber - item)) / _colorsNumber) *
  98. _colorsNumber;
  99. rgbColorStr += "," + rgbColor;
  100. }
  101. });
  102. rgbColorStr += ")";
  103. return rgbColorStr;
  104. // 去重
  105. // if (this.rgbColorList.indexOf(rgbColorStr) >= 0) {
  106. // this.getRGBColor(rootNumber, colorsNumber, forLength);
  107. // } else {
  108. // this.rgbColorList.push(rgbColorStr);
  109. // }
  110. // } else {
  111. // this.$message.info("getColors 请重试!");
  112. // }
  113. };
  114. /**
  115. * 获取geoJSON格式的数据 -- 目前仅供标记疑点相关内容使用(圆也存为面);
  116. *
  117. * 类型: 0 点;1 线;2 面;3 矩形;4 圆形;5 其他;
  118. */
  119. const generateGeoJSON = (title, desc, featureType, coords, geoName) => {
  120. let dataType = "LineString";
  121. let propertiesType = "面";
  122. switch (featureType) {
  123. case 0:
  124. dataType = "Point";
  125. propertiesType = "点";
  126. break;
  127. case 1:
  128. dataType = "LineString";
  129. propertiesType = "线";
  130. break;
  131. case 2:
  132. dataType = "Polygon";
  133. propertiesType = "面";
  134. break;
  135. case 4:
  136. dataType = "Polygon";
  137. propertiesType = "圆";
  138. }
  139. let data = {
  140. type: "Feature",
  141. properties: {
  142. title: title || "",
  143. desc: desc || "",
  144. featureType: propertiesType,
  145. featureTypeIndex: featureType,
  146. name: geoName,
  147. },
  148. geometry: {
  149. coordinates: coords,
  150. // coordinates: [[
  151. // [114.1199232448792, 35.33694447387694],
  152. // [113.75754748638985, 31.716871012257627],
  153. // [109.28122464218609, 32.06198896315057],
  154. // [109.70940919390267, 34.543087977545525],
  155. // ]],
  156. type: dataType,
  157. },
  158. };
  159. return data;
  160. };
  161. /**
  162. * 供Leaflet直接读取Geojson数据的标准数据格式;
  163. * @coordinates 坐标,格式要求 [[ [lng1, lat1],[lng2, lat2]]]
  164. * @type 用于何种应用场景,如 overlayAnalysis
  165. *
  166. * 应用于叠置分析
  167. */
  168. const standardGeojson = (coordinates, properties) => {
  169. let data = {
  170. type: "Feature",
  171. properties: properties,
  172. geometry: {
  173. type: "Polygon",
  174. coordinates: coordinates,
  175. },
  176. };
  177. return data;
  178. };
  179. /**
  180. * 转化为树结构
  181. * @param {*} params
  182. * @param {*} arr
  183. * @param {*} depth
  184. * @returns
  185. */
  186. let convertTree = (params, arr, depth = 0) => {
  187. if (arr.length == 0 || depth >= params.length) return arr;
  188. let res = [];
  189. let v = params[depth];
  190. let map = group(arr, v.field);
  191. map.forEach((val, key) => {
  192. // 生成当前层的节点
  193. let o = v.obj(val[0]);
  194. // 生成所有的子节点
  195. let child = val.map((cv) => v.child(cv));
  196. o.child = child;
  197. // 递归生成
  198. if (child.length > 0) {
  199. o.child = convertTree(params, child, depth + 1);
  200. if (depth + 1 === params.length) {
  201. delete o.child;
  202. }
  203. }
  204. res.push(o);
  205. });
  206. return res;
  207. };
  208. /**
  209. * 根据字段分类
  210. * @param {*} arr
  211. * @param {*} field
  212. * @returns
  213. */
  214. let group = (arr, field) => {
  215. let map = new Map();
  216. arr.forEach((v) => {
  217. // 没有的话就创建一个新数组
  218. if (!map.has(v[field])) {
  219. map.set(v[field], [v]);
  220. return;
  221. }
  222. // 放入同类中
  223. map.get(v[field]).push(v);
  224. });
  225. return map;
  226. };
  227. /**
  228. * 多维数组偏移调整
  229. */
  230. let latLngsCorrection = (latlngsAry)=>{
  231. // 深拷贝
  232. var tempLatlngsAry = JSON.parse(JSON.stringify(latlngsAry));
  233. if (typeof tempLatlngsAry[0] === "number") {
  234. if (tempLatlngsAry.length < 3 && tempLatlngsAry.length > 0) {
  235. // systemConfig.lonCorrectParams -- 经度偏移校正值
  236. tempLatlngsAry[0]-= systemConfig.lonCorrectParams
  237. tempLatlngsAry[1]-=systemConfig.latCorrectParams
  238. return tempLatlngsAry;
  239. }
  240. if (tempLatlngsAry.length === 3) {
  241. tempLatlngsAry[0]-= systemConfig.lonCorrectParams
  242. tempLatlngsAry[1]-=systemConfig.latCorrectParams
  243. return tempLatlngsAry;
  244. }
  245. } else {
  246. for (var i = 0, l = tempLatlngsAry.length; i < l; i++) {
  247. tempLatlngsAry[i] = latLngsCorrection(tempLatlngsAry[i]);
  248. }
  249. }
  250. return tempLatlngsAry;
  251. }
  252. /**
  253. * 多维数组转置
  254. */
  255. let latLngsToReverse = (latlngsAry) => {
  256. // 深拷贝
  257. var tempLatlngsAry = JSON.parse(JSON.stringify(latlngsAry));
  258. if (typeof tempLatlngsAry[0] === "number") {
  259. if (tempLatlngsAry.length < 3 && tempLatlngsAry.length > 0) {
  260. return tempLatlngsAry.reverse();
  261. }
  262. if (tempLatlngsAry.length === 3) {
  263. tempLatlngsAry = tempLatlngsAry.slice(0, 2);
  264. return tempLatlngsAry.reverse();
  265. }
  266. } else {
  267. for (var i = 0, l = tempLatlngsAry.length; i < l; i++) {
  268. tempLatlngsAry[i] = latLngsToReverse(tempLatlngsAry[i]);
  269. }
  270. }
  271. return tempLatlngsAry;
  272. };
  273. export default {
  274. buildGuid,
  275. compare,
  276. getRandomColor,
  277. Encrypt,
  278. Decrypt,
  279. generateGeoJSON,
  280. standardGeojson,
  281. convertTree,
  282. latLngsCorrection,
  283. latLngsToReverse,
  284. getRGBColor,
  285. };