Files
fe_service_ebtp_frontend/src/utils/CommonUtils.ts
2022-09-27 15:31:15 +08:00

338 lines
8.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author: liqiang
* @Date: 2020-11-24 15:26:54
* @LastEditTime: 2021-03-05 13:49:12
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \ebtp-cloud-frontend\src\utils\CommonUtils.ts
*/
/**
* 判断是否等于空
* @param value
*/
export function isEmpty(value: any) {
return value === null || value === void 0 || value === '';
}
/**
* 判断不等于空
* @param value
*/
export function isNotEmpty(value: any) {
return value !== null && value !== void 0 && value !== '';
}
/**
* proTable下拉选格式转换
* @param data 字典数据
*/
export function proTableValueEnum(data: any) {
let json = {};
if (isEmpty(data)) {
return json;
}
for (const item of data) {
json[item.code] = { text: item.dicName };
}
return json;
}
/**
* 字典返回值
* @param data 字典数据
*/
export function returnDictVal(data: any, value: any) {
let val = '';
if (isEmpty(data)) {
return val;
}
for (const item of data) {
item.code === value ? val = item.dicName : null;
}
return val;
}
/**
* procurementType标的类型转换多个 20220927
*/
export function multipleTypeTransform(value: string | null | undefined, valueEnum: any): string {
if (valueEnum && value) {
return value.split(",").map(item => valueEnum[item].text).join();
}
return "-";
}
/**
* 获取url路径信息
* @param name
*/
export function getURLInformation(name: string) {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
let r = window.location.search.substr(1).match(reg);
return r !== null ? unescape(r[2]) : null;
}
/**
* 获取url路径参数信息
* @param name
* @returns
*/
export function getUrlParam(name: string) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return decodeURI(r[2]); //decodeURI参数内容。
return null; //返回参数值
}
/**
* 获取全部url路径信息
* @param name
* @returns 包含全部参数的对象
*/
export function getTotalURLInformation() {
let query: any = location.search.substring(1)
let params = {}
query = query.split('&')
query?.forEach((ele: any, index: any) => {
let q = query[index].split('=')
if (q.length == 2) {
params[q[0]] = q[1]
}
});
return params
}
/**
* 跨工程跳转
* @param url url多个参数 & => %26
*/
export function JumpToOutside(url: string) {
const uri = encodeURIComponent(url)
window.location.href = (`http://10.242.31.158:8100/auth/oauth/authorize?response_type=code&client_id=KgPEkttG&redirect_uri=${uri}&mall3_token=${sessionStorage.getItem('Authorization')}`)
}
/**
* proTable下拉选格式转换 增加筛选 仅展示inspect中数据
* @param data 字典数据
*/
export function proTableValueEnumOther(data: any, inspect?: any) {
let json = {};
if (isEmpty(data)) {
return json;
}
for (const item of data) {
if (inspect.toString().indexOf(item.code) >= 0) {
json[item.code] = { text: item.dicName };
}
}
return json;
}
/**
* 根据字典值返回字典名称数据
* @param dictData
* @param value
*/
export function getDictName(dictData: any, value: any) {
if (isEmpty(dictData)) {
return '';
}
for (const item of dictData) {
if (value === item.code) {
return item.dicName;
}
}
return '';
}
/**
* 根据采购方式获取proTypeCode
* @param bidMethodDict
*/
export function getProTypeCodeByBidMethodDict(bidMethodDict: any) {
if (isEmpty(bidMethodDict)) {
return '';
}
let proTypeCode;
switch (bidMethodDict) {
case 'procurement_mode_1':
case 'procurement_mode_2':
proTypeCode = 'procurement_mode_1,procurement_mode_2';
break;
case 'procurement_mode_3':
proTypeCode = 'procurement_mode_3';
break;
case 'procurement_mode_4':
proTypeCode = 'procurement_mode_4';
break;
case 'procurement_mode_5':
case 'procurement_mode_6':
proTypeCode = 'procurement_mode_5,procurement_mode_6';
break;
case 'procurement_mode_7':
proTypeCode = 'procurement_mode_7';
break;
case 'procurement_mode_8':
proTypeCode = 'procurement_mode_8';
break;
case 'procurement_mode_9':
proTypeCode = 'procurement_mode_8';
break;
default:
proTypeCode = '';
}
return proTypeCode;
}
/**
* 根据获取字典名
* @param data
* @param value
*/
export function getDictNameByVal(data: any[], value: any) {
if (isEmpty(data) || isEmpty(value)) {
return "";
}
for (const item of data) {
if (item.code === String(value)) {
return item.dicName;
}
}
return "";
}
/**
* 获取当前路由相对路径
* @returns
*/
export function getUrlRelativePath() {
let url = document.location.toString();
let arrUrl = url.split("//");
let start = arrUrl[1].indexOf("/");
let relUrl = arrUrl[1].substring(start);
if (relUrl.indexOf("?") != -1) {
relUrl = relUrl.split("?")[0];
}
return relUrl;
}
/**
* 获取目标字符第几次出现的位置
* @param str
* @param cha
* @param num
* @returns
*/
function findString(str: string, cha: string, num: number) {
let x = str.indexOf(cha);
for (var i = 0; i < num; i++) {
x = str.indexOf(cha, x + 1);
}
return x;
}
const projectTypeCodeMaps = new Map();
//招标
projectTypeCodeMaps.set("/Bid", ["procurement_mode_1", "procurement_mode_2"]);
//比选
projectTypeCodeMaps.set("/Comparison", ["procurement_mode_3"]);
//招募
projectTypeCodeMaps.set("/Recruit", ["procurement_mode_4"]);
//谈判
projectTypeCodeMaps.set("/Negotiation", ["procurement_mode_5", "procurement_mode_6",]);
//询价
projectTypeCodeMaps.set("/Inquiry", ["procurement_mode_7"]);
/**
* 获取ProTypeCode
* @returns
*/
export function getProjectTypeCode() {
let url = getUrlRelativePath();
for (const item of projectTypeCodeMaps.keys()) {
if (url.indexOf(item) !== -1) {
return projectTypeCodeMaps.get(item);
}
}
return [];
}
/**
* 根据传入采购方式返回该路径
* @param bidMethodDict
* @returns
*/
export function getBidMethodDictTypeCode(bidMethodDict: string) {
for (const [key, value] of projectTypeCodeMaps.entries()) {
if (value.indexOf(bidMethodDict) !== -1) {
return key;
}
}
let url = getUrlRelativePath(),
index = findString(url, "/", 1);
url = url.substring(0, index);
return url;
}
/**
* 将阿拉伯数字翻译成中文的大写数字
* @param num
* @returns
*/
export const numberToChinese = (num: any) => {
var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十");
var BB = new Array("", "十", "百", "仟", "萬", "億", "点", "");
var a = ("" + num).replace(/(^0*)/g, "").split("."),
k = 0,
re = "";
for (var i = a[0].length - 1; i >= 0; i--) {
switch (k) {
case 0:
re = BB[7] + re;
break;
case 4:
if (!new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$")
.test(a[0]))
re = BB[4] + re;
break;
case 8:
re = BB[5] + re;
BB[7] = BB[5];
k = 0;
break;
}
if (k % 4 == 2 && a[0].charAt(i + 2) != '0' && a[0].charAt(i + 1) == '0')
re = AA[0] + re;
if (a[0].charAt(i) != '0')
re = AA[a[0].charAt(i)] + BB[k % 4] + re;
k++;
}
if (a.length > 1) // 加上小数部分(如果有小数部分)
{
re += BB[6];
for (var i = 0; i < a[1].length; i++)
re += AA[a[1].charAt(i)];
}
if (re == '一十')
re = "十";
if (re.match(/^一/) && re.length == 3)
re = re.replace("一", "");
return re;
}
/**
* 页面内锚点滚动方法
* @param anchorName
*/
export const scrollToAnchor = (anchorName: string) => {
if (anchorName) {
// 找到锚点
let anchorElement = document.getElementById(anchorName);
// 如果对应id的锚点存在就跳转到锚点
if (anchorElement) { anchorElement.scrollIntoView({ block: 'start', behavior: 'smooth' }); }
}
}
/**
* 删除行首行尾的空白字符方法(包括空格、制表符、换页符等等)
* @param str
* @returns
*/
export const trim = (str: string): string => {
if (isNotEmpty(str)) {
let _str = str.replace(/(^\s*)|(\s*$)/g, "");
_str = _str.replace(/[<>|\\/?:*""“”\s\r\n\t]/g, "");
return _str;
}
return '';
}