123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- /**
- * 工具类
- */
- export const setDocumentTitle = title => {
- /**
- * 修改浏览器title 兼容ios
- */
- document.title = title;
- if (window.Env.isIos) {
- const i = document.createElement('iframe');
- i.src = '/favicon.ico';
- i.style.display = 'none';
- i.onload = () => {
- setTimeout(() => {
- i.remove();
- }, 10);
- };
- setTimeout(() => {
- document.body.appendChild(i);
- }, 500);
- }
- };
- export const setCookie = (name, value, time) => {
- const exp = new Date();
- exp.setTime(exp.getTime() + time * 1000);
- document.cookie = `${name}=${escape(value)};expires=${exp.toGMTString()};path=/`;
- };
- export const getCookie = name => {
- const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
- const arr = reg;
- if (arr === document.cookie.match(reg)) {
- return unescape(arr[2]);
- }
- return null;
- };
- export const delCookie = name => {
- const exp = new Date();
- exp.setTime(exp.getTime() - 1);
- const cval = window.getCookie(name);
- if (cval != null) {
- document.cookie = `${name}=${cval};expires=${exp.toGMTString()};path=/`;
- }
- };
- export const getQuery = name => {
- /**
- * 获取url参数
- */
- const reg = new RegExp(`(^|\\?|&)${name}=([^&]*)(&|$)`);
- const r = window.location.href.substr(1).match(reg);
- if (r != null) return unescape(r[2]);
- return null;
- };
- export function formatUrl(path, query) {
- let url = query ? `${path}?` : path;
- if (query) {
- Object.keys(query).forEach(i => {
- if (query[i] instanceof Object && query[i].length > 0) {
- query[i].forEach(k => {
- url += `${i}[]=${k}&`;
- });
- } else if (query[i] || query[i] === 0) {
- url += `${i}=${query[i]}&`;
- }
- });
- }
- return url;
- }
- export function checkMobile(s) {
- const { length } = s;
- if (length === 11 && /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[0-9]{1})|)+\d{8})$/.test(s)) {
- return true;
- }
- return false;
- }
- export function checkEmail(s) {
- if (/^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(s)) {
- return true;
- }
- return false;
- }
- export function zeroFill(num, length) {
- return (Array(length).join('0') + num).slice(-length);
- }
- export function loadScript(url, callback) {
- const script = document.createElement('script');
- script.type = 'text/javascript';
- script.async = true;
- script.defer = true;
- if (script.readyState) {
- script.onreadystatechange = function () {
- if (script.readyState === 'loaded' || script.readyState === 'complete') {
- script.onreadystatechange = null;
- if (callback) callback();
- }
- };
- } else {
- script.onload = function () {
- if (callback) callback();
- };
- }
- script.src = url;
- const head = document.getElementsByTagName('head')[0];
- head.appendChild(script);
- }
- export function generateUUID(len, radix) {
- const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
- const id = [];
- radix = radix || chars.length;
- if (len) {
- for (let i = 0; i < len; i += 1) id[i] = chars[0 | (Math.random() * radix)];
- } else {
- id[8] = id[13] = id[18] = id[23] = '-';
- id[14] = '4';
- for (let i = 0; i < 36; i += 1) {
- if (!id[i]) {
- const r = 0 | (Math.random() * 16);
- id[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r];
- }
- }
- }
- return id.join('');
- }
- export function SortBy(a, b, asc, type) {
- if (!a && a !== 0) {
- return 1;
- }
- if (!b && b !== 0) {
- return -1;
- }
- if (a === b) {
- return 0;
- }
- if (a === '') {
- return 1;
- }
- if (b === '') {
- return -1;
- }
- a = `${a}`;
- b = `${b}`;
- return (
- (type === 'number'
- ? a.localeCompare(b, undefined, { numeric: true })
- : a.localeCompare(b, 'zh', { co: 'pinyin' })) * asc
- );
- }
- export function SortByProps(item1, item2, props) {
- const cps = [];
- for (let i = 0; i < props.length; i += 1) {
- const prop = props[i];
- const asc = prop.direction > 0 ? 1 : -1;
- cps.push(SortBy(item1[prop.key], item2[prop.key], asc, prop.type));
- }
- for (let j = 0; j < cps.length; j += 1) {
- if (cps[j] === 1 || cps[j] === -1) {
- return cps[j];
- }
- }
- return false;
- }
- export function flattenTree(tree, fn, children = 'children') {
- const l = tree.map(item => {
- if (item[children] && item[children].length > 0) {
- const list = flattenTree(item[children], fn, children);
- return list.map((row) => fn(row, item));
- }
- return [item];
- });
- return [].concat(...l);
- }
- export function getMap(list, key = 'value', value = null, children = null) {
- const map = {};
- for (let i = 0; i < list.length; i += 1) {
- const item = list[i];
- let v = value ? item[value] : item;
- if (children && item[children] && item[children].length > 0) {
- v = getMap(item[children], key, value, children);
- }
- map[item[key]] = v;
- }
- return map;
- }
- export function searchKeyword(data, key, keyword, limit) {
- const list = [];
- const tmp = {};
- for (let i = 0; i < data.length; i += 1) {
- const item = key ? data[i][key] : data[i];
- if (item && !tmp[item] && item.indexOf(keyword) >= 0) {
- list.push(item);
- tmp[item] = true;
- if (limit && list.length >= limit) break;
- }
- }
- return list;
- }
- export function search(data = [], key, value) {
- const index = -1;
- for (let i = 0; i < data.length; i += 1) {
- if ((key && data[i][key] === value) || data[i] === value) {
- return i;
- }
- }
- return index;
- }
- export function dataURLtoBlob(dataurl) {
- const arr = dataurl.split(',');
- const mime = arr[0].match(/:(.*?);/)[1];
- const bstr = atob(arr[1]);
- const n = bstr.length;
- const u8arr = new Uint8Array(n);
- for (let i = 0; i < n; i += 1) {
- u8arr[i] = bstr.charCodeAt(i);
- }
- return new Blob([u8arr], { type: mime });
- }
- export function formatSecond(value) {
- let secondTime = parseInt(value || 0, 10); // 秒
- let minuteTime = 0;
- let hourTime = 0;
- if (secondTime > 60) {
- minuteTime = parseInt(secondTime / 60, 10);
- secondTime = parseInt(secondTime % 60, 10);
- hourTime = parseInt(minuteTime / 60, 10);
- minuteTime = parseInt(minuteTime % 60, 10);
- }
- if (hourTime >= 10) {
- hourTime = `${hourTime}`;
- } else {
- hourTime = `0${hourTime}`;
- }
- if (minuteTime >= 10) {
- minuteTime = `${minuteTime}`;
- } else {
- minuteTime = `0${minuteTime}`;
- }
- if (secondTime >= 10) {
- secondTime = `${secondTime}`;
- } else {
- secondTime = `0${secondTime}`;
- }
- return `${hourTime}:${minuteTime}:${secondTime}`;
- }
- export function formatMinuteSecond(value) {
- let secondTime = parseInt(value || 0, 10); // 秒
- let minuteTime = 0;
- if (secondTime > 60) {
- minuteTime = parseInt(secondTime / 60, 10);
- secondTime = parseInt(secondTime % 60, 10);
- }
- if (minuteTime >= 10) {
- minuteTime = `${minuteTime}`;
- } else {
- minuteTime = `0${minuteTime}`;
- }
- if (secondTime >= 10) {
- secondTime = `${secondTime}`;
- } else {
- secondTime = `0${secondTime}`;
- }
- return `${minuteTime}:${secondTime}`;
- }
- export function formatFormError(data, err, prefix = '') {
- const r = {};
- Object.keys(err).forEach(field => {
- r[`${prefix}${field}`] = { value: data[field], errors: err[field].map(e => new Error(e)) };
- });
- return r;
- }
- export function formatDate(time, format = 'YYYY-MM-DD HH:mm:ss') {
- const date = new Date(time);
- const o = {
- 'M+': date.getMonth() + 1,
- 'D+': date.getDate(),
- 'H+': date.getHours(),
- 'm+': date.getMinutes(),
- 's+': date.getSeconds(),
- 'q+': Math.floor((date.getMonth() + 3) / 3),
- S: date.getMilliseconds(),
- };
- if (/(Y+)/.test(format)) format = format.replace(RegExp.$1, `${date.getFullYear()}`.substr(4 - RegExp.$1.length));
- Object.keys(o).forEach(k => {
- if (new RegExp(`(${k})`).test(format)) {
- format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length));
- }
- });
- return format;
- }
- export function formatMinute(seconds, number = true) {
- const time = parseInt(seconds || 0, 10);
- return number ? parseInt(time / 60, 10) : `${parseInt(time / 60, 10)}min`;
- }
- export function formatSeconds(seconds, rand = false) {
- const time = parseInt(seconds || 0, 10);
- if (time < 60) {
- return `${time}s`;
- }
- if (time >= 60 && time < 3600) {
- return `${parseInt(time / 60, 10)}min${rand ? '' : formatSeconds(time % 60)}`;
- }
- return `${parseInt(time / 3600, 10)}hour${rand ? '' : formatSecond(time % 3600)}hour`;
- }
- export function formatPercent(child, mother, number = true) {
- if (!mother || !child) return number ? 0 : '0%';
- return number ? Math.floor((child * 100) / mother) : `${Math.floor((child * 100) / mother)}%`;
- }
- export function formatTreeData(list, key = 'id', title = 'title', index = 'parent_id') {
- const map = getMap(list, key);
- const result = [];
- list.forEach(row => {
- row.children = [];
- row.title = row[title];
- if (!row.key) row.key = `${row[key]}`;
- row.value = row[key];
- });
- list.forEach(row => {
- if (row[index] && map[row[index]]) {
- if (!map[row[index]].children) map[row[index]].children = [];
- map[row[index]].children.push(row);
- } else {
- result.push(row);
- }
- });
- return result;
- }
- export function flattenObject(ob, prefix = '', force = false) {
- const toReturn = {};
- if (prefix) prefix = `${prefix}.`;
- Object.keys(ob).forEach(i => {
- if (typeof ob[i] === 'object' && ob[i] !== null && !ob[i].length) {
- const flatObject = flattenObject(ob[i]);
- Object.keys(flatObject).forEach(x => {
- toReturn[`${prefix}${i}.${x}`] = flatObject[x];
- });
- } else if (force && ob[i].length) {
- ob[i].forEach((row, index) => {
- const flatObject = flattenObject(row);
- Object.keys(flatObject).forEach(x => {
- toReturn[`${prefix}${i}.${index}.${x}`] = flatObject[x];
- });
- });
- } else {
- toReturn[`${prefix}${i}`] = ob[i];
- }
- });
- return toReturn;
- }
- function _formatMoney(s, n) {
- if (!s) s = 0;
- n = n > 0 && n <= 20 ? n : 2;
- s = `${parseFloat(`${s}`.replace(/[^\d.-]/g, '')).toFixed(n)}`;
- const l = s
- .split('.')[0]
- .split('')
- .reverse();
- const r = s.split('.')[1];
- let t = '';
- for (let i = 0; i < l.length; i += 1) {
- t += l[i] + ((i + 1) % 3 === 0 && i + 1 !== l.length ? ',' : '');
- }
- return `${t
- .split('')
- .reverse()
- .join('')}.${r}`;
- }
- export function formatMoney(price) {
- if (typeof price === 'object') {
- return `${price.symbol} ${_formatMoney(price.value, 2)}`;
- }
- return `${_formatMoney(price, 2)}`;
- }
- export function bindTags(targetList, field, render, def, notFound) {
- let index = -1;
- targetList.forEach((row, i) => {
- if (row.key === field) index = i;
- });
- targetList[index].notFoundContent = notFound;
- targetList[index].select = (def || []).map(row => {
- return render(row);
- });
- }
- export function bindSearch(targetList, field, Component, listFunc, render, def, notFound = null) {
- let index = -1;
- targetList.forEach((row, i) => {
- if (row.key === field) index = i;
- });
- const key = `lastFetchId${field}${index}${generateUUID(4)}`;
- if (!Component[key]) Component[key] = 0;
- const searchFunc = data => {
- Component[key] += 1;
- const fetchId = Component[key];
- targetList[index].loading = true;
- Component.setState({ fetching: true });
- listFunc(data).then(result => {
- if (fetchId !== Component[key]) {
- // for fetch callback order
- return;
- }
- targetList[index].select = (result.list || result || []).map(row => {
- return render(row);
- });
- targetList[index].loading = false;
- Component.setState({ fetching: false });
- });
- };
- const item = {
- showSearch: true,
- showArrow: true,
- filterOption: false,
- onSearch: keyword => {
- searchFunc({ page: 1, size: 5, keyword });
- },
- notFoundContent: notFound,
- };
- targetList[index] = Object.assign(targetList[index], item);
- if (def) {
- if (targetList[index].type === 'multiple' || targetList[index].mode === 'multiple') {
- searchFunc({ ids: def, page: 1, size: def.length });
- } else {
- searchFunc({ ids: [def], page: 1, size: 1 });
- }
- } else {
- item.onSearch();
- }
- }
- export function generateSearch(field, props, Component, listFunc, render, def, notFound = null) {
- const key = `lastFetchId${field}${generateUUID(4)}`;
- if (!Component[key]) Component[key] = 0;
- let item = {
- showSearch: true,
- showArrow: true,
- filterOption: false,
- notFoundContent: notFound,
- };
- item = Object.assign(props || {}, item);
- const searchFunc = data => {
- Component[key] += 1;
- const fetchId = Component[key];
- item.loading = true;
- Component.setState({ [field]: item, fetching: true });
- listFunc(data).then(result => {
- if (fetchId !== Component[key]) {
- // for fetch callback order
- return;
- }
- item.select = result.list.map(row => {
- return render(row);
- });
- item.loading = false;
- Component.setState({ [field]: item, fetching: false });
- });
- };
- item.onSearch = keyword => {
- searchFunc({ page: 1, size: 5, keyword });
- };
- if (def) {
- if (item.mode === 'multiple' || item.type === 'multiple') {
- searchFunc({ ids: def, page: 1, size: def.length });
- } else {
- searchFunc({ ids: [def], page: 1, size: 1 });
- }
- } else {
- item.onSearch();
- }
- Component.setState({ [field]: item });
- }
- export function getHtmlText(text) {
- text = text.replace(new RegExp(/\r\n/, 'g'), '\r').replace(new RegExp(/\n/, 'g'), '\r');
- let html = '';
- text.split('\r').forEach(item => {
- item.split(' ').forEach(t => {
- html += `< i uuid = "${generateUUID(4)}" > ${t}</i > `;
- });
- html += '<br/>';
- });
- return html;
- }
- export function getSimpleText(html) {
- let text = html.replace(new RegExp('<br/>', 'g'), '\n\r');
- text = text.replace(new RegExp('<.+?>', 'g'), '');
- return text;
- }
- export function randomList(length) {
- const list = [];
- for (let i = 0; i < length; i += 1) {
- list.push(i);
- }
- for (let i = 0; i < length; i += 1) {
- const o = Math.floor(Math.random() * length);
- const tmp = list[o];
- list[o] = list[i];
- list[i] = tmp;
- }
- return list;
- }
- export function sortListWithOrder(target, order) {
- const list = [];
- order.forEach(t => {
- list.push(target[t]);
- });
- return list;
- }
- export function resortListWithOrder(target, order) {
- const list = [];
- for (let i = 0; i < order.length; i += 1) {
- list.push('');
- }
- order.forEach((t, i) => {
- list[t] = target[i];
- });
- return list;
- }
- // 下划线转换驼峰
- export function toHump(name) {
- return name.replace(/_(\w)/g, (all, letter) => {
- return letter.toUpperCase();
- });
- }
- // 驼峰转换下划线
- export function toLine(name) {
- return name.replace(/([A-Z])/g, '_$1').toLowerCase();
- }
- export function formatMonth(days, number = true) {
- return number ? parseInt(days / 30, 10) : `${parseInt(days / 30, 10)}个月`;
- }
- export function timeRange(timerange) {
- let startTime = null;
- let endTime = null;
- switch (timerange) {
- case 'all':
- break;
- case 'week':
- endTime = new Date();
- startTime = new Date(endTime.getTime() - 86400000 * 7);
- break;
- case 'month':
- endTime = new Date();
- startTime = new Date();
- startTime.setMonth(startTime.getMonth() - 1);
- break;
- case 'month3':
- endTime = new Date();
- startTime = new Date();
- startTime.setMonth(startTime.getMonth() - 3);
- break;
- case 'today':
- default:
- startTime = new Date();
- startTime.setHours(0, 0, 0, 0);
- endTime = new Date(startTime.getTime() + 86400000);
- }
- return [startTime, endTime];
- }
|