@simba088/mat-ui
Version:
基于Vue和ElementUi的PC组件库
618 lines (578 loc) • 19.8 kB
JavaScript
/* eslint-disable*/
//浏览器相关
const inBrowser = typeof window !== 'undefined';
const UA = inBrowser && window.navigator.userAgent.toLowerCase();
const isIE = UA && /msie|trident/.test(UA);
const isIE9 = UA && UA.indexOf('msie 9.0') > 0;
const isEdge = UA && UA.indexOf('edge/') > 0;
const isAndroid = UA && UA.indexOf('android') > 0;
const isIOS = UA && /iphone|ipad|ipod|ios/.test(UA);
const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge;
const isWx = UA && /MicroMessenger/i.test(UA);
const isXysApp = UA && /mucxiaoyusan/i.test(UA);
const isKcbApp = UA && /muckacabao/i.test(UA);
const isApp = isXysApp || isKcbApp;
const isMobileDevice = (UA && /phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone/i).test(UA);
export const ua = {
isIE,
isMobile: isMobileDevice,
isIE9,
isEdge,
isAndroid,
isIOS,
isChrome,
isWx,
isApp,
isXysApp,
isKcbApp
};
export const isMucWebAutoTester = /MucWebAutoTester/.test(window.navigator.userAgent);
//工具函数
export function findIndex(ary, fn) {
if (ary.findIndex) {
return ary.findIndex(fn);
}
/* istanbul ignore next */
let index = -1;
/* istanbul ignore next */
ary.some(function (item, i, ary) {
const ret = fn.call(this, item, i, ary);
if (ret) {
index = i;
return ret;
}
});
/* istanbul ignore next */
return index;
}
export function deepAssign(to, from) {
for (const key in from) {
if (!to[key] || typeof to[key] !== 'object') {
to[key] = from[key];
} else {
deepAssign(to[key], from[key]);
}
}
}
export function isArray(args) {
if (Array.isArray) {
return Array.isArray(args);
}
return Object.prototype.toString.call(args) === '[object Array]';
}
export function isString(args) {
return Object.prototype.toString.call(args) === '[object String]';
}
export function isError(args) {
return Object.prototype.toString.call(args) === '[object Error]';
}
export function isNumber(args) {
return Object.prototype.toString.call(args) === '[object Number]';
}
export function isObject(args) {
return Object.prototype.toString.call(args) === '[object Object]';
}
export function isNull(args) {
return args === null;
}
export function isUndefined(args) {
return args === undefined;
}
// 组装页面数据 获取gid,承保,核保都会用
export function getApiData(pageData) {
const {
attrModules = {},
createCommon,
plan,
priceInfo,
healthConfirm,
antiMoneyInfo,
proposalNo,
promotion,
paperList
} = deepCopy(pageData);
const {
applicant,
insureds,
beneficiaries,
renewalBank,
information,
otherinfomation
} = attrModules;
const outData = {};
const bundle = inObj => {
let certificate = null;
let socialIns = null;
let resident = null;
let office = null;
const outObj = {};
for (const key in inObj) {
const defaultValue = inObj[key].defaultValue;
const required = inObj[key].required;
const display = inObj[key].display;
// 如是必传或者是显示的元素
if (required == 1 || display == 1) {
if (key == 'idType' || key == 'id' || key == 'idValidType' || key == 'idValidEndTime' || key == 'idValidStartTime') {
certificate = certificate || {};
certificate[key] = defaultValue;
} else if (key == 'socialInsAddr' || key == 'socialInsDetail' || key == 'socialInsPost' || key == 'socialInsTel') {
socialIns = socialIns || {};
socialIns[key] = defaultValue;
} else if (key == 'residentAddr' || key == 'residentDetail' || key == 'residentPost' || key == 'residentTel') {
resident = resident || {};
resident[key] = defaultValue;
} else if (key == 'officeAddr' || key == 'officeDetail' || key == 'officePost' || key == 'officeTel') {
office = office || {};
office[key] = defaultValue;
} else {
outObj[key] = defaultValue;
}
}
}
if (certificate) outObj.certificate = certificate;
if (socialIns) outObj.socialIns = socialIns;
if (resident) outObj.resident = resident;
if (office) outObj.office = office;
return outObj;
};
const bundleAml = inObj => {
if (antiMoneyInfo) {
for (let i = 0; i < antiMoneyInfo.length; i++) {
const {
backId: faceIdentityPicId,
frontId: backIdentityPicId,
id,
name,
status
} = antiMoneyInfo[i];
if (inObj.id.defaultValue == id && inObj.cname.defaultValue == name) {
return { faceIdentityPicId, backIdentityPicId };
}
}
return null;
}
};
// 处理url参数
if (createCommon && createCommon.urlQuery) {
const { urlQuery, include = [] } = createCommon;
outData.common = getApiCommon({ urlQuery, include });
}
// 核保id添加到common中
if (proposalNo) {
outData.common = outData.common || {};
outData.common.policy = {};
outData.common.policy.proposalNo = proposalNo;
}
// 处理投保人数据
if (applicant || renewalBank) {
outData.applicant = {};
if (applicant) outData.applicant = bundle(applicant);
if (renewalBank) outData.applicant.renewalBank = bundle(renewalBank); // 银行卡信息合并到投保人中
// 处理投保人反洗钱
if (applicant && antiMoneyInfo) {
const amlInfo = bundleAml(applicant);
if (amlInfo) outData.applicant.antiMoneyInfo = amlInfo;
}
// 处理投保人智核数据
if (paperList) {
for (let i = 0; i < paperList.length; i++) {
const { memDetail = {}, id } = paperList[i];
// 判断是不是智核投保人
if (Number(memDetail.isInsured) === 1) {
continue;
}
// 判断智核的投保人姓名是否有值,如果有值必须等于投保人模块中的cname值
if (memDetail.cname !== '' && outData.applicant.cname !== undefined && memDetail.cname !== outData.applicant.cname) {
break;
}
// 判断智核的投保人证件号是否有值,如果有值必须等于投保人模块中的证件号
if (memDetail.cert !== '' && outData.applicant.id !== undefined && memDetail.cert !== outData.applicant.id) {
break;
}
if (id === undefined) {
break;
}
outData.applicant.intelCheck = { subPaperId: id };
break;
}
}
}
// 处理受益人数据
let formatBeneficiaries = null;
if (beneficiaries) {
formatBeneficiaries = [];
// 循环所有被保人的受益模块
for (let i = 0; i < beneficiaries.length; i++) {
const person = beneficiaries[i];
let dutyBeneficiaries = null;
// 循环一个被保人的受益人模块的所有责任
for (let j = 0; j < person.length; j++) {
const [dutys, benes] = person[j]; //责任
// 如果责任下的第一个责任项存在
if (dutys) {
const { type, dutyId } = dutys;
if (type && (type.display == 1 || type.required == 1)) {
dutyBeneficiaries = dutyBeneficiaries || {};
dutyBeneficiaries[dutyId.defaultValue] = bundle(dutys);
// 如果责任下的第二个责任项存在,且责任选择为指定受益人
if (benes && type.defaultValue == 2) {
// 第二个责任项是顺位和受益人相关
const newBenes = [];
// 循环一个责任中含有指定的所有顺位
for (let k = 0; k < benes.length; k++) {
const benesPriority = benes[k];
// 循环一个顺位中所有受益人
for (let l = 0; l < benesPriority.length; l++) {
const oneBene = benesPriority[l];
let newOneBene = {};
// 对一个受益人的key-value进行重新组装
newOneBene = bundle(oneBene);
newOneBene.order = k + 1; // 第几个顺位
newOneBene.indexPerOrder = l + 1; // 第几个受益人
newBenes.push(newOneBene);
}
}
dutyBeneficiaries[dutyId.defaultValue].beneficiaries = newBenes;
}
}
}
}
dutyBeneficiaries && formatBeneficiaries.push(dutyBeneficiaries);
}
}
// 处理被保人数据
if (insureds || plan || healthConfirm || formatBeneficiaries || priceInfo && priceInfo.priceData) {
outData.insureds = [];
const forArr = insureds || [''];
for (let i = 0; i < forArr.length; i++) {
const insured = forArr[i];
let newInsured = null;
const obj = {};
if (insured) {
newInsured = bundle(insured);
// 处理投保人反洗钱
if (antiMoneyInfo) {
const amlInfo = bundleAml(insured);
if (amlInfo) newInsured.antiMoneyInfo = amlInfo;
}
}
if (newInsured) obj.insured = newInsured;
if (plan) obj.plan = plan;
if (healthConfirm) obj.healthConfirm = healthConfirm;
if (formatBeneficiaries) obj.dutyBeneficiarys = formatBeneficiaries[i];
if (priceInfo && priceInfo.priceData) {
const {
exInsurances,
mainInsurance,
multiPrice,
renewalDiscountFee
} = priceInfo.priceData[i];
obj.buyInfo = { exInsurances, mainInsurance };
if (renewalDiscountFee !== undefined) {
obj.buyInfo.renewalDiscountFee = renewalDiscountFee || 0;
}
if (multiPrice) obj.buyInfo.multiPrice = multiPrice;
}
// 处理被保人智核数据
if (paperList) {
for (let i = 0; i < paperList.length; i++) {
const { memDetail = {}, id } = paperList[i];
// 判断是不是智核被保人
if (Number(memDetail.isInsured) !== 1) {
continue;
}
// 判断智核的被保人姓名是否有值,如果有值必须等于被保人模块中的cname值
if (memDetail.cname !== '' && obj.insured.cname !== undefined && memDetail.cname !== obj.insured.cname) {
continue;
}
// 判断智核的被保人证件号是否有值,如果有值必须等于被保人模块中的证件号
if (memDetail.cert !== '' && obj.insured.id !== undefined && memDetail.cert !== obj.insured.id) {
continue;
}
if (id === undefined) {
continue;
}
obj.insured.intelCheck = { subPaperId: id };
}
}
outData.insureds.push(obj);
}
}
// 处理优惠劵
if (promotion) {
outData.promotion = {};
outData.promotion.coupons = [];
outData.promotion.coupons.push(promotion.couponId);
}
// 处理价格数据
if (priceInfo) {
outData.payInfo = {};
if (promotion && promotion.couponPrice) {
outData.payInfo.discountFee = promotion.couponPrice; //优惠金额
outData.payInfo.payFee = priceInfo.totalPrice - promotion.couponPrice; // 最终支付的价格
} else {
outData.payInfo.discountFee = priceInfo.discountFee; //优惠金额
outData.payInfo.payFee = priceInfo.totalPrice - priceInfo.discountFee; // 最终支付的价格
}
outData.payInfo.totalFee = priceInfo.totalPrice; //总价格
if (priceInfo.secondTotalPrice !== 0 && priceInfo.secondTotalPrice !== undefined) {
// 月付次月价格
outData.payInfo.secondDiscountFee = 0;
outData.payInfo.secondTotalFee = priceInfo.secondTotalPrice;
outData.payInfo.secondPayFee = priceInfo.secondTotalPrice;
}
}
// 处理保险信息
if (information) {
if (information.endTime && information.endTime.defaultValue) {
information.endTime.defaultValue = Number(information.endTime.defaultValue.split('-').join(''));
}
outData.information = bundle(information);
}
// 其他模块信息
if (otherinfomation) {
outData.otherinfomation = bundle(otherinfomation);
}
return outData;
}
/* @param {Function} func The function to debounce.
* @param {number} [wait=0]
* The number of milliseconds to delay; if omitted, `requestAnimationFrame` is
* used (if available).
* @param {Object} [options={}] The options object.
* @param {boolean} [options.leading=false]
* Specify invoking on the leading edge of the timeout.
* @param {number} [options.maxWait]
* The maximum time `func` is allowed to be delayed before it's invoked.
* @param {boolean} [options.trailing=true]
* Specify invoking on the trailing edge of the timeout.
* @returns {Function} Returns the new debounced function.
*/
export function debounce(func, wait, options) {
let lastArgs, lastThis, maxWait, result, timerId, lastCallTime;
let lastInvokeTime = 0;
let leading = false;
let maxing = false;
let trailing = true;
// Bypass `requestAnimationFrame` by explicitly setting `wait=0`.
const useRAF = !wait && wait !== 0 && typeof window.requestAnimationFrame === 'function';
if (typeof func !== 'function') {
throw new TypeError('Expected a function');
}
wait = +wait || 0;
if (isObject(options)) {
leading = !!options.leading;
maxing = 'maxWait' in options;
maxWait = maxing ? Math.max(+options.maxWait || 0, wait) : maxWait;
trailing = 'trailing' in options ? !!options.trailing : trailing;
}
function invokeFunc(time) {
const args = lastArgs;
const thisArg = lastThis;
lastArgs = lastThis = undefined;
lastInvokeTime = time;
result = func.apply(thisArg, args);
return result;
}
function startTimer(pendingFunc, wait) {
if (useRAF) {
window.cancelAnimationFrame(timerId);
return window.requestAnimationFrame(pendingFunc);
}
return setTimeout(pendingFunc, wait);
}
function cancelTimer(id) {
if (useRAF) {
return window.cancelAnimationFrame(id);
}
clearTimeout(id);
}
function leadingEdge(time) {
// Reset any `maxWait` timer.
lastInvokeTime = time;
// Start the timer for the trailing edge.
timerId = startTimer(timerExpired, wait);
// Invoke the leading edge.
return leading ? invokeFunc(time) : result;
}
function remainingWait(time) {
const timeSinceLastCall = time - lastCallTime;
const timeSinceLastInvoke = time - lastInvokeTime;
const timeWaiting = wait - timeSinceLastCall;
return maxing ? Math.min(timeWaiting, maxWait - timeSinceLastInvoke) : timeWaiting;
}
function shouldInvoke(time) {
const timeSinceLastCall = time - lastCallTime;
const timeSinceLastInvoke = time - lastInvokeTime;
// Either this is the first call, activity has stopped and we're at the
// trailing edge, the system time has gone backwards and we're treating
// it as the trailing edge, or we've hit the `maxWait` limit.
return lastCallTime === undefined || timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= maxWait;
}
function timerExpired() {
const time = Date.now();
if (shouldInvoke(time)) {
return trailingEdge(time);
}
// Restart the timer.
timerId = startTimer(timerExpired, remainingWait(time));
}
function trailingEdge(time) {
timerId = undefined;
// Only invoke if we have `lastArgs` which means `func` has been
// debounced at least once.
if (trailing && lastArgs) {
return invokeFunc(time);
}
lastArgs = lastThis = undefined;
return result;
}
function cancel() {
if (timerId !== undefined) {
cancelTimer(timerId);
}
lastInvokeTime = 0;
lastArgs = lastCallTime = lastThis = timerId = undefined;
}
function flush() {
return timerId === undefined ? result : trailingEdge(Date.now());
}
function pending() {
return timerId !== undefined;
}
function debounced(...args) {
const time = Date.now();
const isInvoking = shouldInvoke(time);
lastArgs = args;
lastThis = this;
lastCallTime = time;
if (isInvoking) {
if (timerId === undefined) {
return leadingEdge(lastCallTime);
}
if (maxing) {
// Handle invocations in a tight loop.
timerId = startTimer(timerExpired, wait);
return invokeFunc(lastCallTime);
}
}
if (timerId === undefined) {
timerId = startTimer(timerExpired, wait);
}
return result;
}
debounced.cancel = cancel;
debounced.flush = flush;
debounced.pending = pending;
return debounced;
}
export function throttle(func, wait, options) {
let leading = true;
let trailing = true;
if (typeof func !== 'function') {
throw new TypeError('Expected a function');
}
if (isObject(options)) {
leading = 'leading' in options ? !!options.leading : leading;
trailing = 'trailing' in options ? !!options.trailing : trailing;
}
return debounce(func, wait, {
leading,
trailing,
maxWait: wait
});
}
export function isNative(Ctor) {
return typeof Ctor === 'function' && /native code/.test(Ctor.toString());
}
export function isMobile(value) {
if (!/^1\d{10}$/.test(value)) {
return false;
}
return true;
}
export function formatMobile(val) {
const len = val.length;
if (len > 3 && len < 8) {
val = val.replace(/\s/g, '').replace(/^(.{3})/g, '$1 ');
} else if (len >= 8) {
val = val.replace(/\s/g, '').replace(/^(.{3})(.+)/g, (all, first, second) => {
return `${first} ${second.replace(/(.{4})(?=.)/g, '$1 ')}`;
});
}
return val;
}
export function formatBankCard(val) {
return val.replace(/\s/g, '').replace(/(.{4})(?=.)/g, '$1 ');
}
export function formatFontSize(value) {
// 判断传参是否是数字 不是加上默认单位 'px'
if (value === undefined && value === null) {
return undefined;
}
return (/^\d+(\.\d+)?$/.test(value + '') ? `${value}px` : value
);
}
export function formatTitle(tit) {
if (!tit) return '';
const length = tit.length;
if (length === 2) {
return tit.split('').join('  ');
} else if (length === 3) {
return tit.split('').join(' ');
}
return tit;
}
export function outerWidth(el, includeMargin = true) {
let width = el.offsetWidth;
if (includeMargin) {
const style = el.currentStyle || getComputedStyle(el);
width += parseInt(style.marginLeft || 0) + parseInt(style.marginRight || 0);
}
return width;
}
export function outerHeight(el, includeMargin = true) {
let height = el.offsetHeight;
if (includeMargin) {
const style = el.currentStyle || getComputedStyle(el);
height += parseInt(style.marginTop || 0) + parseInt(style.marginBottom || 0);
}
return height;
}
export function hexToRgb(hex, alpha) {
const shortResult = /^#([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})$/i.exec(hex);
const longResult = /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
let rgb = null;
if (shortResult) {
rgb = {
r: parseInt(shortResult[1] + shortResult[1], 16),
g: parseInt(shortResult[2] + shortResult[2], 16),
b: parseInt(shortResult[3] + shortResult[3], 16)
};
} else if (longResult) {
rgb = {
r: parseInt(longResult[1], 16),
g: parseInt(longResult[2], 16),
b: parseInt(longResult[3], 16)
};
}
if (!rgb) {
return '';
}
if (alpha !== undefined) {
return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alpha})`;
} else {
return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
}
}
// 生成随机字符串
export function randomString(len) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz';
const maxPos = chars.length;
let pwd = '';
len = len || 32;
for (let i = 0; i < len; i++) {
pwd += chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}