js 常用公共类封装

JavaScript013

js 常用公共类封装,第1张

/**

* @用户信息缓存,公共js封装

* @Author maguangbo

* @Date 2019-07-18

* @Email 815186911@qq.com

* **/

export default (function() {

var com = {}

com.setCurrentUserInfo = function(userInfo) {

return uni.setStorageSync('userInfo', userInfo)

}

com.getCurrentuserInfo = function() {

return uni.getStorageSync('userInfo')

}

com.getToken = function(){

return uni.getStorageSync('tokens')

}

com.setToken = function(content){

return uni.setStorageSync('tokens',content)

}

var tokenInfos ={

"token": "",

"expiredTime": ""

}

com.getTokens = function(){

if(uni.getStorageSync('tokens')){

var tInfos = uni.getStorageSync('tokens')

tInfos.token &&(tokenInfos.token = tInfos.token)

tInfos.expiredTime &&(tokenInfos.expiredTime = tInfos.expiredTime)

}

return tokenInfos

}

var userInfo = {

"id": '',

"userName": '',

"nickName": '',

"email": '',

"phone": '',

"identity": '',

"roleId": '',

"apiUserId": '',

"apiKey": '',

"createDate": ''

}

com.getUserInfos =function(){

if(uni.getStorageSync('userInfo')){

var infos = uni.getStorageSync('userInfo')

infos.id &&(userInfo.id = infos.id)

infos.userName &&(userInfo.userName = infos.userName)

infos.nickName &&(userInfo.nickName = infos.nickName)

infos.email &&(userInfo.email = infos.email)

infos.phone &&(userInfo.phone = infos.phone)

infos.apiUserId &&(userInfo.apiUserId = infos.apiUserId)

infos.apiKey &&(userInfo.apiKey = infos.apiKey)

infos.createDate &&(userInfo.createDate = infos.createDate)

return userInfo

}

return false

}

com.toast = (title, duration=1500, mask=false, icon='none')=>{

if(Boolean(title) === false){

return

}

uni.showToast({

title,

duration,

mask,

icon

})

}

return com

}())

这是一个常用的公共用户信息封装实体类,个人感觉很方便,如果有更好的的欢迎大佬指教!

data的规则中设置表达式时可以调用当前model中的定义的函数,调用方式可以用$model.函数名

如:js中定义了一个fCodeRule函数

123456

Model.prototype.fCodeRule = function(value) {if (value === 'test') {return value } elsereturn 'testabc'}

规则中可以通过$model.fCodeRule($row.val(“fName”))调用

如果是定义了一个ADM格式的公共js,要在规则中调用公共js中的数据,不能只引用js文件就可以,需要

1.引用公共的js文件

2.在model的构造函数中定义变量

3.在规则中通过$model.定义的变量.函数名

如下:

define(function(require) {var $ = require("jquery") var justep = require("$UI/system/lib/justep") var OA = require("$UI/OA/common/js/pub")var Model = function() {this.callParent() this.OA = OA }

规则中通过$model.OA.XXX()调用

共用js里的路径应该用绝对路径,比方说js文件夹是根目录中的文件夹,就这样:

document.write('<script src="/js/XXX.js" type="text/javascript"></script>')

也可以加上网址(这样的话可以实现跨网站调用):

document.write('<script src="http://xxx.xxx.xxx/js/XXX.js" type="text/javascript"></script>')