/** * 全局公用过滤器 */ import Constant from '../constant/constant'; // const Constant = require('../constant/constant') /** * 根据location 返回指定类型(param)的数据 * type 可选 string | Array, house,groudId,poolCode,config,node,driver * 空值时返回 全部数据分割之后的数组 */ function locationPipe(location: string, type: string | Array, driver?: string): string { if (!location) { return '-'; } let arr = location.split('/'); if (typeof type === 'string') { return locationType(arr, type, driver); } else if (typeof type === 'object') { let content = ''; type.forEach((key, idx) => { content += locationType(arr, key, driver); idx !== type.length - 1 && (content += ' | ') }) return content; } return '-' } /**过滤节点类型 */ function locationType(locationArr: Array, type: string, driver?: string) { let str = ''; switch (type) { case 'house': str = locationArr[0]; break; case 'groudId': str = locationArr[1]; break; case 'poolCode': str = locationArr[2]; break; case 'config': str = locationArr[3]; break; case 'node': str = locationArr[4]; break; case 'driver': str = String(driver).toLocaleUpperCase(); break; default: str = '-'; } return str === 'NA' ? '-' : str; } /** *账号信息加密 * @param {*} value 账号 * @param {*} [type] 加密类型(加密位数区别) * @return {*} {*} */ function StarInfoPipe(value: any, type?: any): any { let newVal; switch (type) { case 'set': if (!value) { newVal = '—'; } else if (value.length > 12) { newVal = value.substr(0, 5) + '***' + value.substr(value.length - 6, 6); } else if (value.length > 5) { newVal = value.substr(0, 2) + '***' + value.substr(value.length - 3, 3); } else if (value.length > 3) { newVal = value.substr(0, 1) + '***' + value.substr(value.length - 1, 1); } else { newVal = value.substr(0, 1) + '***'; } break; case 'name': if (!value) { newVal = '—'; } else if (value.length > 2) { newVal = value.substr(0, 1) + '*' + value.substr(value.length - 1, 1); } else { newVal = value.substr(0, 1) + '*'; } break; default: if (!value) { newVal = '-'; } else if (value.length > 5) { newVal = value.substr(0, 2) + '***' + value.substr(value.length - 3, 3); } else if (value.length > 3) { newVal = value.substr(0, 1) + '***' + value.substr(value.length - 1, 1); } else { newVal = value.substr(0, 1) + '***'; } break; } return newVal; } /** *任务状态信息过滤 * @param {*} status * @return {*} {*} */ function asyncTaskStatus(status: string): string { if (status == '0') { status = '等待执行'; } else if (status == '1') { status = '正在执行'; } else if (status == '2') { status = '执行成功'; } else if (status == '-1' || status == '-2') { status = '执行失败'; } else if (status == '-1,-2') { status = '执行失败'; } return status; } /**查询rds架构 */ function getRdsCategory(value: any) { if (!value) { return '全部'; } const obj: any = { basic: '单节点', highavailability: '双节点', finance: '三节点', }; return obj[value]; } /**流量数据单位转换 */ function unitBwPipe(value: any, unit: any) { let val = !!value ? value + ' ' + unit : '无'; if (value >= 1024 && value / 1024 < 1024) { let tmp = (value / 1024).toFixed(2); val = !!tmp ? tmp + ' K' + unit : '无'; } else if (value / 1024 >= 1024 && value / 1024 / 1024 < 1024) { let tmp = (value / 1024 / 1024).toFixed(2); val = !!tmp ? tmp + ' M' + unit : '无'; } else if (value / 1024 / 1024 >= 1024) { let tmp = (value / 1024 / 1024 / 1024).toFixed(2); val = !!tmp ? tmp + ' G' + unit : '无'; } return val; } /** * WAF类型配置过滤 * @param value * @returns */ function wafType(value: string): string { let type: any = { low: '低级配置', middle: '中级配置', high: '高级配置', standard: '标准版', professional: '专业版', '标准版': 'standard', '专业版': 'professional', 低级配置: 'low', 中级配置: 'middle', 高级配置: 'high', }; return value ? type[value] : ' '; } /** * IP类型 * @param value * @returns */ function ipTypePipe(value: string): string { return Constant.bandDescMap[value] ? Constant.bandDescMap[value] : value ? value.replace('IP', '').replace('VM_', '') : '-'; } /** * 计费方式 */ function billingMethodPipe(value: string): string { switch (value) { case 'ElasticByDay': return '弹性计费'; case 'ElasticByDayFree': return '弹性计费(免费)'; case 'Package': return '包年包月'; case 'PackageFree': return '包年包月(免费)'; case 'FlowByHour': return '按流量计费'; case 'Bandwitch': return '按带宽计费'; default: return '弹性计费'; } } /** * 带宽计费方式 * @param value * @returns */ function bwBillingMethodPipe(value: string): string { switch (value) { case 'FlowByHour': return '按流量计费'; case 'ElasticByDay': return '按带宽计费'; default: return '按带宽计费'; } } /** * 脱敏 * @param value * @param type */ function desensitization(value: any, type: any) { let returnValue = ""; switch (type) { case "phone": { returnValue = value.substring(0, 2) + '****' + value.substring(8) break; } } return returnValue; } /** * 磁盘类型 * @param type {String} 磁盘介质类型 */ function diskMediumType(type: any) { if (!type) { return; } let name: any = "" switch (type) { case "LVM_HDD": { name = "本地中心存储" break; } case "RBD_HDD": { name = "中心存储" break; } case "RBD_SSD": { name = "SSD存储" break; } case "iSCSI_SATA": { name = "中心存储" break; } case "iSCSI_SSD": { name = "SSD存储" break; } } return name } function system(value: any, type: string = 'icon') { let icon = "", name = ""; let returnValue = ""; if (!value) { return ''; } switch (type) { case 'icon': { if (value.indexOf('CentOS') !== -1) { icon = "icon-centos" } else if (value.indexOf('Debian') !== -1) { icon = "icon-debian" } else if (value.indexOf('Fedora') !== -1) { icon = "icon-fedora" } else if (value.indexOf('RedHat') !== -1) { icon = "icon-redhat" } else if (value.indexOf('Ubuntu') !== -1) { icon = "icon-ubuntu" } else if(value.indexOf('Windows') !== -1) { icon = "icon-windows" } else if(value.indexOf('RouterOS') !== -1){ icon = 'icon-routeros' } else if (value.indexOf('VyOS') !== -1) { icon = 'icon-config-info' } else { icon = 'icon-windows' } returnValue = icon; break; } case "name": { let arr = value.split('_'); arr.splice(0, 1); return arr.join('_'); // returnValue = value.split('Template_')[1] || value.split('Template3.3_')[1]; } } return returnValue } /** * 网络类型 */ function network(value: any) { if (!value) { return ''; } let name = "" if (value === 'vpc') { name = "VPC网络" } else { name = "经典网络" } return name } /** * 带宽类型 */ function bandwidthModel(value: any) { if (!value) { return ''; } let name = "" if (value === 'fixed') { name = "固定带宽" } else if (value === 'dynamic') { name = "动态带宽" } return name } /** * 资源名称 * @param value */ function resourceNamePipe(value: string): string { switch (value) { case 'vm': return '云主机'; case "image": return '镜像' case "disk": return '云磁盘' case "snap": return '云快照' case "ip": return '云IP' case 'nat': return 'NAT网关'; case 'slb': return 'SLB负载均衡'; case 'rds': return 'RDS云数据库'; case 'securityGroup': { return '安全组'; } case 'securityRule': { return '安全组规则'; } case "bandWidth": { return '带宽' } } return ''; } /** * 资源名称 * @param value */ function newResourceNamePipe(value: string): string { switch (value) { case 'vmRate': return '云主机'; case "imageRate": return '镜像' case "diskRate": return '云磁盘' case "snapRate": return '云快照' case "ipRate": return '云IP' case 'natRate': return 'NAT网关'; case 'slbRate': return 'SLB负载均衡'; case 'rdsRate': return 'RDS云数据库'; case 'securityGroupRate': { return '安全组'; } case 'securityRuleRate': { return '安全组规则'; } case "bandWidthRate": { return '带宽' } } return ''; } /** * 资源计费状态 * @param value */ function resourceBillingState(value: string, type = "name"): string { let name = "", icon = "" switch (value) { case 'normal': name = '正常'; icon = "icon-circle-success color-success" break; case null: case 'expired': name = '欠费'; icon = "icon-forbidden color-danger" break; case "arrears": name = '到期'; icon = "icon-forbidden color-danger" break; default: return '' } if (type === 'name') { return name } else { return icon } } /** * 实名认证状态 * @param value */ function realNameState(value: string | number, type = "name"): string { let name = "", icon = "" switch (value) { case 1: case '1': name = '已认证'; icon = "icon-circle-success color-success" break; case null: case 0: case '0': name = '未认证'; icon = "icon-forbidden color-danger" break; case -2: case '-2': name = '认证失败'; icon = "icon-forbidden color-danger" break; default: return '' } if (type === 'name') { return name } else { return icon } } /** * 实名认证状态 * @param value */ function verifyStatusNew(value: any, type = "person"): string { let name = "", icon = "" let status = ''; let company = value.companyVeriyedStatus, person = value.personVeriyed; switch (company) { case 0: switch (person) { case 0: { status = '未实名认证'; break; } case -1: { status = '个人认证待审核'; break; } case -2: { status = '个人认证失败'; break; } case -3: { status = '个人认证审核中'; break; } case 1: { status = '个人认证成功'; break; } } break; case -1: { status = '企业认证失败'; break; } case -2: { status = '企业认证审核中'; break; } case -3: { status = '企业认证待审核'; break; } case 1: { status = '企业认证成功'; break; } } return status; } /** * 资源启动状态 * @param value */ function resourceStartState(value: string | number | boolean, type = "name"): string { let name = "", icon = "" switch (value) { case "true": case true: case 1: case "1": name = '已开启'; icon = "icon-enabled color-success" break; case null: case false: case "false": case "0": case 0: name = '未开启'; icon = "icon-forbidden color-info" break; default: return '' } if (type === 'name') { return name } else { return icon } } /** * 资源启动状态 * @param value */ function slbState(value: string, openStatus: any, type = "name"): string { let name = "", icon = "" switch (value) { case 'expired': name = '欠费停用'; icon = "icon-forbidden color-danger" break; case 'normal': if (Number(openStatus) === 1) { name = '运行中'; icon = "icon-enabled color-success" } else { name = '已停用'; icon = "icon-forbidden color-info" } break; default: return '-' } if (type === 'name') { return name } else { return icon } } /**调度算法 */ function balanceType(value: string) { switch (value) { case 'rr': return '轮询'; case 'wrr': return '加权轮询'; case 'wlc': return '加权最小连接数'; } } /** * 资源使用状态 * @param value */ function resourceUseState(value: string): string { switch (value) { case 'normal': case '1': return '空闲'; case 'used': case "0": return '已使用'; default: return '' } } function diskType(value: any) { switch (value) { case 'system': case 'SYS': return '系统盘'; case 'data': case 'DATA': return '数据盘'; default: return '系统盘' } } /**存储类型 */ function storageType(value: any) { switch (value) { case 'RBD_HDD': case 'iSCSI_SATA': return '中心存储'; case 'RBD_SSD': case 'iSCSI_SSD': return 'SSD存储'; case 'LVM_SSD': return '本地SSD存储'; case 'LVM_HDD': return '本地中心存储'; } } function abnormalErrorTimeType(value: any) { let type = ''; let time: any = { theDayBeforeYesterday: '前天', yesterday: '昨天', today: '今天', lastWeek: '上周', thisWeek: '本周', theMonthBeforeLastMonth: '上两月', lastMonth: '上月', thisMonth: '本月', theYearBeforeLastYear: '前年', lastYear: '去年', thisYear: '今年', nearlyFourHours: '最近四小时', nearlyDay: '最近一天', nearlyWeek: '最近一周', nearlyTwoMonth: '近两月', nearlyMonth: '最近一月', nearlyHalfYear: '最近半年', nearlyYear: '最近一年', nearlyTwelveMonth: '最近十二月', custom: '自定义' }; return time[value] || value; } /**快照类型 */ function snapType(value: string) { if (!value) { return '全部' } let obj: any = { 'fixed': '固定快照', 'auto': '自动快照' } return obj[value]; } /** * 主机电源状态 * @param value * @param type */ function vmPowerStatus(value: any, type: any = "name") { let name = "", icon = ""; switch (value) { case "init": { name = "初始化"; icon = "icon-loading-arrow loading-icon"; break; } case "halted": { name = "已关机"; icon = "icon-halted color-info"; break } case "halteding": { name = "关机中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "starting": { name = "开机中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "resetart": { name = "重启中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "restarting": { name = "重装中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "config": { name = "配置中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "running": { name = "运行中"; icon = "icon-enabled color-success"; break; } case "unavailable": { name = "暂不可用"; icon = "icon-deep-error color-danger"; break; } case "stop": { name = "到期停机"; icon = "icon-forbidden color-danger"; break; } case "arrears": { name = "欠费停机"; icon = "icon-forbidden color-danger"; break; } } if (type === 'name') { return name; } else { return icon } } /** * RDS电源状态 * @param value * @param type */ function rdsPowerStatus(value: any, type: any = "name") { let name = "", icon = ""; switch (value) { case "creating": { name = "创建中"; icon = "icon-loading-arrow loading-icon"; break; } case "stopped": { name = "已停止"; icon = "icon-halted color-info"; break } case "stopping": { name = "停止中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "startuping": { name = "启动中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "restoring": { name = "恢复中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "backuping": { name = "备份中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "upgrading": { name = "升级中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "switching": { name = "主从切换中"; icon = "icon-loading-arrow loading-icon color-info"; break } case "running": { name = "运行中"; icon = "icon-enabled color-success"; break; } case "update_waiting": { name = "等待更新信息"; icon = "icon-forbidden color-info"; break; } case 'failure': case "unavailable": { name = "暂不可用"; icon = "icon-forbidden color-danger"; break; } case "stop": { name = "到期停机"; icon = "icon-forbidden color-danger"; break; } case "arrears": { name = "欠费停机"; icon = "icon-forbidden color-danger"; break; } } if (type === 'name') { return name; } else { return icon } } function priceFormat(price: number) { price = +price; let priceChange = price.toFixed(2); return '¥ ' + priceChange } /**格式化价格 */ function money(number: number) { return priceFormat(number).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") } /** * 获取单价 * @param price * @param count */ function unitPrice(price: number, count: any = 1, billing: any = "ElasticByDay", duration: any = 1) { let unitPrice = priceFormat(price / count/ duration) + '/' + (billing === 'ElasticByDay' ? '天' : '月'); return unitPrice } function discount(value: any) { value = +value; value = (value * 10).toFixed(1); return value; } /**保留小数,默认两位 */ function toFixed(value: string | number, num: number = 2) { return +value ? Number(value).toFixed(num) : value } /** * 任务状态 */ function taskStatus(status: number | string) { switch (+status) { case 0: { return '未生效'; } case 1: { return '已生效'; } case 2: { return '已取消'; } case 3: { return '生效失败'; } } return status; } /** * 操作状态 */ function optStatus(status: any, type: any = 'text') { let icon: any = ""; let text: any = ""; switch (status) { case 'success': { text = '成功'; icon = "icon-circle-success color-success"; break; } case 'error': { text = '失败'; icon = "icon-deep-error color-danger"; } } if (type === 'text') { return text; } else { return icon; } } /**磁盘类型 */ function diskTypeConfig(value: any) { return Constant.diskType[value] ? Constant.diskType[value] : value; } function accountTypeRds(value: any) { let type: any = { general: '普通权限账号', advanced: '高级权限账号', custom: '自定义权限账号' }; return value ? type[value] || value : ''; } export default { desensitization, // 脱敏 locationPipe, // 节点可用区&池编号等 ipTypePipe, // IP类型 billingMethodPipe, // 计费模式 diskMediumType, // 磁盘介质类型 system, // 系统信息 network, // 网络类型 bandwidthModel, // 带宽类型 resourceNamePipe, // 资源名称 newResourceNamePipe, // 资源名称 resourceBillingState, // 资源计费状态 resourceUseState, diskType, vmPowerStatus, unitPrice, priceFormat, bwBillingMethodPipe, StarInfoPipe, asyncTaskStatus, discount, wafType, storageType, snapType, unitBwPipe, getRdsCategory, taskStatus, resourceStartState, slbState, toFixed, abnormalErrorTimeType, balanceType, optStatus, money, realNameState, // 实名认证 verifyStatusNew, // 实名认证 diskTypeConfig, rdsPowerStatus, accountTypeRds, }