/* * @Author: 常坤 c_kunx@163.com * @Date: 2022-11-01 11:56:52 * @LastEditors: kennthKun c_kunx@163.com * @LastEditTime: 2022-11-04 11:41:11 * @FilePath: /kx-ms-ts/src/utils/index.ts * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ import moment from 'moment'; import { DEFAULT_ID, SYSYTYPE } from './const'; import { getCookie, getLocaleStorage } from './cookie'; import { getSessionStorage } from './cookie'; // 手机号脱敏 export const changePhoneType = (value: string) => { if (value) { const tells = value; const tellPatern = /(\d{3})\d*(\d{4})/; const phone = tells.replace(tellPatern, '$1****$2'); return phone; } else { return ''; } }; export function getHeaders() { const token = getCookie('AILIEYUN_ACCESS_TOKEN'); const ROLE_DATA = getLocaleStorage('ROLE_DATA') || '{}'; return { 'Content-Type': 'application/json', Authorization: `${token}`, dept_id: JSON.parse(ROLE_DATA)?.deptId || '', role_id: JSON.parse(ROLE_DATA)?.roleId || '', app_id: getLocaleStorage('APPID') || DEFAULT_ID?.APPID, tenant_id: getLocaleStorage('TENANTID') || DEFAULT_ID?.TENANT_ID, Domain: window.location.hostname, }; } export function getItemProps(schems: object, item: object) { const data = {} const itemKeys = Object.keys(item) const schemsKeys = Object.keys(schems) for (const s of schemsKeys) { if (item[s] === undefined) { data[s] = schems[s] } } for (const i of itemKeys) { if (schems[i] === undefined) { data[i] = item[i] } if (item[i] !== undefined && typeof item[i] !== 'object') { data[i] = item[i] } if (typeof schems[i] === 'object') { if (schems[i] instanceof Array) { data[i] = [...schems[i], ...item[i]] } else { data[i] = { ...schems[i], ...item[i] } } } } return data } let CssName: string = ''; export const deafultCss = (name: string) => { CssName = name; }; export const getPrefixCls = ( suffixCls?: string, customizePrefixCls?: string, ): string => { if (customizePrefixCls) return customizePrefixCls; return CssName ? suffixCls ? `${CssName}-${suffixCls}` : `${CssName}` : suffixCls ? `ant-${suffixCls}` : `ant`; }; export interface moneyProps { num: string | number | null | undefined; defaultSymbol?: string; } // table 处理 金额 export const moneyHandle = (num: number | string, type?: string) => { let newNum = ''; if (num || num === 0) { if (typeof num === 'string') { newNum = num; } else { newNum += num; } } if (newNum.indexOf('.') === -1) { newNum += '.00'; } if (newNum.length - newNum.indexOf('.') === 2) { newNum += '0'; } if (newNum.length - newNum.indexOf('.') > 3) { newNum = newNum.slice(0, newNum.indexOf('.') + 3); } const numFoot = newNum.split('.'); const numHead = toThousands(numFoot[0]); return `${type || ''}${numHead}.${numFoot[1]}`; }; // 每3位加, function toThousands(num: string) { let result = ''; let counter = 0; for (let i = num.length - 1; i >= 0; i -= 1) { counter += 1; result = num.charAt(i) + result; if (!(counter % 3) && i !== 0) { result = `,${result}`; } } return result; } export const dateDiff = (time: any) => { function add0(m: any) { return m < 10 ? `0${m}` : m; } let ntime = `${time}`; // eslint-disable-next-line no-underscore-dangle if (time._isAMomentObject) { ntime = `${moment(time).unix()}` } // 10位 13位时间戳 if (ntime.indexOf('-') === -1) { const ctime = new Date( ntime.length === 10 ? Number(ntime) * 1000 : Number(ntime), ); const y = ctime.getFullYear(); const m = ctime.getMonth() + 1; const d = ctime.getDate(); const h = ctime.getHours(); const mm = ctime.getMinutes(); const s = ctime.getSeconds(); ntime = `${y}-${add0(m)}-${add0(d)} ${add0(h)}:${add0(mm)}:${add0(s)}`; } // 日期中含有T if (ntime.includes('T')) { ntime = ntime.replace('T', ' '); } // 直接算好的 return ntime.split(' '); }; export const getButtonRole = (auth: any) => { if (!auth || getLocaleStorage('SYSTYPE') === SYSYTYPE.CUSTOMER) { return true } let bool = false; const permission = JSON.parse(getSessionStorage('BUTTON_ROLE') || '[]'); const keysValue = permission?.map((item: any) => { return item['code']; }); if (keysValue.includes(auth)) { bool = true; } return bool; } export const ChangeImgUrl = (url:string) => { return url.indexOf('http') === 0 ? url : BASE_IMG + url }