EUtil - 常规工具

提供读写缓存数据、获取URL参数、判断设备类型、窗口尺寸、UUID、isXXX、提示、设置等待状态、ajax等公用方法

init()

调用windowResizeHandler,监听了windowresize事件

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对象

get(key) ⇒ null | *

根据key获取元素变量的value,如果没有指定的key将返回null

Param Type Description
key string key

set(key, value)

向全局变量中添加值

Param Type Description
key string key
value object value

has(key) ⇒ boolean

查找元素变量中是否有指定key

Returns: boolean - true/false

Param Type Description
key string key

remove(key)

根据key删除元素缓存的value

Param Type Description
key string key

getURLParam(paramName) ⇒ string | null

根据参数名称从url中获取参数值,如没有返回null

Param Type Description
paramName string 参数名

isMobileDevice() ⇒ boolean

根据窗口判断是否属于移动设备(宽度小于lg)

Returns: boolean - true/false

isDesktopDevice() ⇒ boolean

根据窗口判断是否属于桌面设备(宽度大于lg)

Returns: boolean - true/false

getViewPort() ⇒ object

获取浏览器窗口尺寸

Returns: object - eg: {width: 1920, height: 1080}

isInResponsiveRange(mode) ⇒ boolean

检查当前设备是否为指定类型

Returns: boolean - true/false

Param Type Description
mode string 类型(e.g: desktop, desktop-and-tablet, tablet, tablet-and-mobile, mobile)

detectIE() ⇒ boolean | number

检查是否为IE系列浏览器,如果是则返回版本,如果不是返回false

getUniqueID(prefix) ⇒ string

生成一个指定前缀的 unique ID

Param Type Description
prefix string {string} 前缀

getBreakpoint(mode) ⇒ number

获取在breakpoints中定义的响应式前缀对应的width

Returns: number - width

Param Type Description
mode string 响应式模式前缀 (e.g: xxl, xl, lg, md, sm)

getRandomInt(min, max) ⇒ number

获取指定最小和最大范围内随机生成的整数值

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 目标元素,支持选择器ElementjQuery对象
options object 选项

scrollUpdate(element)

更新滚动条插件PerfectScrollbar

Param Type Description
element element | string 目标元素,支持选择器ElementjQuery对象

scrollUpdateAll(elements)

更新页面中所有滚动条插件PerfectScrollbar

Param Type Description
elements element | string 目标元素,支持选择器ElementjQuery对象

scrollDestroy(element)

销毁滚动条插件PerfectScrollbar

Param Type Description
element element | string 目标元素,支持选择器ElementjQuery对象

scrollTo(target, top, speed)

将运动条滚动到指定位置

Param Type Description
target element | string 目标元素,支持选择器ElementjQuery对象
top number 滚动到什么位置
speed number 动画时长,默认500ms

isNull(obj) ⇒ boolean

判断对象是否为undefinednull,反方法isNotNull(obj),如果要判断字符串是否为空请用isBlank(str)`

Returns: boolean - true/false

Param Type Description
obj object 待检查对象

isNotNull(obj) ⇒ boolean

判断对象是否不为undefinednull,如果要判断字符串是否为空请用isNotBlank(str)

Returns: boolean - true/false

Param Type Description
obj object 待检查对象

isBlank(str) ⇒ boolean

判断字符串是否为undefinednull'',反方法isNotBlank(str)

Returns: boolean - true/false

Param Type Description
str string 待检查字符串

isNotBlank(str) ⇒ boolean

判断字符串是否不为undefinednull''

Returns: boolean - true/false

Param Type Description
str string 待检查字符串

isFunction(fn) ⇒ boolean

检查对象是否为Function类型

Returns: boolean - true/false

Param Type Description
fn object 待检查function

isNumber(obj) ⇒ boolean

判断是否是数字,如果不是数字类型尝试转为数字类型,如果发生异常则返回false(方法不会抛出异常)

Returns: boolean - true/false

Param Type Description
obj object 待检查number

isArray(obj) ⇒ boolean

检查对象是否是数组类型

Returns: boolean - true/false

Param Type Description
obj object 待检查对象

isString(obj) ⇒ boolean

判断是否为字符串类型

Returns: boolean - true/false

Param Type Description
obj object 待检查对象

subStr(str, length) ⇒ string

截取指定长度字符串,如果超出在后面拼接...

Returns: string - 截取后字符串

Param Type Description
str string 字符串
length number 长度

isTopPage() ⇒ boolean

是否是顶级页面,根据最上层页面(window.top)是否是自己

Returns: boolean - true/false

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 目标元素,支持选择器ElementjQuery对象

closeWait(element)

移除等待遮罩

Param Type Description
element element | string 目标元素,支持选择器ElementjQuery对象

setButtonWait(element)

设置按钮处理中状态,为了防止等待状态时按钮出现多个图标,如按钮有图标,会隐藏使用处理中图标代替;调用offButtonWait()后会恢复原有图标

Param Type Description
element element | string 目标元素,支持选择器ElementjQuery对象

offButtonWait(element)

取消按钮处理中状态

Param Type Description
element element | string 目标元素,支持选择器ElementjQuery对象

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)

当后端响应HttpStatus500时,解析错误类型并做相应处理

Param Type
XMLHttpRequest object
textStatus object
errorThrown object