123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- /**
- * 动态加载图片
- * **/
- export const getAssetsURI = (uri: string): string => {
- return new URL(uri, import.meta.url).href;
- }
- /**
- * 获取URL字符串GET传参参数
- * **/
- export const getUrlParams: (url: Required<string>)=>Map<string, string> | null = (url) => {
- const params: string = url.split("?")[1];
- if (params) {
- const arr = params.split("&");
- const map: Map<string, string> = new Map();
- arr.forEach((item) => {
- const arr2 = item.split("=");
- map.set(arr2[0], arr2[1]);
- });
- return map;
- }
- return null;
- }
- /***
- * JSON转formData
- */
- export const jsonToFormData = (json: any): FormData | null => {
- const keys: string[] = Object.keys(json)
- const formData = new FormData();
- if (keys.length > 0) {
- keys.forEach((key) => {
- formData.append(key, (json[key] instanceof Object) ? JSON.stringify(json[key]) : json[key]);
- });
- }
- return formData;
- }
- /**
- * 补零函数:将小于10的数补零并返回字符串
- * @param num number
- * @return string
- */
- const zeroFillToString = (num: number): string => {
- return num < 10? '0' + num : num.toString();
- }
- /**
- * 格式化日期
- */
- export const formatDate = (date: Date) => {
- if(!(date instanceof Date)) throw new Error('date不是Date类型')
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate();
- return `${year}-${zeroFillToString(month)}-${zeroFillToString(day)}`;
- }
- /**
- * 格式化时间
- */
- export const formatDateTime = (date: Date) => {
- if(!(date instanceof Date)) throw new Error('date不是Date类型')
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate();
- const hours = date.getHours();
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return `${year}-${zeroFillToString(month)}-${zeroFillToString(day)} ${zeroFillToString(hours)}:${zeroFillToString(minute)}:${zeroFillToString(second)}`;
- }
- /**
- * 格式化时间
- */
- export const formatDateTimeByTime = (time: number) => {
- const date = new Date();
- date.setTime(time);
- return formatDateTime(date);
- }
- /**
- * 格式化获取localStore object类型数据
- * @param lKey:string localStore key 必填
- * @param key?:string obj key 可选
- * */
- export const getStoreObject = (lKey: string, key?: string) => {
- if (!lKey) throw new Error('key不能为空!');
- const result: string | null = localStorage.getItem(lKey);
- if (!result) return null;
- const obj = JSON.parse(result);
- if (!key) return obj;
- return obj[key];
- }
- export const setStoreObject = (lKey: string, data: any) => {
- if (!lKey) throw new Error('key不能为空!');
- if (!data) return;
- if (typeof (data) === 'object') {
- localStorage.setItem(lKey, JSON.stringify(data));
- return;
- }
- if (typeof (data) === 'string') {
- localStorage.setItem(lKey, data);
- return;
- }
- }
|