maintain.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. export const tools = {
  2. //根据key值去对象中取对应的值
  3. dataForKey: function (data: any, key: any) {
  4. let arr = key.split(".")
  5. if (arr && arr[0]) {
  6. for (let i = 0; i < arr.length; i++) {
  7. if (arr[i] && data[arr[i]]) {
  8. data = data[arr[i]]
  9. } else {
  10. return ''
  11. }
  12. }
  13. return data
  14. }
  15. },
  16. //根据key值去设置对象的值
  17. setDataForKey: function (data: any, key: any, value: any) {
  18. let arr = key.split(".")
  19. if (arr && arr[0]) {
  20. for (let i = 0; i < arr.length; i++) {
  21. if (!(arr[i] && data[arr[i]])) {
  22. data[arr[i]] = {}
  23. }
  24. if (value !== undefined) {
  25. if (i == arr.length - 1) {
  26. data[arr[i]] = value
  27. }
  28. }
  29. data = data[arr[i]]
  30. }
  31. }
  32. },
  33. deepCopy: function (obj: any): any {
  34. const out = [],
  35. len = obj.length;
  36. for (let i = 0; i < len; i++) {
  37. if (obj[i] instanceof Array) {
  38. out[i] = tools.deepCopy(obj[i]);
  39. } else out[i] = obj[i];
  40. }
  41. return out;
  42. },
  43. formatDate: (now: any) => {
  44. let year = now.getFullYear();
  45. let month = now.getMonth() + 1;
  46. let date = now.getDate();
  47. let hour = now.getHours();
  48. let minute = now.getMinutes();
  49. let second = now.getSeconds();
  50. return year + "-" + month + "-" + (date > 10 ? date : '0' + date) + " " + hour + ":" + (minute > 10 ? minute : '0' + minute) + ":" + (second > 10 ? second : '0' + second);
  51. },
  52. /**
  53. * @param arr [a,b,c]
  54. *
  55. * val value
  56. */
  57. returnNewObj: (data: any, arr: any, val: any) => {
  58. if (arr.length > 1) {
  59. let k = arr.shift()
  60. if (!data[k]) {
  61. data[k] = {}
  62. }
  63. tools.returnNewObj(data[k], arr, val)
  64. } else {
  65. let curk = arr[0]
  66. data[curk] = val;
  67. }
  68. },
  69. /**
  70. * @param data Object
  71. * @returns 将对象中 key 为a.b.c 转换为a:{b:{c:''}}
  72. */
  73. formatData: (data: any) => {
  74. let newData = JSON.parse(JSON.stringify(data))
  75. for (let key in newData) {
  76. if (key.indexOf('.') > -1) {
  77. let arr = key.split('.')
  78. tools.returnNewObj(newData, arr, newData[key])
  79. delete newData[key]
  80. }
  81. }
  82. return newData
  83. },
  84. /**
  85. * 解析模型文件名称信息
  86. * @param name 模型文件名称
  87. * @returns 模型文件名包含的楼层信息
  88. */
  89. getMoldeFileInfo: (name: string) => {
  90. const regList = name.match(/(-[FB]\d+M?[.-])|(-RF[.-])/g);
  91. const floorInfo: any = {}
  92. if (regList && regList.length === 1) {
  93. const floorName = regList[0].slice(1, -1);
  94. floorInfo.floorName = floorName;
  95. if (floorName === 'RF') { //顶层
  96. floorInfo.floorSequenceId = 9999;
  97. return floorInfo;
  98. } else { //非顶层
  99. const strList = floorName.split("");
  100. if (strList[0] && strList[0] === 'F') { //地上
  101. if (strList[strList.length - 1] === 'M') { //有夹层
  102. floorInfo.floorSequenceId = Number(floorName.slice(1, -1)) * 10 + 5;
  103. } else { //无夹层
  104. floorInfo.floorSequenceId = Number(floorName.slice(1)) * 10;
  105. }
  106. return floorInfo;
  107. } else if (strList[0] && strList[0] === 'B') { //地下
  108. if (strList[strList.length - 1] === 'M') { //有夹层
  109. floorInfo.floorSequenceId = -(Number(floorName.slice(1, -1)) * 10 + 5);
  110. } else { //无夹层
  111. floorInfo.floorSequenceId = -(Number(floorName.slice(1)) * 10);
  112. }
  113. return floorInfo;
  114. } else { //其他
  115. return false;
  116. }
  117. }
  118. } else {
  119. return false;
  120. }
  121. }
  122. }
  123. export default tools