EUtil - 常规工具
提供读写缓存数据、获取URL参数、判断设备类型、窗口尺寸、UUID、isXXX、提示、设置等待状态、ajax等公用方法
init()
调用windowResizeHandler
,监听了window
的resize
事件
addResizeHandler(callback)
添加窗口大小调整回调,无需考虑防抖,已经在windowResizeHandler
统一处理
Param | Type | Description |
---|---|---|
callback | function | 回调函数 |
removeResizeHandler(callback)
移除窗口大小调整回调
Param | Type | Description |
---|---|---|
callback | function | 回调函数 |
runResizeHandlers()
触发窗口大小调整事件,会调用resizeHandlers
中所有回调
data(element)
全局变量,一般用于存放一些与元素绑定的变量
Param | Type | Description |
---|---|---|
element | object | html对象或jquery对象 |
null
| *
get(key) ⇒ 根据key
获取元素变量的value
,如果没有指定的key
将返回null
Param | Type | Description |
---|---|---|
key | string | key |
set(key, value)
向全局变量中添加值
Param | Type | Description |
---|---|---|
key | string | key |
value | object | value |
boolean
has(key) ⇒ 查找元素变量中是否有指定key
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
key | string | key |
remove(key)
根据key
删除元素缓存的value
Param | Type | Description |
---|---|---|
key | string | key |
string
| null
getURLParam(paramName) ⇒ 根据参数名称从url中获取参数值,如没有返回null
Param | Type | Description |
---|---|---|
paramName | string | 参数名 |
boolean
isMobileDevice() ⇒ 根据窗口判断是否属于移动设备(宽度小于lg)
boolean
isDesktopDevice() ⇒ 根据窗口判断是否属于桌面设备(宽度大于lg)
object
getViewPort() ⇒ 获取浏览器窗口尺寸
Returns: object
- eg: {width: 1920, height: 1080}
boolean
isInResponsiveRange(mode) ⇒ 检查当前设备是否为指定类型
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
mode | string | 类型(e.g: desktop, desktop-and-tablet, tablet, tablet-and-mobile, mobile) |
boolean
| number
detectIE() ⇒ 检查是否为IE系列浏览器,如果是则返回版本,如果不是返回false
string
getUniqueID(prefix) ⇒ 生成一个指定前缀的 unique ID
Param | Type | Description |
---|---|---|
prefix | string | {string} 前缀 |
number
getBreakpoint(mode) ⇒ 获取在breakpoints
中定义的响应式前缀对应的width
Returns: number
- width
Param | Type | Description |
---|---|---|
mode | string | 响应式模式前缀 (e.g: xxl, xl, lg, md, sm) |
number
getRandomInt(min, max) ⇒ 获取指定最小和最大范围内随机生成的整数值
Param | Type | Description |
---|---|---|
min | number | 最小 |
max | number | 最大 |
transitionEnd(element, callback)
css3 动画结束事件
Param | Type | Description |
---|---|---|
element | element | 目标元素 |
callback | function | 回调函数 |
scrollInit(element, options)
初始化滚动条插件PerfectScrollbar
Param | Type | Description |
---|---|---|
element | element | string | 目标元素,支持选择器 、Element 、jQuery对象 |
options | object | 选项 |
scrollUpdate(element)
更新滚动条插件PerfectScrollbar
Param | Type | Description |
---|---|---|
element | element | string | 目标元素,支持选择器 、Element 、jQuery对象 |
scrollUpdateAll(elements)
更新页面中所有滚动条插件PerfectScrollbar
Param | Type | Description |
---|---|---|
elements | element | string | 目标元素,支持选择器 、Element 、jQuery对象 |
scrollDestroy(element)
销毁滚动条插件PerfectScrollbar
Param | Type | Description |
---|---|---|
element | element | string | 目标元素,支持选择器 、Element 、jQuery对象 |
scrollTo(target, top, speed)
将运动条滚动到指定位置
Param | Type | Description |
---|---|---|
target | element | string | 目标元素,支持选择器 、Element 、jQuery对象 |
top | number | 滚动到什么位置 |
speed | number | 动画时长,默认500ms |
boolean
isNull(obj) ⇒ 判断对象是否为undefined
或null
,反方法isNotNull(obj),如果要判断字符串是否为空请用
isBlank(str)`
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
obj | object | 待检查对象 |
boolean
isNotNull(obj) ⇒ 判断对象是否不为undefined
或null
,如果要判断字符串是否为空请用isNotBlank(str)
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
obj | object | 待检查对象 |
boolean
isBlank(str) ⇒ 判断字符串是否为undefined
、null
或''
,反方法isNotBlank(str)
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
str | string | 待检查字符串 |
boolean
isNotBlank(str) ⇒ 判断字符串是否不为undefined
、null
或''
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
str | string | 待检查字符串 |
boolean
isFunction(fn) ⇒ 检查对象是否为Function类型
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
fn | object | 待检查function |
boolean
isNumber(obj) ⇒ 判断是否是数字,如果不是数字类型尝试转为数字类型,如果发生异常则返回false
(方法不会抛出异常)
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
obj | object | 待检查number |
boolean
isArray(obj) ⇒ 检查对象是否是数组类型
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
obj | object | 待检查对象 |
boolean
isString(obj) ⇒ 判断是否为字符串类型
Returns: boolean
- true/false
Param | Type | Description |
---|---|---|
obj | object | 待检查对象 |
string
subStr(str, length) ⇒ 截取指定长度字符串,如果超出在后面拼接...
Returns: string
- 截取后字符串
Param | Type | Description |
---|---|---|
str | string | 字符串 |
length | number | 长度 |
boolean
isTopPage() ⇒ 是否是顶级页面,根据最上层页面(window.top)是否是自己
alert(config, callback)
弹出提示(会查找顶级页面然后调用SweetAlert2
中的fire()
)
Param | Type | Description |
---|---|---|
config | object | option |
callback | function | 回调 |
alertInfo(title, subtitle)
弹出提示(级别: info-信息;会查找顶级页面然后调用SweetAlert2
中的fire()
)
Param | Type | Description |
---|---|---|
title | string | 标题 |
subtitle | string | 副标题 |
alertSuccess(title, subtitle)
弹出提示(级别: success-成功;会查找顶级页面然后调用SweetAlert2
中的fire()
)
Param | Type | Description |
---|---|---|
title | string | 标题 |
subtitle | string | 副标题 |
alertWarning(title, subtitle)
- 弹出提示(级别: warning-警告;会查找顶级页面然后调用
SweetAlert2
中的fire()
)
Param | Type | Description |
---|---|---|
title | string | 标题 |
subtitle | string | 副标题 |
alertError(title, subtitle)
弹出提示(级别: error-错误;会查找顶级页面然后调用SweetAlert2
中的fire()
)
Param | Type | Description |
---|---|---|
title | string | 标题 |
subtitle | string | 副标题 |
alertConfirm(title, subtitle, okCallback, cancelCallback)
询问提示(会查找顶级页面然后调用SweetAlert2
中的fire()
)
Param | Type | Description |
---|---|---|
title | string | 标题 |
subtitle | string | 副标题 |
okCallback | function | 点击确定回调 |
cancelCallback | function | 点击取消回调 |
openWait(element)
显示等待遮罩
Param | Type | Description |
---|---|---|
element | element | string | 目标元素,支持选择器 、Element 、jQuery对象 |
closeWait(element)
移除等待遮罩
Param | Type | Description |
---|---|---|
element | element | string | 目标元素,支持选择器 、Element 、jQuery对象 |
setButtonWait(element)
设置按钮处理中状态,为了防止等待状态时按钮出现多个图标,如按钮有图标,会隐藏使用处理中图标代替;调用offButtonWait()
后会恢复原有图标
Param | Type | Description |
---|---|---|
element | element | string | 目标元素,支持选择器 、Element 、jQuery对象 |
offButtonWait(element)
取消按钮处理中状态
Param | Type | Description |
---|---|---|
element | element | string | 目标元素,支持选择器 、Element 、jQuery对象 |
ajax(option)
封装jquery.ajax方法,提供了一些自定义属性以提高开发效率增加用户体验;默认使用post
请求并且dataType: 'json'
如果dataType: 'json'
或响应信息为object
类型,得到响应信息后尝试解析数据是否为成功并提示;如不需要上述处理逻辑可将option.needAlert
设置为false
得到后端响应后,取消option.wait
等待状态,当HttpStatus不为200时,如option.error
已存在则调用用户传入回调,如不存在则调用EUtil.ajaxErrorCallback(XMLHttpRequest, textStatus, errorThrown)
Param | Type | Description |
---|---|---|
option | object | 参数 |
option.wait | string | ajax执行中需要设置等待区域的选择器 |
option.needAlert | boolean | 服务器响应后是否弹出提示 |
handleResult(res, callback)
解析响应码并提示message
,
响应码规则请参考响应码
Param | Type | Description |
---|---|---|
res | object | 响应数据 |
callback | function | null | 回调 |
handleResult~handleTip(method, title, subtitle)
弹出提示
Kind: inner method of handleResult
Param | Type | Description |
---|---|---|
method | string | 方法 eg:successTip、infoTip、warnTip、errorTip |
title | string | 标题 |
subtitle | string | 副标题,如果为空则不进行提示 |
ajaxErrorCallback(XMLHttpRequest, textStatus, errorThrown)
当后端响应HttpStatus
为500
时,解析错误类型并做相应处理
Param | Type |
---|---|
XMLHttpRequest | object |
textStatus | object |
errorThrown | object |