index.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import dayjs from 'dayjs';
  2. import dataDictionary from './dataDictionary';
  3. let utils = {
  4. /**
  5. * 参数处理
  6. */
  7. tansParams: (params: any) => {
  8. let result = '';
  9. for (const propName of Object.keys(params)) {
  10. const value = params[propName];
  11. const part = encodeURIComponent(propName) + '=';
  12. if (value !== null && value !== '' && typeof value !== 'undefined') {
  13. if (typeof value === 'object') {
  14. for (const key of Object.keys(value)) {
  15. if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
  16. const params = propName + '[' + key + ']';
  17. const subPart = encodeURIComponent(params) + '=';
  18. result += subPart + encodeURIComponent(value[key]) + '&';
  19. }
  20. }
  21. } else {
  22. result += part + encodeURIComponent(value) + '&';
  23. }
  24. }
  25. }
  26. return result;
  27. },
  28. /**
  29. * 参数处理
  30. */
  31. getProject: (data: any, type: number) => {
  32. //格式化数据
  33. let list = [];
  34. for (let key in data) {
  35. // let arrList = key.split('@@');
  36. let newArr = key.split('_');
  37. newArr.push(data[key]);
  38. list.push(newArr);
  39. }
  40. // console.log("list",list)
  41. //重组数组
  42. let newList = [];
  43. let project: any = dataDictionary.project;
  44. for (let key in project) {
  45. let area: any = [];
  46. list.forEach((item) => {
  47. if (key == item[0]) {
  48. let arrList = item[1].split('@@');
  49. area.push({
  50. key: item[1],
  51. name: arrList[0],
  52. value: item[2],
  53. });
  54. }
  55. });
  56. let obj: any = {
  57. key: key,
  58. name: project[key],
  59. area: area
  60. };
  61. newList.push(obj);
  62. }
  63. return newList;
  64. },
  65. /**
  66. * 秒转为时间
  67. */
  68. timeFormat: (val: number) => {
  69. let h = parseInt(((val / 60 / 60) % 24).toString());
  70. let m = parseInt(((val / 60) % 60).toString());
  71. let s = parseInt((val % 60).toString());
  72. if (h > 0) {
  73. m = h * 60 + m;
  74. }
  75. return `${m}:${s}`;
  76. },
  77. // 时间戳转为日期
  78. timestampFormat: (data: any, format: any) => {
  79. let formatData = format || 'YYYY-MM-DD HH:mm:ss';
  80. if(data){
  81. if(data.toString().length == 10){
  82. return dayjs(data * 1000).format(formatData)
  83. }else{
  84. return dayjs(data).format(formatData)
  85. }
  86. }else{
  87. return '';
  88. }
  89. },
  90. /**
  91. * 跑步成绩格式化
  92. */
  93. runTime: (times: any, noMin: boolean = false, type: boolean, lang: any = 0) => {
  94. if (times != undefined && times.toString().indexOf('.') != -1) {
  95. times = Math.round((Number(times) + Number.EPSILON) * 100) / 100;
  96. }
  97. if (type) {
  98. if (times <= 0) {
  99. return '0分0秒';
  100. }
  101. // let isMinus = false
  102. if (isNaN && isNaN(times)) {
  103. times = 0;
  104. }
  105. if (times === -1) {
  106. return '-';
  107. }
  108. if (times < 0) {
  109. times = Math.abs(times);
  110. // isMinus = true
  111. }
  112. if (noMin) {
  113. // dayjs SS格式化后SS保持原样, 所以直接用SSS格式化后.slice(0, -1)去掉最后一位
  114. return dayjs(+times * 1000)
  115. .format('ss″SSS')
  116. .slice(0, -1);
  117. }
  118. //console.log(times, "runTimerunTime");
  119. if (times >= 3600) {
  120. return (
  121. Math.floor(times / 3600) +
  122. '' +
  123. dayjs(+times * 1000)
  124. .format('mm:ss.SSS')
  125. .slice(0, -1)
  126. );
  127. } else if (times >= 60) {
  128. return dayjs(+times * 1000)
  129. .format('m分s秒 ')
  130. .slice(0, -1);
  131. } else {
  132. return dayjs(+times * 1000)
  133. .format('m分s秒 ')
  134. .slice(0, -1);
  135. }
  136. } else {
  137. if (times <= 0) {
  138. if (noMin) {
  139. return '00″00';
  140. } else {
  141. return '00′00″00';
  142. }
  143. }
  144. // let isMinus = false
  145. if (isNaN && isNaN(times)) {
  146. times = 0;
  147. }
  148. if (times === -1) {
  149. return '-';
  150. }
  151. if (times < 0) {
  152. times = Math.abs(times);
  153. // isMinus = true
  154. }
  155. if (noMin) {
  156. // dayjs SS格式化后SS保持原样, 所以直接用SSS格式化后.slice(0, -1)去掉最后一位
  157. return dayjs(+times * 1000)
  158. .format('ss″SSS')
  159. .slice(0, -1);
  160. }
  161. //console.log(times, "runTimerunTime");
  162. if (times >= 3600) {
  163. return (
  164. Math.floor(times / 3600) +
  165. '' +
  166. dayjs(+times * 1000)
  167. .format('mm:ss.SSS')
  168. .slice(0, -1)
  169. );
  170. } else if (times >= 60) {
  171. return dayjs(+times * 1000)
  172. .format('mm′ss″SSS')
  173. .slice(0, -1);
  174. } else {
  175. if (lang == 1) {
  176. return dayjs(+times * 1000)
  177. .format('ss秒SSS')
  178. .slice(0, -1);
  179. } else {
  180. return dayjs(+times * 1000)
  181. .format('ss″SSS')
  182. .slice(0, -1);
  183. }
  184. }
  185. }
  186. },
  187. // 当前时间
  188. getDate: () => {
  189. let now = new Date();
  190. let year = now.getFullYear();
  191. let month = now.getMonth();
  192. let day = now.getDate();
  193. let hours = now.getHours();
  194. let minutes = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
  195. let seconds = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();
  196. month = month + 1;
  197. //let week;
  198. //let arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
  199. //week = arr_week[now.getDay()];
  200. let time = year + '-' + month + '-' + day + ' ' + ' ' + hours + ':' + minutes + ':' + seconds;
  201. return time;
  202. },
  203. // 全屏
  204. fullScreen: () => {
  205. if (document.fullscreenElement) {
  206. document.exitFullscreen();
  207. }
  208. document.body.requestFullscreen();
  209. },
  210. // 设置收藏夹图标
  211. setFavicon: (url?:any) => {
  212. let dom = document.querySelectorAll('link');
  213. for (let i = 0; i < dom.length; i++) {
  214. if (dom[i].rel === 'icon') {
  215. dom[i].href = url || ''
  216. }
  217. }
  218. }
  219. };
  220. export default utils;