/* * @Date: 2022-01-19 16:39:51 * @LastEditors: JinxChen * @LastEditTime: 2022-04-13 16:22:59 * @FilePath: \AntpayFrontEnd\src\utils\index.js * @description: 工具类 */ import store from "@/store"; /** * 判断是否为空 */ export function isNull(o) { if (o === null || o === undefined || o === '') { return true; // eslint-disable-next-line } else if (Array.prototype.isPrototypeOf(o) && o.length === 0) { return true; // eslint-disable-next-line } else if (Object.prototype.isPrototypeOf(o) && Object.keys(o).length === 0) { return true; } return false; } /** * 判断是否为非空 */ export function isNotNull(o) { return !isNull(o); } // 判断用户是否是支付宝浏览器 export function isAlipayBrowser(userAgent) { if (/AlipayClient/.test(userAgent)) { console.log("当前浏览器是支付宝浏览器"); return true; } else { console.log("当前浏览器是非支付宝浏览器"); return false; } } // 判断用户的设备是否是android export function isAndroid(userAgent) { if (userAgent.indexOf('Android') > -1 || userAgent.indexOf('Linux') > -1) { console.log("当前用户设备是android"); return true; } else if (userAgent.indexOf('iPhone') > -1) { console.log("当前用户设备是ios"); return false; } } // 初始化分期数数组, 1. 如果 分数数组面 包含有 '1' 并且还有其它'12' '24'两个分期, 则把 '1'删掉 // 2. 否则直接返回该数组 export function intCount(array) { let index = array.some(item => item === '1'); if( index) { // 大于-1表示存在 let newArray = array.splice(1, 2); return newArray; // 存在就删除 } else { return array; } } // 通过接口返回的数组判读是否显示花呗或者支付宝,, 1. 1 12 24 都存在则支付宝花呗全都显示 // 2. 只有 1 则 只显示支付宝 // 3. 没有 1 则显示花呗 export function isShowAlipay(array) { let index = array.indexOf('1'); if(index > -1 && array.length === 1 ) { return true; } else if (index > -1 && array.length > 2) { return true; } else { return false; } } export function isShowAntpay(array) { let index = array.indexOf('1'); if(index < 0 && array.length >= 1) { return true; } else { return false; } } // 将传入内容的某个文字全部替换成指定的文字 /** * * @param {*} target 目标字符串 * @param {*} searchValue 要搜索和替换的子串 * @param {*} replacement 替换的新字符串 * @returns */ export function replaceAll(target, searchValue, replacement) { // 应用类型,目前1 是健康好蕴,其他则不替换 const appTypeList = ['1']; const appType = store.getters.appType; if(appTypeList.indexOf(appType) > -1) { if(isNotNull(target) && isNotNull(searchValue)) { if(target.includes(searchValue)) { return target.replace(new RegExp(searchValue, 'g'), replacement); } else { return target } } else { return target } } else { return target } }