import Cookies from 'js-cookie'; import { getCurrentTime } from '@/api/system'; import { getTheCurrentUserName } from '@/api/config'; const UJCMS_LOCALE = 'ujcms-locale'; const UJCMS_SITE_ID = 'ujcms-site-id'; export const getCookieLocale = (): string | undefined => Cookies.get(UJCMS_LOCALE); export const setCookieLocale = (local: string): void => { Cookies.set(UJCMS_LOCALE, local); }; export const getSessionSiteId = (): number | null => { const siteId = sessionStorage.getItem(UJCMS_SITE_ID); if (siteId != null) { return Number(siteId); } return null; }; export const setSessionSiteId = (siteId: number): void => { sessionStorage.setItem(UJCMS_SITE_ID, String(siteId)); }; export const getSiteHeaders = (): any => { const siteId = getSessionSiteId(); return siteId != null ? { [UJCMS_SITE_ID]: siteId } : {}; }; export const pageSizes = [10, 20, 50, 100, 200, 400, 800]; export const pageLayout = 'total, sizes, prev, pager, next, jumper'; export const toParams = (params: Record): any => { const obj = { ...params }; Object.keys(obj).forEach((key) => { if (obj[key] instanceof Array) { const [first] = obj[key]; if (first instanceof Date) { key.split(',').forEach((item, index) => { obj[item] = obj[key][index]; }); delete obj[key]; return; } obj[key] = obj[key].join(','); } }); return obj; }; export const resetParams = (params: Record): void => { Object.keys(params).forEach((key) => { // eslint-disable-next-line delete params[key]; }); }; /** * 移动树形结构的列表,同一父节点下的子节点才能一起移动排序。 */ export const moveTreeList = (selected: any[], data: any[], type: 'top' | 'up' | 'down' | 'bottom'): any[] => { const { parentId } = selected[0]; const ids = selected .filter((item) => item.parentId === parentId) .sort((a, b) => a.order - b.order) .map((item) => item.id); const list = data.filter((item) => item.parentId === parentId).map((item) => item.id); const top = 0; const bottom = list.length; let up = list.indexOf(ids[0]); if (up <= top) { up = top + 1; } let down = list.indexOf(ids[ids.length - 1]); if (down >= bottom) { down = bottom - 1; } for (let i = 0, len = ids.length; i < len; i += 1) { const index = list.indexOf(ids[i]); const [deleted] = list.splice(index, 1); if (type === 'top') { list.splice(top + i, 0, deleted); } else if (type === 'bottom') { list.splice(bottom, 0, deleted); } else if (type === 'up') { list.splice(up - 1 + i, 0, deleted); } else if (type === 'down') { list.splice(down + 1, 0, deleted); } } return list; }; /** * 移动普通列表 */ export const moveList = (selected: any[], list: any[], type: 'top' | 'up' | 'down' | 'bottom'): any[] => { selected.sort((a, b) => a.order - b.order); const top = 0; const bottom = list.length; let up = list.indexOf(selected[0]); if (up <= top) up = top + 1; let down = list.indexOf(selected[selected.length - 1]); if (down >= bottom) down = bottom - 1; for (let i = 0, len = selected.length; i < len; i += 1) { const index = list.indexOf(selected[i]); const [deleted] = list.splice(index, 1); if (type === 'top') { list.splice(top + i, 0, deleted); } else if (type === 'bottom') { list.splice(bottom, 0, deleted); } else if (type === 'up') { list.splice(up - 1 + i, 0, deleted); } else if (type === 'down') { list.splice(down + 1, 0, deleted); } } return list; }; /** * 返回添加记录所需lcJudgmentRuleReq */ export const handleId = ( answerId: number | string, emptyOne: number | string, emptyTwo: number | string, operationIds: number | string, type: number | string, ): Record => { return { answerId, emptyOne, emptyTwo, operationIds, type, }; }; /** * 阿拉伯数字转化为中文数字 */ export const numToChinese = (num: number): string => { const arr1 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; const arr2 = ['', '十', '百', '千', '万', '亿', '点', '']; const a = `${num}`.replace(/(^0*)/g, '').split('.'); let k = 0; let re = ''; for (let i = a[0].length - 1; i >= 0; i--) { switch (k) { case 0: re = arr2[7] + re; break; case 4: if (!new RegExp(`0{4}//d{${a[0].length - i - 1}}$`).test(a[0])) re = arr2[4] + re; break; case 8: re = arr2[5] + re; arr2[7] = arr2[5]; k = 0; break; default: } if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = arr1[0] + re; if (a[0].charAt(i) != 0) re = arr1[a[0].charAt(i)] + arr2[k % 4] + re; k++; } return num > 9 && num < 20 ? re.slice(1) : re; }; /** * 传入文件名和数据,下载文件 */ export const downloadFileDirect = (fileName: string, data: any) => { if ('download' in document.createElement('a')) { const elink = document.createElement('a'); elink.download = fileName; elink.style.display = 'none'; elink.href = URL.createObjectURL(data); document.body.appendChild(elink); elink.click(); URL.revokeObjectURL(elink.href); // 释放URL 对象 document.body.removeChild(elink); } }; /** * 匹配数字 */ export const getNum = (str: string): string => { if (str) { const result = str.match(/\d+/g); return result ? result[0] : ''; } return ''; }; /** * 匹配运算字符 */ export const getSymbol = (str: string): string => { if (str) { const result = str.match(/[<>=]+/g); return result ? result[0] : ''; } return ''; }; /** * 匹配中文 */ export const getChinese = (str: string): string => { if (str) { const result = str.match(/[\u4e00-\u9fa5]+/g); return result ? result[0] : ''; } return ''; }; /** * 判断传入字符是否是负整数 */ export const isIllegalNum = (str: string | number): boolean => { if (str) { return isNaN(str) || str < 0; } return true; }; /** * 传入文件名获取文件后缀 */ export const getFileExt = (fileName: string): string => { return fileName.substring(fileName.lastIndexOf('.') + 1); }; /** * 返回projectId、checkpointId、assessmentId、competitionId */ export const getIds = (): Record => { return { cid: +Cookies.get('sand-cid'), checkpointId: +Cookies.get('sand-level') || '', projectId: +Cookies.get('sand-projectId') || '', assessmentId: +Cookies.get('sand-assessmentId') || '', competitionId: +Cookies.get('sand-competitionId') || '', }; }; /** * 获取当前时间 */ export const getNow = (): Promise => { return new Promise(async (resolve) => { const res = await getCurrentTime(); resolve(new Date(res.currentTime)); }); }; /** * 获取当前用户名 */ export const getUsername = async (): string => { const res = await getTheCurrentUserName(); return res.userName; }; export const whethers = [ { id: 795, name: '启用', }, { id: 796, name: '不启用', }, ]; export const opt1 = [ { id: 345, name: '是', }, { id: 346, name: '否', }, ];