92 lines
3.1 KiB
TypeScript
92 lines
3.1 KiB
TypeScript
import VueCookie from 'vue-cookie'
|
|
import { setLocalData, getLocalData } from './common'
|
|
import axios from 'axios'
|
|
import { Base64 } from 'js-base64'
|
|
import { getData } from 'lz-utils-lib'
|
|
|
|
// 工厂模式:获取用户信息
|
|
export default class GetUserInfo {
|
|
static whiteSNeedtores = ['/unicom/auth/result']
|
|
static envs = { lth5: 'online', tlth5: 'test' }
|
|
static env = ''
|
|
mobile = ''
|
|
|
|
// 获取search_info
|
|
static getInstance = () => {
|
|
const host = location.host
|
|
/* if (['/unicom/login', '/unicom/ycMall/login'].includes(location.pathname) || location.search.includes('jumpData=')) {
|
|
GetUserInfo.isNeedClearCache = true
|
|
} */
|
|
let returnData = ''
|
|
const { returnData: returnDataStr } = getData.getUrlParams(location.search) || {}
|
|
returnData = JSON.parse(Base64.decode(decodeURIComponent(returnDataStr)))
|
|
const env = (GetUserInfo.env = GetUserInfo.envs[host.replace(/^([a-z]+).+/g, '$1')])
|
|
const { envParams, extendParams } = (returnData as any) || {}
|
|
GetUserInfo.logParams(env, returnData, envParams, extendParams)
|
|
returnDataStr && !import.meta.env.DEV && GetUserInfo.sendReturnData('returnData=' + returnDataStr, extendParams.mobile, location.hostname)
|
|
return new GetUserInfo(returnData, envParams, extendParams)
|
|
}
|
|
|
|
// constructor
|
|
constructor(returnData, envParams, extendParams) {
|
|
// if ((returnData || GetUserInfo.isNeedClearCache) && !GetUserInfo.whiteSNeedtores.includes(location.pathname)) {
|
|
if (returnData && !GetUserInfo.whiteSNeedtores.includes(location.pathname)) {
|
|
this.clearCache()
|
|
this.saveCache(envParams, extendParams)
|
|
} else {
|
|
this.getCache()
|
|
}
|
|
}
|
|
|
|
// 清除缓存
|
|
clearCache = () => {
|
|
const noCleanKeys = []
|
|
const noCleanKeysData = noCleanKeys.reduce((data, key) => {
|
|
data[key] = localStorage.getItem(key)
|
|
return data
|
|
}, {})
|
|
localStorage.clear()
|
|
Object.keys(noCleanKeysData).forEach((key) => {
|
|
noCleanKeysData[key] && localStorage.setItem(key, noCleanKeysData[key])
|
|
})
|
|
VueCookie.delete('userInfo')
|
|
VueCookie.delete('extendParamsData')
|
|
}
|
|
|
|
// 获取旧缓存
|
|
getCache = () => {
|
|
const userInfo = getLocalData('userInfo') || {}
|
|
this.mobile = userInfo.mobile
|
|
}
|
|
|
|
// 清除后保存缓存
|
|
saveCache = (envParams, extendParams) => {
|
|
if (extendParams) {
|
|
this.mobile = extendParams.mobile
|
|
setLocalData('userInfo', extendParams)
|
|
setLocalData('extendParamsData', extendParams)
|
|
}
|
|
const envData = Base64.encode(JSON.stringify(envParams))
|
|
setLocalData('envData', envData)
|
|
}
|
|
|
|
// 打印日志
|
|
static logParams = (env, returnData, envParams, extendParams) => {
|
|
if (env !== 'online') {
|
|
console.log('href: ', location.href)
|
|
console.log('returnData: ', JSON.stringify(returnData))
|
|
console.log('extendParams: ', JSON.stringify(extendParams))
|
|
console.log('envParams: ', JSON.stringify(envParams))
|
|
}
|
|
}
|
|
|
|
// 发送returnData
|
|
static sendReturnData = (returnData, username, hostname) => {
|
|
/* axios.post('https://bllog.yijiesudai.com/bl-log/web/app/send/returnData', {
|
|
username,
|
|
returnData: returnData,
|
|
environment: hostname,
|
|
}) */
|
|
}
|
|
}
|