@mucfe/matui
Version:
基于Vue和ElementUi的PC组件库
511 lines (467 loc) • 14.3 kB
JavaScript
/* eslint-disable*/
import copy from './fastcopy/index'
export function deepCopy(obj) {
if (!obj) return obj;
return copy(obj)
}
//浏览器相关
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
}
export function getFormatedImg (imgArr = []) {
let formatedImg = []
let strRegex = new RegExp('https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]')
for (let i = 0; i < imgArr.length; i++) {
let currItem = imgArr[i]
if (strRegex.test(currItem)) {
formatedImg.push(currItem.split('?')[0])
} else {
return
}
}
return formatedImg
}
// 组装页面数据 获取gid,承保,核保都会用
export function getApiData (pageData) {
let { attrModules = {}, plan } = JSON.parse(JSON.stringify(pageData))
const { applicant, insureds, hcpics, hctext, makeup, apphctext, apphcpics } = attrModules
const outData = {}
const bundle = (inObj) => {
let certificate = null
let socialIns = null
let resident = null
let office = null
let outObj = {}
let companyCertificate = {}
let companyAddress = {}
for (const key in inObj) {
let defaultValue = inObj[key]['defaultValue']
const required = inObj[key]['required']
const display = inObj[key]['display']
if (isArray(defaultValue)) {
let tranformedVakl = getFormatedImg(defaultValue)
if (tranformedVakl) {
defaultValue = tranformedVakl
}
}
// 如是必传或者是显示的元素
// 如是必传或者是显示的元素
if (Number(required) === 1 || Number(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 if (key === 'companyIdType' || key === 'companyId') {
companyCertificate = companyCertificate || {}
companyCertificate[key] = defaultValue
} else if (key === 'detail') {
companyAddress = companyAddress || {}
companyAddress[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
}
// 待客核保plan放在了attrmodules中
if (attrModules.plan) {
plan = bundle(attrModules.plan)
}
// 处理投保人数据
if (applicant) {
outData.applicant = {}
if (applicant) outData.applicant = bundle(applicant)
}
// 处理被保人数据
if (insureds || plan) {
outData.insureds = []
let forArr = insureds || ['']
for (let i = 0; i < forArr.length; i++) {
let insured = forArr[i]
let newInsured = null
let obj = {}
if (insured) {
newInsured = bundle(insured)
}
if (newInsured) obj.insured = newInsured
if (plan) obj.plan = plan
outData.insureds.push(obj)
}
}
// 被保人补充说明
if (hctext) {
outData.hctext = bundle(hctext)
}
// 投保人补充说明
if (apphctext) {
outData.apphctext = bundle(apphctext)
}
// 被保人补充图片
if (hcpics) {
outData.hcpics = bundle(hcpics)
}
// 投保人补充图片
if (apphcpics) {
outData.apphcpics = bundle(apphcpics)
}
//处理补充资料
if (makeup) {
outData.makeup = {}
let forArr = makeup || ['']
for (let i = 0; i < forArr.length; i++) {
let makeupItem = forArr[i]
let newItem = null
if (makeupItem) {
newItem = bundle(makeupItem)
}
if (newItem) {
outData.makeup = Object.assign(outData.makeup, newItem)
}
}
}
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
}