index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import productDits from "./productDicts";
  2. export const home = 'main';
  3. export function resetProperties(srcObj: any, destObj?: Object){
  4. // 先设置为undefined
  5. for (const key in srcObj) {
  6. // console.log('key', key)
  7. if (srcObj.hasOwnProperty(key)) {
  8. srcObj[key] = undefined;
  9. }
  10. }
  11. // 再更新为目标对象
  12. if(destObj){
  13. Object.assign(srcObj, destObj);
  14. }
  15. };
  16. export {productDits};
  17. /**
  18. *
  19. * @param timestamp 时间戳转时间格式
  20. * @param type
  21. * @returns
  22. */
  23. export function timestampToTime(timestamp: number, type?: string) {
  24. if(type == 's'){
  25. timestamp = timestamp * 1000;
  26. }
  27. let date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  28. let Y = date.getFullYear() + '-';
  29. let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  30. let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  31. let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours())+ ':';
  32. let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  33. let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  34. return Y+M+D+h+m+s;
  35. }
  36. // 树
  37. function addNode(dest: any, node: any){
  38. // 添加路径
  39. node.key = node.id
  40. if(node.path instanceof Array){
  41. node.path.push(dest.id)
  42. }else{
  43. node.path = [dest.id]
  44. }
  45. if(dest.id != undefined && node.pid != undefined && dest.id == node.pid){
  46. if (dest.children) dest.children.push(node)
  47. else dest.children = [node]
  48. return dest;
  49. }else if(dest.children instanceof Array){
  50. for(let i = 0; i < dest.children.length; i++){
  51. if(addNode(dest.children[i], node)){
  52. return dest
  53. }
  54. }
  55. }
  56. delete node.path;
  57. return undefined;
  58. }
  59. function addNodeMutual(node1: any, node2: any){
  60. const item = addNode(node1, node2);
  61. if(item){
  62. return item;
  63. }else{
  64. return addNode(node2, node1);
  65. }
  66. }
  67. function addListNode(list: any[], node: any){
  68. for(let i = 0; i < list.length; i++){
  69. const item = addNodeMutual(list[i], node)
  70. if(item){
  71. list[i] = item;
  72. return list
  73. }
  74. }
  75. return undefined;
  76. }
  77. export function tree(list: any[]){
  78. const data: any[] = [];
  79. for(let i = 0; i < list.length; i++){
  80. if(addListNode(data, list[i]) == undefined){
  81. data.push(list[i])
  82. }
  83. }
  84. return data
  85. }
  86. // modal可拖拽
  87. export function draggable(aClass: string){
  88. let modal:any = document.getElementsByClassName(aClass)[0];
  89. console.log(modal)
  90. let header:any = modal.querySelector('.ant-modal-header');
  91. let mask:any = modal.parentNode
  92. header.addEventListener('mouseover', () => {
  93. header.style.cursor = 'move';
  94. })
  95. header.addEventListener('mousedown', function(e:any){
  96. let x = e.pageX - modal.offsetLeft;
  97. let y = e.pageY - modal.offsetTop;
  98. document.addEventListener('mousemove', move);
  99. function move(e:any){
  100. modal.style.position = 'absolute'
  101. modal.style.top = (e.pageY - y) + 'px'
  102. modal.style.left = (e.pageX - x) + 'px'
  103. // 左侧
  104. if(modal.offsetLeft < 0){
  105. modal.style.left = '0px'
  106. }else if(modal.offsetLeft > (mask.clientWidth - modal.clientWidth - 10)){
  107. modal.style.left = (mask.clientWidth - modal.clientWidth ) + 'px'
  108. }
  109. // 上
  110. if(modal.offsetTop < 0){
  111. modal.style.top = '0px'
  112. }else if(modal.offsetTop > (mask.clientHeight - modal.clientHeight)){
  113. modal.style.top = (mask.clientHeight - modal.clientHeight) + 'px'
  114. }
  115. // console.log(e)
  116. // console.log('%O', mask)
  117. // console.log('%O', modal)
  118. // console.log(modal.offsetTop)
  119. // console.log(modal.offsetLeft)
  120. }
  121. header.addEventListener('mouseup', function(){
  122. document.removeEventListener('mousemove', move)
  123. })
  124. })
  125. }