w-vue-middle
Version:
统一公共服务组件
123 lines (115 loc) • 2.93 kB
JavaScript
/*
* @Author: Jason Liu
* @Date: 2022-08-26 10:50:38
* @Desc:
*/
import Vue from 'vue'
// localstorage封装
window.$storage = Vue.prototype.$storage = {
userInfo: {},
parameter: {},
queryURLparams(url = window.location.href) {
let obj = {}
if (url.indexOf('?') < 0) return obj
let arr = url.split('?')
url = arr[1]
let array = url.split('&')
for (let i = 0; i < array.length; i++) {
let arr2 = array[i]
let arr3 = arr2.split('=')
obj[arr3[0]] = arr3[1]
}
return obj
},
/**
* @Author: Jason Liu
* @description: 设置缓存对象
*/
set(name, info) {
try {
// 设置缓存数据
if (typeof info === 'object') {
info = JSON.stringify(info)
}
if (info) {
localStorage.setItem(name, escape(info))
} else {
localStorage.removeItem(name)
}
} catch {
}
},
/**
* @Author: Jason Liu
* @description: 获取缓存对象
*/
get(name, isjson = false, def = "{}") {
let result = ''
const loca = localStorage.getItem(name)
if (loca) {
result = unescape(loca)
}
if (isjson) {
result = JSON.parse(result || def);
}
return result
},
/**
* @Author: Jason Liu
* @description: 删除缓存对象
*/
remove(key) {
try {
localStorage.removeItem(key);
} catch {
}
},
/**
* @Author: Jason Liu
* @description: 获取用户token信息
*/
getToken() {
let token = this.get("Bearer")
if (token) {
$storage.userInfo = $storage.get("USER_INFO", true);
}
return token;
},
/**
* @Author: Jason Liu
* @description: 获取机构编码
* @return {*}
*/
getOrdCode() {
let org = this.get("accept_system_org");
return org || $storage.userInfo.orgId;
},
/**
* @Author: Jason Liu
* @description: 清除token
*/
clearToken() {
this.remove("Bearer");
let loginPath = "login";
const tokey = $storage.get("tokey") || "datafactory";
if (window.apps && window.apps.login) {
loginPath = window.apps.login;
} else if ($service.login) {
loginPath = $service.login;
}
if (tokey == "datafactory") {
window.$router.push({
name: loginPath,
})
} else {
const url = $storage.get("authfailurl");
if (url) {
window.location.href = decodeURIComponent(url);
} else {
window.$router.push({
name: loginPath,
})
}
}
}
};