tools.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. import Handsontable from "handsontable-pro"
  2. const tools = {}
  3. let arrX = []
  4. let arrY = []
  5. tools.getPointDetails = (arr) => {
  6. arr.map(item => {
  7. if (item.PointList && item.PointList.length) {
  8. item.PointList.map(em => {
  9. arrX.push(em.X)
  10. arrY.push(em.Y)
  11. })
  12. }
  13. })
  14. }
  15. /**
  16. *
  17. * @param {*} obj 将point标签的x,y进行比对
  18. * @return object 返回数组,其中包括最大值最小值X,Y
  19. */
  20. tools.getPoint = function (obj) {
  21. arrX = []
  22. arrY = []
  23. for (let i in obj) {
  24. tools.getPointDetails(obj[i])
  25. }
  26. let maxX = Math.max.apply(null, arrX)
  27. let minX = Math.min.apply(null, arrX)
  28. let maxY = Math.max.apply(null, arrY)
  29. let minY = Math.min.apply(null, arrY)
  30. let data = {
  31. maxX,
  32. minX,
  33. maxY,
  34. minY
  35. }
  36. return data
  37. }
  38. /**
  39. *
  40. * @param {*} array 比较数组1
  41. * @param {*} array2 比较数组2
  42. *
  43. * 返回两个数组中不同的元素
  44. */
  45. tools.compareArr = function (array, array2) {
  46. var arr3 = [];
  47. for (key in array) {
  48. var stra = array[key];
  49. var count = 0;
  50. for (var j = 0; j < array2.length; j++) {
  51. var strb = array2[j];
  52. if (stra == strb) {
  53. count++;
  54. }
  55. }
  56. if (count === 0) { //表示数组1的这个值没有重复的,放到arr3列表中
  57. arr3.push(stra);
  58. }
  59. }
  60. return arr3;
  61. }
  62. /**
  63. *
  64. * @param {*} arr 需要改变的数组
  65. * @param {*} k 其中Y值需要变换的倍数
  66. * @param {*} name arr中的key值
  67. *
  68. * 返回arr map 后arr[mapIndex]name * k 的数组
  69. */
  70. tools.changeMap = function (arr, k, name) {
  71. let data = arr.map(items => {
  72. if (items[name] && items[name].length) {
  73. items[name].map(children => {
  74. if (Array.isArray(children)) {
  75. return children.map(res => {
  76. res.Y = res.Y * k
  77. return res
  78. })
  79. } else {
  80. children.Y = children.Y * k
  81. return children
  82. }
  83. })
  84. }
  85. })
  86. return data
  87. }
  88. tools.getMouseCanvas = function (view, e) {
  89. let bbox = view.canvasView.getBoundingClientRect();
  90. let x = e.clientX - bbox.left,
  91. y = e.clientY - bbox.top;
  92. return view.mapToScene({
  93. x: x,
  94. y: y
  95. });
  96. }
  97. /**
  98. *
  99. * @param {*} view graphy class
  100. * @param {*} e mouse元素e
  101. */
  102. tools.mouseInElement = function (view, e, type) {
  103. let mouse = tools.getMouseCanvas(view, e),
  104. falg = false,
  105. items = view.scene.root.children,
  106. i = 0,
  107. t = type || 3;
  108. for (; i < items.length; i++) {
  109. if (items[i].type == t) {
  110. if (items[i].contains(mouse)) {
  111. falg = true
  112. break
  113. }
  114. }
  115. }
  116. return {
  117. falg,
  118. item: items[i] || [],
  119. index: i
  120. }
  121. }
  122. /**
  123. *
  124. * @param {graphy中的所有children} items
  125. * @param {类型} type
  126. * @param {list} codes
  127. * @param {item中的code} code
  128. */
  129. tools.clear = function (items, type, codes, code) {
  130. let indexArr = [];
  131. if (items && items.length) {
  132. let i = 0;
  133. for (i; i < items.length; i++) {
  134. let j = 0;
  135. for (j; j < codes.length; j++) {
  136. if (items[i][type] = type && items[i][code] == codes[j][code]) {
  137. indexArr.push(i)
  138. }
  139. }
  140. }
  141. }
  142. return indexArr
  143. }
  144. tools.getText = function (arr, name) {
  145. let text = '';
  146. if (arr && arr.length) {
  147. arr.map(item => {
  148. text += (!!text ? '、' : '') + item[name]
  149. })
  150. }
  151. return text
  152. }
  153. tools.cutString = function (string, num, text) {
  154. return string.substring(0, num) + '...' + text
  155. }
  156. tools.deepCopy = function (obj) {
  157. var out = [],
  158. i = 0,
  159. len = obj.length;
  160. for (; i < len; i++) {
  161. if (obj[i] instanceof Array) {
  162. out[i] = tools.deepCopy(obj[i]);
  163. } else out[i] = obj[i];
  164. }
  165. return out;
  166. }
  167. tools.copyArr = (source) => {
  168. if (!source || typeof source !== 'object') {
  169. throw new Error('error arguments', 'shallowClone');
  170. }
  171. var targetObj = source.constructor === Array ? [] : {};
  172. for (var keys in source) {
  173. if (source.hasOwnProperty(keys)) {
  174. if (source[keys] && typeof source[keys] === 'object') {
  175. targetObj[keys] = source[keys].constructor === Array ? [] : {};
  176. targetObj[keys] = tools.copyArr(source[keys]);
  177. } else {
  178. targetObj[keys] = source[keys];
  179. }
  180. }
  181. }
  182. return targetObj;
  183. }
  184. tools.deepCopyObj = function (v) {
  185. var o = v.constructor === Array ? [] : {};
  186. for (var key in v) {
  187. o[key] = typeof v[key] === 'Object' ? tools.deepCopyObj(v[key]) : v[key];
  188. }
  189. return o;
  190. }
  191. tools.deepClone = (obj) => {
  192. var proto = Object.getPrototypeOf(obj);
  193. return Object.assign({}, Object.create(proto), obj);
  194. }
  195. //hansontable插件的修改
  196. tools.customDropdownRenderer = function (
  197. instance,
  198. td,
  199. row,
  200. col,
  201. prop,
  202. value,
  203. cellProperties
  204. ) {
  205. var selectedId;
  206. var optionsList = cellProperties.chosenOptions.data;
  207. if (
  208. typeof optionsList === "undefined" ||
  209. typeof optionsList.length === "undefined" ||
  210. !optionsList.length
  211. ) {
  212. Handsontable.renderers.TextRenderer(
  213. instance,
  214. td,
  215. row,
  216. col,
  217. prop,
  218. value,
  219. cellProperties
  220. );
  221. return td;
  222. }
  223. var values = (value + "").split(",");
  224. value = [];
  225. for (var index = 0; index < optionsList.length; index++) {
  226. if (optionsList[index].content && optionsList[index].content.length) {
  227. for (let i = 0; i < optionsList[index].content.length; i++) {
  228. if (
  229. optionsList[index].content[i] &&
  230. optionsList[index].content[i].length
  231. ) {
  232. for (let j = 0; j < optionsList[index].content[i].length; j++) {
  233. if (
  234. values.indexOf(
  235. optionsList[index].content[i].content[j].Code + ""
  236. ) > -1
  237. ) {
  238. selectedId = optionsList[index].content[i].content[j].Code;
  239. value.push(optionsList[index].content[i].content[j].Name);
  240. }
  241. }
  242. } else {
  243. if (
  244. values.indexOf(optionsList[index].content[i].Code + "") > -1
  245. ) {
  246. selectedId = optionsList[index].content[i].Code;
  247. value.push(optionsList[index].content[i].Name);
  248. }
  249. }
  250. }
  251. } else {
  252. if (values.indexOf(optionsList[index].Code + "") > -1) {
  253. selectedId = optionsList[index].Code;
  254. value.push(optionsList[index].Name);
  255. }
  256. }
  257. }
  258. value = value.join(", ");
  259. Handsontable.renderers.TextRenderer(
  260. instance,
  261. td,
  262. row,
  263. col,
  264. prop,
  265. value,
  266. cellProperties
  267. );
  268. return td;
  269. }
  270. //hansontable插件查看详情
  271. tools.lookDetails = function (instance, td, row, col, prop, value, cellProperties) {
  272. td.style.color = "#409EFF";
  273. td.style.cursor = "pointer";
  274. if (!!value) {
  275. if (typeof (value) == 'object') {
  276. td.innerHTML = "已有值,查看";
  277. } else {
  278. td.innerHTML = value
  279. }
  280. } else {
  281. td.innerHTML = "查看详情";
  282. }
  283. return td;
  284. }
  285. //是否关联
  286. tools.hasRelation = function (instance, td, row, col, prop, value, cellProperties) {
  287. if (value) {
  288. td.innerHTML = "已关联";
  289. } else {
  290. td.innerHTML = "未关联";
  291. }
  292. return td;
  293. }
  294. //关联的资产
  295. tools.LinkEquipLocalName = function (instance, td, row, col, prop, value, cellProperties) {
  296. if (value) {
  297. td.innerHTML = value == 'null'?'':value;
  298. } else {
  299. let html = instance.getDataAtRowProp(row, 'linkEquipName');
  300. td.innerHTML = html == 'null'?'':html;
  301. }
  302. return td;
  303. }
  304. tools.num = function (instance, td, row, col, prop, value, cellProperties) {
  305. td.style.color = "#409EFF";
  306. td.style.cursor = "pointer";
  307. td.innerHTML = value;
  308. return td;
  309. }
  310. tools.setItem = (key, value) => {
  311. if (typeof value == 'string') {
  312. localStorage.setItem(key, value);
  313. } else {
  314. localStorage.setItem(key, JSON.stringify(value));
  315. }
  316. }
  317. tools.getItem = (key) => {
  318. const re = /^\[|\{|\}|\]$/g //判断字符中是否有[]{}
  319. let getIt = localStorage.getItem(key)
  320. if (re.test(getIt)) {
  321. return JSON.parse(getIt)
  322. } else {
  323. return getIt
  324. }
  325. }
  326. tools.removeItem = (key) => {
  327. localStorage.removeItem(key)
  328. }
  329. //取两个数组的差值
  330. tools.differenceArr = (a, b) => {
  331. return a.concat(b).filter(v => !a.includes(v) || !b.includes(v))
  332. }
  333. //取两个数组的相同值
  334. tools.sameArr = (a, b) => {
  335. return a.filter(v => b.includes(v))
  336. }
  337. tools.sortArr = (arr, key, sequence) => {
  338. function compare(property) {
  339. return function (a, b) {
  340. var value1 = a[property];
  341. var value2 = b[property];
  342. if (sequence) {
  343. return value1 - value2;
  344. } else {
  345. return value2 - value1
  346. }
  347. }
  348. }
  349. return arr.sort(compare(key))
  350. }
  351. tools.pagination = (pageNo, pageSize, array) => {
  352. let offset = (pageNo - 1) * pageSize;
  353. return offset + pageSize >= array.length ?
  354. array.slice(offset, array.length) :
  355. array.slice(offset, offset + pageSize);
  356. }
  357. tools.getSameItems = (arr1, key1, arr2, key2) => {
  358. let data = []
  359. arr1.map(item => {
  360. arr2.map(child => {
  361. if (item[key1] == child[key2]) {
  362. data.push(item)
  363. }
  364. })
  365. })
  366. return data
  367. }
  368. tools.isIn = (x, y, json) => {
  369. let nCross = 0,
  370. point = typeof (x) == 'object' ? [x.x, x.y] : [x, y],
  371. APoints = json,
  372. length = APoints.length,
  373. p1, p2, i, xinters;
  374. p1 = APoints[0];
  375. for (i = 1; i <= length; i++) {
  376. p2 = APoints[i % length];
  377. if (
  378. point[0] > Math.min(p1[0], p2[0]) &&
  379. point[0] <= Math.max(p1[0], p2[0])
  380. ) {
  381. if (point[1] <= Math.max(p1[1], p2[1])) {
  382. if (p1[0] != p2[0]) {
  383. //计算位置信息
  384. xinters = (point[0] - p1[0]) * (p2[1] - p1[1]) / (p2[0] - p1[0]) + p1[1];
  385. if (p1[1] == p2[1] || point[1] <= xinters) {
  386. nCross++
  387. }
  388. }
  389. }
  390. }
  391. p1 = p2;
  392. }
  393. if (nCross % 2 == 0) {
  394. return false
  395. } else {
  396. return true
  397. }
  398. }
  399. tools.arrayUnique = (arr, name) => {
  400. var hash = {};
  401. return arr.reduce(function (item, next) {
  402. hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
  403. return item;
  404. }, []);
  405. }
  406. tools.dateAddYear = function (date, yearCount) {
  407. var tempDate = dateToDate(date);
  408. var count = parseInt(yearCount);
  409. var oldYear = tempDate.getFullYear();
  410. var oldMonth = tempDate.getMonth();
  411. var oldDate = tempDate.getDate();
  412. var newYear = oldYear + count;
  413. var newDate = new Date(newYear, oldMonth, oldDate);
  414. //防止月份数不一致,进行微调
  415. while (newDate.getMonth() != oldMonth) {
  416. oldDate--;
  417. newDate = new Date(newYear, oldMonth, oldDate);
  418. }
  419. var month = newDate.getMonth() + 1;
  420. var day = newDate.getDate();
  421. month = (month.toString().length == 1) ? ("0" + month) : month;
  422. day = (day.toString().length == 1) ? ("0" + day) : day;
  423. var result = newDate.getFullYear() + '-' + month + '-' + day; //当前日期
  424. return result;
  425. }
  426. tools.formatDate = (now) => {
  427. let year = now.getFullYear();
  428. let month = now.getMonth() + 1;
  429. let date = now.getDate();
  430. let hour = now.getHours();
  431. let minute = now.getMinutes();
  432. let second = now.getSeconds();
  433. return year + "-" + month + "-" + (date > 10 ? date : '0' + date) + " " + hour + ":" + (minute > 10 ? minute : '0' + minute) + ":" + (second > 10 ? second : '0' + second);
  434. };
  435. tools.formatDataSource = function (data) {
  436. let options
  437. let arr = []
  438. function recursion(data) {
  439. let arr = []
  440. data.map((item) => {
  441. if (item.code && item.name)
  442. arr.push({Code: item.code, Name: item.name})
  443. if (item.content && item.content.length)
  444. arr = arr.concat(recursion(item.content))
  445. })
  446. return arr
  447. }
  448. try {
  449. if (data instanceof Array) {
  450. options = data
  451. } else {
  452. options = JSON.parse(data)
  453. }
  454. if (options) {
  455. return recursion(options)
  456. } else {
  457. return arr
  458. }
  459. } catch (err) {
  460. console.log(err);
  461. return arr
  462. }
  463. }
  464. tools.isObjectValueEqual = function (a, b) {//判断两个对象的可枚举属性值是否相等(认为null,"",undefinde相等)
  465. //取对象a和b的属性名
  466. var aProps = Object.keys(a)
  467. var bProps = Object.keys(b)
  468. //判断属性名的length是否一致
  469. if (aProps.length != bProps.length) {
  470. return false
  471. }
  472. //循环取出属性名,再判断属性值是否一致
  473. for (var i = 0; i < aProps.length; i++) {
  474. var propName = aProps[i]
  475. if (!(a[propName] instanceof Object || b[propName] instanceof Object)) {
  476. if (!(a[propName] == b[propName] || (!a[propName] && !b[propName]))) {
  477. return false
  478. }
  479. } else {
  480. if (!tools.isObjectValueEqual(a[propName], b[propName])) {
  481. return false
  482. }
  483. }
  484. }
  485. return true
  486. }
  487. tools.dataForKey = function (data, key) {
  488. let arr = key.split(".")
  489. if (arr && arr[0]) {
  490. for (let i = 0; i < arr.length; i++) {
  491. if (arr[i] && data[arr[i]]) {
  492. data = data[arr[i]]
  493. } else {
  494. return ''
  495. }
  496. }
  497. return data
  498. }
  499. }
  500. tools.setDataForKey = function (data, key, value) {
  501. let arr = key.split(".")
  502. if (arr && arr[0]) {
  503. for (let i = 0; i < arr.length; i++) {
  504. if (!(arr[i] && data[arr[i]])) {
  505. data[arr[i]] = {}
  506. }
  507. if (value !== undefined) {
  508. if (i == arr.length - 1) {
  509. data[arr[i]] = value
  510. }
  511. } else {
  512. if (i == arr.length - 1) {
  513. data[arr[i]] = ''
  514. }
  515. }
  516. data = data[arr[i]]
  517. }
  518. }
  519. }
  520. tools.flattenKeys = function (obj) {
  521. if(obj == undefined) return
  522. let res = {}
  523. function isObject(val) {
  524. return typeof val === 'object' && !Array.isArray(val)
  525. }
  526. function digKeys(prev, obj) {
  527. Object.entries(obj).forEach(([key, value]) => {
  528. const currentKey = prev ? `${prev}.${key}` : key
  529. if (isObject(value)) {
  530. digKeys(currentKey, value)
  531. } else {
  532. res[currentKey] = value
  533. }
  534. })
  535. }
  536. digKeys('', obj)
  537. return res
  538. }
  539. function dateToDate(date) {
  540. var sDate = new Date();
  541. if (typeof date == 'object' && typeof new Date().getMonth == "function") {
  542. sDate = date;
  543. } else if (typeof date == "string") {
  544. var arr = date.split('-');
  545. if (arr.length == 3) {
  546. sDate = new Date(arr[0] + '-' + arr[1] + '-' + arr[2]);
  547. }
  548. }
  549. return sDate;
  550. }
  551. export default tools