|
|
@ -1,238 +1,254 @@
|
|
|
|
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
|
|
|
|
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
|
|
|
|
import {Tag} from "antd";
|
|
|
|
import { Tag } from "antd";
|
|
|
|
import {
|
|
|
|
import { CaretUpOutlined, CaretDownOutlined } from "@ant-design/icons";
|
|
|
|
CaretUpOutlined,
|
|
|
|
|
|
|
|
CaretDownOutlined
|
|
|
|
|
|
|
|
} from '@ant-design/icons';
|
|
|
|
|
|
|
|
import moment from "moment";
|
|
|
|
import moment from "moment";
|
|
|
|
|
|
|
|
|
|
|
|
if (!String.prototype.padStart) {
|
|
|
|
if (!String.prototype.padStart) {
|
|
|
|
String.prototype.padStart = function padStart(targetLength, padString) {
|
|
|
|
String.prototype.padStart = function padStart(targetLength, padString) {
|
|
|
|
targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
|
|
|
|
targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
|
|
|
|
padString = String((typeof padString !== 'undefined' ? padString : ' '));
|
|
|
|
padString = String(typeof padString !== "undefined" ? padString : " ");
|
|
|
|
if (this.length > targetLength) {
|
|
|
|
if (this.length > targetLength) {
|
|
|
|
return String(this);
|
|
|
|
return String(this);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
targetLength = targetLength - this.length;
|
|
|
|
targetLength = targetLength - this.length;
|
|
|
|
if (targetLength > padString.length) {
|
|
|
|
if (targetLength > padString.length) {
|
|
|
|
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
|
|
|
|
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return padString.slice(0, targetLength) + String(this);
|
|
|
|
return padString.slice(0, targetLength) + String(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function copy(obj) {
|
|
|
|
export function copy(obj) {
|
|
|
|
return JSON.parse(JSON.stringify(obj));
|
|
|
|
return JSON.parse(JSON.stringify(obj));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function named(value) {
|
|
|
|
export function named(value) {
|
|
|
|
return function (target) {
|
|
|
|
return function (target) {
|
|
|
|
target.definedName = value;
|
|
|
|
target.definedName = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function formatCurrency(name) {
|
|
|
|
export function formatCurrency(name) {
|
|
|
|
if (name === 'USD') {
|
|
|
|
if (name === "USD") {
|
|
|
|
return '$';
|
|
|
|
return "$";
|
|
|
|
} else if (name === 'RMB') {
|
|
|
|
} else if (name === "RMB") {
|
|
|
|
return '¥';
|
|
|
|
return "¥";
|
|
|
|
} else if (name === 'EUR') {
|
|
|
|
} else if (name === "EUR") {
|
|
|
|
return '€'
|
|
|
|
return "€";
|
|
|
|
} else if (name === 'GBP') {
|
|
|
|
} else if (name === "GBP") {
|
|
|
|
return '£'
|
|
|
|
return "£";
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return name + ' ';
|
|
|
|
return name + " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function formatPrice(price) {
|
|
|
|
export function formatPrice(price) {
|
|
|
|
return Math.ceil(price).toLocaleString();
|
|
|
|
return Math.ceil(price).toLocaleString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//千分符的金额转成数字,默认为0
|
|
|
|
|
|
|
|
export function price_to_number(price) {
|
|
|
|
|
|
|
|
let num_string = (price + "").replace(/,/g, "");
|
|
|
|
|
|
|
|
let number = parseFloat(num_string);
|
|
|
|
|
|
|
|
return isNaN(number) ? 0 : number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function formatPercent(number) {
|
|
|
|
export function formatPercent(number) {
|
|
|
|
return Math.round(number * 100) + '%'
|
|
|
|
return Math.round(number * 100) + "%";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function formatDate(date) {
|
|
|
|
export function formatDate(date) {
|
|
|
|
if (isEmpty(date)) {
|
|
|
|
if (isEmpty(date)) {
|
|
|
|
return 'NaN';
|
|
|
|
return "NaN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const year = date.getFullYear();
|
|
|
|
const year = date.getFullYear();
|
|
|
|
const month = date.getMonth() + 1;
|
|
|
|
const month = date.getMonth() + 1;
|
|
|
|
const day = date.getDate();
|
|
|
|
const day = date.getDate();
|
|
|
|
|
|
|
|
|
|
|
|
const monthStr = ('' + month).padStart(2, 0);
|
|
|
|
const monthStr = ("" + month).padStart(2, 0);
|
|
|
|
const dayStr = ('' + day).padStart(2, 0);
|
|
|
|
const dayStr = ("" + day).padStart(2, 0);
|
|
|
|
const formatted = year + '-' + monthStr + '-' + dayStr;
|
|
|
|
const formatted = year + "-" + monthStr + "-" + dayStr;
|
|
|
|
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
return formatted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function formatTime(date) {
|
|
|
|
export function formatTime(date) {
|
|
|
|
const hours = date.getHours();
|
|
|
|
const hours = date.getHours();
|
|
|
|
const minutes = date.getMinutes();
|
|
|
|
const minutes = date.getMinutes();
|
|
|
|
|
|
|
|
|
|
|
|
const hoursStr = ('' + hours).padStart(2, 0);
|
|
|
|
const hoursStr = ("" + hours).padStart(2, 0);
|
|
|
|
const minutesStr = ('' + minutes).padStart(2, 0);
|
|
|
|
const minutesStr = ("" + minutes).padStart(2, 0);
|
|
|
|
const formatted = hoursStr + ':' + minutesStr;
|
|
|
|
const formatted = hoursStr + ":" + minutesStr;
|
|
|
|
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
return formatted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function formatDatetime(date) {
|
|
|
|
export function formatDatetime(date) {
|
|
|
|
|
|
|
|
const year = date.getFullYear();
|
|
|
|
|
|
|
|
const month = date.getMonth() + 1;
|
|
|
|
|
|
|
|
const day = date.getDate();
|
|
|
|
|
|
|
|
|
|
|
|
const year = date.getFullYear();
|
|
|
|
const monthStr = ("" + month).padStart(2, 0);
|
|
|
|
const month = date.getMonth() + 1;
|
|
|
|
const dayStr = ("" + day).padStart(2, 0);
|
|
|
|
const day = date.getDate();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const monthStr = ('' + month).padStart(2, 0);
|
|
|
|
|
|
|
|
const dayStr = ('' + day).padStart(2, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const hours = date.getHours();
|
|
|
|
const hours = date.getHours();
|
|
|
|
const minutes = date.getMinutes();
|
|
|
|
const minutes = date.getMinutes();
|
|
|
|
|
|
|
|
|
|
|
|
const hoursStr = ('' + hours).padStart(2, 0);
|
|
|
|
const hoursStr = ("" + hours).padStart(2, 0);
|
|
|
|
const minutesStr = ('' + minutes).padStart(2, 0);
|
|
|
|
const minutesStr = ("" + minutes).padStart(2, 0);
|
|
|
|
|
|
|
|
|
|
|
|
const formatted = year + '-' + monthStr + '-' + dayStr + ' ' + hoursStr + ':' + minutesStr;
|
|
|
|
const formatted = year + "-" + monthStr + "-" + dayStr + " " + hoursStr + ":" + minutesStr;
|
|
|
|
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
return formatted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function mixins(...list) {
|
|
|
|
export function mixins(...list) {
|
|
|
|
return function (target) {
|
|
|
|
return function (target) {
|
|
|
|
list.forEach(val => {
|
|
|
|
list.forEach(val => {
|
|
|
|
const mixinObj = Object.create(val.prototype, {})
|
|
|
|
const mixinObj = Object.create(val.prototype, {});
|
|
|
|
const name = Object.getPrototypeOf(mixinObj).constructor.name;
|
|
|
|
const name = Object.getPrototypeOf(mixinObj).constructor.name;
|
|
|
|
const camelCase = name.substr(0, 1).toLowerCase() + name.substr(1);
|
|
|
|
const camelCase = name.substr(0, 1).toLowerCase() + name.substr(1);
|
|
|
|
Object.assign(target.prototype, {[camelCase]: mixinObj})
|
|
|
|
Object.assign(target.prototype, { [camelCase]: mixinObj });
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function camelCase(name) {
|
|
|
|
export function camelCase(name) {
|
|
|
|
return name.substr(0, 1).toLowerCase() + name.substr(1);
|
|
|
|
return name.substr(0, 1).toLowerCase() + name.substr(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class UrlBuilder {
|
|
|
|
export class UrlBuilder {
|
|
|
|
constructor(url) {
|
|
|
|
constructor(url) {
|
|
|
|
this.url = url;
|
|
|
|
this.url = url;
|
|
|
|
this.paramList = [];
|
|
|
|
this.paramList = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
append(name, value) {
|
|
|
|
append(name, value) {
|
|
|
|
if (isNotEmpty(value)) {
|
|
|
|
if (isNotEmpty(value)) {
|
|
|
|
this.paramList.push({'name': name, 'value': value});
|
|
|
|
this.paramList.push({ name: name, value: value });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
build() {
|
|
|
|
build() {
|
|
|
|
this.paramList.forEach((e, i, a) => {
|
|
|
|
this.paramList.forEach((e, i, a) => {
|
|
|
|
if (i === 0) {
|
|
|
|
if (i === 0) {
|
|
|
|
this.url += '?';
|
|
|
|
this.url += "?";
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.url += '&';
|
|
|
|
this.url += "&";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.url += e.name + '=' + e.value;
|
|
|
|
this.url += e.name + "=" + e.value;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return this.url;
|
|
|
|
return this.url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function isNotEmpty(val) {
|
|
|
|
export function isNotEmpty(val) {
|
|
|
|
return val !== undefined && val !== null && val !== '';
|
|
|
|
return val !== undefined && val !== null && val !== "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function isEmpty(val) {
|
|
|
|
export function isEmpty(val) {
|
|
|
|
return val === undefined || val === null || val === '';
|
|
|
|
return val === undefined || val === null || val === "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function empty(a) {
|
|
|
|
export function empty(a) {
|
|
|
|
if (a === "") return true; //检验空字符串
|
|
|
|
if (a === "") return true; //检验空字符串
|
|
|
|
if (a === "null") return true; //检验字符串类型的null
|
|
|
|
if (a === "null") return true; //检验字符串类型的null
|
|
|
|
if (a === "undefined") return true; //检验字符串类型的 undefined
|
|
|
|
if (a === "undefined") return true; //检验字符串类型的 undefined
|
|
|
|
if (!a && a !== 0 && a !== "") return true; //检验 undefined 和 null
|
|
|
|
if (!a && a !== 0 && a !== "") return true; //检验 undefined 和 null
|
|
|
|
if (Array.prototype.isPrototypeOf(a) && a.length === 0) return true; //检验空数组
|
|
|
|
if (Array.prototype.isPrototypeOf(a) && a.length === 0) return true; //检验空数组
|
|
|
|
if (Object.prototype.isPrototypeOf(a) && Object.keys(a).length === 0) return true; //检验空对象
|
|
|
|
if (Object.prototype.isPrototypeOf(a) && Object.keys(a).length === 0) return true; //检验空对象
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function prepareUrl(url) {
|
|
|
|
export function prepareUrl(url) {
|
|
|
|
return new UrlBuilder(url);
|
|
|
|
return new UrlBuilder(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function debounce(fn, delay = 500) {
|
|
|
|
export function debounce(fn, delay = 500) {
|
|
|
|
let timer;
|
|
|
|
let timer;
|
|
|
|
return (e) => {
|
|
|
|
return e => {
|
|
|
|
e.persist();
|
|
|
|
e.persist();
|
|
|
|
clearTimeout(timer);
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
fn(e);
|
|
|
|
fn(e);
|
|
|
|
}, delay);
|
|
|
|
}, delay);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function throttle(fn, delay, atleast) {
|
|
|
|
export function throttle(fn, delay, atleast) {
|
|
|
|
let timeout = null,
|
|
|
|
let timeout = null,
|
|
|
|
startTime = new Date();
|
|
|
|
startTime = new Date();
|
|
|
|
return function () {
|
|
|
|
return function () {
|
|
|
|
let curTime = new Date();
|
|
|
|
let curTime = new Date();
|
|
|
|
clearTimeout(timeout);
|
|
|
|
clearTimeout(timeout);
|
|
|
|
if (curTime - startTime >= atleast) {
|
|
|
|
if (curTime - startTime >= atleast) {
|
|
|
|
fn();
|
|
|
|
fn();
|
|
|
|
startTime = curTime;
|
|
|
|
startTime = curTime;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
timeout = setTimeout(fn, delay);
|
|
|
|
timeout = setTimeout(fn, delay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function clickUrl(url) {
|
|
|
|
export function clickUrl(url) {
|
|
|
|
const httpLink = document.createElement('a');
|
|
|
|
const httpLink = document.createElement("a");
|
|
|
|
httpLink.href = url;
|
|
|
|
httpLink.href = url;
|
|
|
|
httpLink.target = '_blank';
|
|
|
|
httpLink.target = "_blank";
|
|
|
|
httpLink.click();
|
|
|
|
httpLink.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function show_vs_tag(vs, vs_diff, data1, data2) {
|
|
|
|
export function show_vs_tag(vs, vs_diff, data1, data2) {
|
|
|
|
let tag = '-';
|
|
|
|
let tag = "-";
|
|
|
|
if (parseInt(vs) < 0) {
|
|
|
|
if (parseInt(vs) < 0) {
|
|
|
|
tag = <Tag icon={<CaretDownOutlined/>} color="gold">{vs} {vs_diff}</Tag>
|
|
|
|
tag = (
|
|
|
|
} else if (parseInt(vs) > 0) {
|
|
|
|
<Tag icon={<CaretDownOutlined />} color="gold">
|
|
|
|
tag = <Tag icon={< CaretUpOutlined/>} color="lime">{vs} {vs_diff}</Tag>
|
|
|
|
{vs} {vs_diff}
|
|
|
|
}// else {
|
|
|
|
</Tag>
|
|
|
|
// tag = <Tag icon={< CaretUpOutlined/>} color="lime">{vs} {vs_diff}</Tag>
|
|
|
|
);
|
|
|
|
// }
|
|
|
|
} else if (parseInt(vs) > 0) {
|
|
|
|
return <span><div>{data1} vs {data2} </div> {tag} </span>
|
|
|
|
tag = (
|
|
|
|
|
|
|
|
<Tag icon={<CaretUpOutlined />} color="lime">
|
|
|
|
|
|
|
|
{vs} {vs_diff}
|
|
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
<span>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
|
|
|
{data1} vs {data2}
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{tag}
|
|
|
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//数组去掉重复
|
|
|
|
//数组去掉重复
|
|
|
|
export function unique(arr) {
|
|
|
|
export function unique(arr) {
|
|
|
|
let x = new Set(arr);
|
|
|
|
let x = new Set(arr);
|
|
|
|
return [...x];
|
|
|
|
return [...x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function getWeek (date) { // 参数时间戳
|
|
|
|
export function getWeek(date) {
|
|
|
|
let week = moment(date).day();
|
|
|
|
// 参数时间戳
|
|
|
|
switch (week) {
|
|
|
|
let week = moment(date).day();
|
|
|
|
case 1:
|
|
|
|
switch (week) {
|
|
|
|
return '周一'
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
return "周一";
|
|
|
|
return '周二'
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
return "周二";
|
|
|
|
return '周三'
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
return "周三";
|
|
|
|
return '周四'
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
return "周四";
|
|
|
|
return '周五'
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
return "周五";
|
|
|
|
return '周六'
|
|
|
|
case 6:
|
|
|
|
case 0:
|
|
|
|
return "周六";
|
|
|
|
return '周日'
|
|
|
|
case 0:
|
|
|
|
}
|
|
|
|
return "周日";
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|