common.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * 动态加载图片
  3. * **/
  4. export const getAssetsURI = (uri: string): string => {
  5. return new URL(uri, import.meta.url).href;
  6. }
  7. /**
  8. * 获取URL字符串GET传参参数
  9. * **/
  10. export const getUrlParams: (url: Required<string>)=>Map<string, string> | null = (url) => {
  11. const params: string = url.split("?")[1];
  12. if (params) {
  13. const arr = params.split("&");
  14. const map: Map<string, string> = new Map();
  15. arr.forEach((item) => {
  16. const arr2 = item.split("=");
  17. map.set(arr2[0], arr2[1]);
  18. });
  19. return map;
  20. }
  21. return null;
  22. }
  23. /***
  24. * JSON转formData
  25. */
  26. export const jsonToFormData = (json: any): FormData | null => {
  27. const keys: string[] = Object.keys(json)
  28. const formData = new FormData();
  29. if (keys.length > 0) {
  30. keys.forEach((key) => {
  31. formData.append(key, (json[key] instanceof Object) ? JSON.stringify(json[key]) : json[key]);
  32. });
  33. }
  34. return formData;
  35. }
  36. /**
  37. * 补零函数:将小于10的数补零并返回字符串
  38. * @param num number
  39. * @return string
  40. */
  41. const zeroFillToString = (num: number): string => {
  42. return num < 10? '0' + num : num.toString();
  43. }
  44. /**
  45. * 格式化日期
  46. */
  47. export const formatDate = (date: Date) => {
  48. if(!(date instanceof Date)) throw new Error('date不是Date类型')
  49. const year = date.getFullYear()
  50. const month = date.getMonth() + 1
  51. const day = date.getDate();
  52. return `${year}-${zeroFillToString(month)}-${zeroFillToString(day)}`;
  53. }
  54. /**
  55. * 格式化时间
  56. */
  57. export const formatDateTime = (date: Date) => {
  58. if(!(date instanceof Date)) throw new Error('date不是Date类型')
  59. const year = date.getFullYear()
  60. const month = date.getMonth() + 1
  61. const day = date.getDate();
  62. const hours = date.getHours();
  63. const minute = date.getMinutes()
  64. const second = date.getSeconds()
  65. return `${year}-${zeroFillToString(month)}-${zeroFillToString(day)} ${zeroFillToString(hours)}:${zeroFillToString(minute)}:${zeroFillToString(second)}`;
  66. }
  67. /**
  68. * 格式化时间
  69. */
  70. export const formatDateTimeByTime = (time: number) => {
  71. const date = new Date();
  72. date.setTime(time);
  73. return formatDateTime(date);
  74. }
  75. /**
  76. * 格式化获取localStore object类型数据
  77. * @param lKey:string localStore key 必填
  78. * @param key?:string obj key 可选
  79. * */
  80. export const getStoreObject = (lKey: string, key?: string) => {
  81. if (!lKey) throw new Error('key不能为空!');
  82. const result: string | null = localStorage.getItem(lKey);
  83. if (!result) return null;
  84. const obj = JSON.parse(result);
  85. if (!key) return obj;
  86. return obj[key];
  87. }
  88. export const setStoreObject = (lKey: string, data: any) => {
  89. if (!lKey) throw new Error('key不能为空!');
  90. if (!data) return;
  91. if (typeof (data) === 'object') {
  92. localStorage.setItem(lKey, JSON.stringify(data));
  93. return;
  94. }
  95. if (typeof (data) === 'string') {
  96. localStorage.setItem(lKey, data);
  97. return;
  98. }
  99. }