js 常用公共类封装

JavaScript017

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

}())

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

楼上正解。

1.页面A、页面B调用了common.js,并同时打开

2.页面B先加载完毕,

开始请求服务器操作()

{

2.1 判断某个cookie(js写入的cookie)的值,若为1表示其他页面已经请求,若不存在或者为0表示未请求

2.2cookie值为0,请求服务器,并用js写入cookie值1

}

服务器端,页面加载的时候,初始化js(例如删除、修改值)写入的cookie

页面A仍按上述流程执行