import dayjs from 'dayjs';
|
import _ from 'lodash';
|
import { LifeRechargeConstants } from '@life-payment/core-vue';
|
|
export function object2query(object: Record<string, string | number>) {
|
return Object.keys(object)
|
.map((key) => (object[key] ? `${key}=${object[key]}` : ''))
|
.filter(Boolean)
|
.join('&');
|
}
|
|
export function query2object<T extends { [key: string]: string }>(query: string): T {
|
return Object.fromEntries(
|
decodeURIComponent(query)
|
.split('&')
|
.map((x) => x.split('='))
|
);
|
}
|
|
export function formatTimeWithoutCurrentYear(date: string) {
|
if (!date) return date;
|
const targetDate = dayjs(date);
|
const targetYear = targetDate.year();
|
const nowYear = dayjs().year();
|
return targetYear >= nowYear
|
? targetDate.format('MM-DD HH:mm')
|
: targetDate.format('YYYY-MM-DD HH:mm');
|
}
|
|
//去收尾空格
|
export function trim(str: string) {
|
return str.replace(/(^\s*)|(\s*$)/g, '');
|
}
|
|
export function paginateList<T>(list: T[], pageIndex: number, pageSize: number) {
|
const startIndex = (pageIndex - 1) * pageSize;
|
const endIndex = startIndex + pageSize;
|
return list.slice(startIndex, endIndex);
|
}
|
|
/**
|
* 添加星号
|
* @param str
|
* @param start
|
* @param end
|
* @returns
|
*/
|
export function addStarForString(str: string, start = 0, end = 0) {
|
return str.substring(0, start) + '*'.repeat(end - start) + str.substring(end);
|
}
|
|
export function addStarForEmail(str: string) {
|
const end = str.lastIndexOf('.');
|
return addStarForString(str, 2, end);
|
}
|
|
export function addStarForPhone(str: string) {
|
return `*${str.substring(str.length - 4)}`;
|
}
|
|
export function formatTimeAgo(date: string, format = 'YYYY-MM-DD HH:mm') {
|
const diff = dayjs().diff(date, 'seconds'); // 计算时间差,单位为秒
|
|
if (diff < 60) {
|
return `${diff}秒前`;
|
} else if (diff < 60 * 60) {
|
const minutes = Math.floor(diff / 60);
|
return `${minutes}分钟前`;
|
} else if (diff < 60 * 60 * 24) {
|
const hours = Math.floor(diff / (60 * 60));
|
return `${hours}小时前`;
|
} else if (dayjs(date).isSame(dayjs(), 'day')) {
|
return dayjs(date).format('HH:mm');
|
} else {
|
// const days = Math.floor(diff / (60 * 60 * 24));
|
// return `${days}天前`;
|
return formatTimeWithoutCurrentYear(date); //dayjs(date).format(format);
|
}
|
}
|
|
export function toRound(val: number, num = 2) {
|
if (val >= 0) {
|
return _.round(val, num);
|
} else {
|
const abs = Math.abs(val);
|
return 0 - _.round(abs, num);
|
}
|
}
|
|
export function toThousand(input: string | number, toFixed = 2) {
|
const num = toRound(Number(input)).toFixed(toFixed);
|
if (Number(num) >= 0) {
|
return num.toString().replace(/(^|\s)\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
|
} else {
|
const posNum = (0 - Number(num)).toFixed(2);
|
return (
|
'-' + posNum.toString().replace(/(^|\s)\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
|
);
|
}
|
}
|
|
export function format(date: string | Date, fmt = 'YYYY-MM-DD') {
|
return date ? dayjs(date).format(fmt) : '';
|
}
|
|
export function formatMaxNumber(count: number, max = 9999) {
|
return count > max ? `${max}+` : count;
|
}
|
|
export function guid() {
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
const r = (Math.random() * 16) | 0,
|
v = c === 'x' ? r : (r & 0x3) | 0x8;
|
return v.toString(16);
|
});
|
}
|
|
export function delay(timeout = 500) {
|
return new Promise((resolve) => {
|
setTimeout(() => {
|
resolve(1);
|
}, timeout);
|
});
|
}
|
|
export function filterJoin(list: any[], separator = '-') {
|
return list.filter(Boolean).join(separator);
|
}
|
|
export const hiddenIDNumberForEnd6 = (realIDNumber: string) =>
|
realIDNumber.replace(/^(\d+)(.{6})$/, '$1******');
|
|
// 订单状态
|
export function orderStatusEnum(
|
payStatus?: LifeRechargeConstants.LifePayStatusEnum,
|
lifePayOrderStatus?: LifeRechargeConstants.LifePayOrderStatusEnum
|
) {
|
if (
|
(lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.充值中 &&
|
payStatus === LifeRechargeConstants.LifePayStatusEnum.已支付) ||
|
payStatus === LifeRechargeConstants.LifePayStatusEnum.待退款
|
) {
|
return '支付成功';
|
}
|
if (payStatus === LifeRechargeConstants.LifePayStatusEnum.已退款) {
|
return '已退款';
|
}
|
if (
|
lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.已完成 &&
|
payStatus === LifeRechargeConstants.LifePayStatusEnum.已支付
|
) {
|
return '充值成功';
|
}
|
return '';
|
}
|
|
export function convertOrderFrontStatus(
|
payStatus?: LifeRechargeConstants.LifePayStatusEnum,
|
lifePayOrderStatus?: LifeRechargeConstants.LifePayOrderStatusEnum,
|
lifePayRefundStatus?: LifeRechargeConstants.LifePayRefundStatusEnum
|
) {
|
// if (
|
// (lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.充值中 &&
|
// payStatus === LifeRechargeConstants.LifePayStatusEnum.已支付) ||
|
// payStatus === LifeRechargeConstants.LifePayStatusEnum.待退款
|
// ) {
|
// return LifeRechargeConstants.LifePayOrderFrontStatusEnum.支付成功;
|
// }
|
if (
|
lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.已退款 ||
|
payStatus === LifeRechargeConstants.LifePayStatusEnum.已退款
|
) {
|
return LifeRechargeConstants.LifePayOrderFrontStatusEnum.已退款;
|
}
|
if (lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.待退款) {
|
return LifeRechargeConstants.LifePayOrderFrontStatusEnum.退款待审核;
|
}
|
if (
|
lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.退款失败 ||
|
lifePayRefundStatus === LifeRechargeConstants.LifePayRefundStatusEnum.退款驳回
|
) {
|
return LifeRechargeConstants.LifePayOrderFrontStatusEnum.退款失败;
|
}
|
if (
|
lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.已完成 &&
|
payStatus === LifeRechargeConstants.LifePayStatusEnum.已支付
|
) {
|
return LifeRechargeConstants.LifePayOrderFrontStatusEnum.充值成功;
|
}
|
return LifeRechargeConstants.LifePayOrderFrontStatusEnum.支付成功;
|
}
|
|
export class StringUtils {
|
static insertSpaces(str: string, space = 4) {
|
if (!str) return '';
|
const regex = new RegExp(`(.{${space}})`, 'g');
|
return str.replace(regex, '$1 ');
|
}
|
|
static societyCreditCodeInsertSpaces(str: string) {
|
if (!str) return '';
|
return str.replace(/(.{4})(.{4})(.{4})(.{6})/g, '$1 $2 $3 $4');
|
}
|
|
static idNumberInsertSpaces(str: string) {
|
if (!str) return '';
|
return str.replace(/(.{3})(.{3})(.{4})(.{4})(.{4})/g, '$1 $2 $3 $4 $5');
|
}
|
|
static phoneNumberAddSpace(realPhoneNumber: string) {
|
if (!realPhoneNumber) return '';
|
return realPhoneNumber.replace(/^(\d{3})(\d*)(\d{4})$/, '$1 $2 $3');
|
}
|
|
static formatterNumber(str: string) {
|
const cleanedValue = str.replace(/[^\d.]/g, '');
|
const singleDotValue = cleanedValue.replace(/(\..*)\./g, '$1');
|
const numberValue = parseFloat(singleDotValue);
|
return isNaN(numberValue) ? '' : singleDotValue;
|
}
|
}
|