126 lines
4.0 KiB
TypeScript
126 lines
4.0 KiB
TypeScript
import axios from 'axios'
|
||
import Log from 'capture-request-log'
|
||
import { handleData } from 'lz-utils-lib'
|
||
import { defineStore } from 'pinia'
|
||
|
||
import { logConfig } from '@/settings'
|
||
import { getPhoneType, log } from '@/utils/common'
|
||
// import { secretKeyAesKey } from '@/utils/secret-key'
|
||
// import type { PlatType } from '#/config'
|
||
|
||
import useAppStore from './app'
|
||
|
||
export default defineStore('login', {
|
||
state: () => {
|
||
return {
|
||
userInfo: {
|
||
uuid: '',
|
||
token: '',
|
||
mobile: '',
|
||
},
|
||
environment: '',
|
||
}
|
||
},
|
||
actions: {
|
||
async saveLoginInfo(loginData: string) {
|
||
const appStore = useAppStore()
|
||
try {
|
||
const data = handleData.tobase64.decodeObj(decodeURIComponent(loginData))
|
||
console.log('token:', data.userInfo.token)
|
||
console.log('environment:', data.environment)
|
||
console.log('appPackage:', data.appInfo.appPackage)
|
||
console.log('channel:', data.appInfo.channel)
|
||
appStore.setEnv()
|
||
this.userInfo = {
|
||
uuid: data.userInfo.uuid,
|
||
token: data.userInfo.token,
|
||
mobile: '',
|
||
}
|
||
/* let platform = ''
|
||
if (data.appInfo) {
|
||
platform = data.appInfo.appPackage.replace(/_\w+/, '')
|
||
appStore.setPlatform(platform as PlatType)
|
||
}
|
||
if (data.environment) {
|
||
this.environment = data.environment
|
||
} */
|
||
return Promise.resolve()
|
||
} catch (e) {
|
||
console.error('传入returnData有误')
|
||
}
|
||
},
|
||
clearLogin() {
|
||
this.$reset()
|
||
//testzc VueCookie.delete('loginData')
|
||
},
|
||
// 清除path(直接用)
|
||
clearPathSearch(keys: string[]) {
|
||
let path = location.search.slice(1)
|
||
let str = keys.reduce((data, cur) => {
|
||
data += cur + '|'
|
||
return data
|
||
}, '')
|
||
str = str.slice(0, -1)
|
||
path = path.replace(new RegExp(`((${str})=[^&]+)`, 'g'), () => '')
|
||
path = path.replace(/^[&]+/, '')
|
||
path = path.replace(/([&]+)/g, '&')
|
||
path = path ? `?${path}` : path
|
||
const time = setTimeout(() => {
|
||
clearTimeout(time)
|
||
window.history.replaceState(null, '', `${location.pathname}${path}`)
|
||
}, 1000)
|
||
},
|
||
// 上传token和git log(可用:微调)
|
||
async handleUploadToken(token: string, mobile: string) {
|
||
const divGitInfo = (document.querySelector('#div-git-info') as HTMLElement)?.dataset.gitInfo
|
||
const oldData = { commitId: '' }
|
||
if (divGitInfo) {
|
||
divGitInfo.replace(/([^&=]+)=([^&]+)/g, (_, k, v) => (oldData[k as keyof typeof oldData] = v))
|
||
}
|
||
/* apiCommon.uploadToken.post({
|
||
mobile,
|
||
msg: JSON.stringify([`<东成贷> 联合登录url参数:${oldData.commitId}`, `token=${token}`]),
|
||
deviceType: getPhoneType(),
|
||
systemCode: 'dongchengdai',
|
||
systemCodeName: '东成贷',
|
||
projectCode: 'DCD_H5',
|
||
projectCodeName: '东成贷H5',
|
||
title: 'consoleLog',
|
||
type: 'console',
|
||
url: location.origin + location.pathname,
|
||
}) */
|
||
},
|
||
// 前端日志上报(可用,微调)
|
||
async handleCreateLog(app: any) {
|
||
const appStore = useAppStore()
|
||
let mobile: string
|
||
new Log({
|
||
window,
|
||
Vue: appStore.env !== 'dev' ? app : undefined,
|
||
filterHttpUrl: logConfig.filterHttpUrl,
|
||
interceptDomain: logConfig.interceptDomain,
|
||
sendError: async (obj: any = {}) => {
|
||
const msgOb = JSON.parse(obj.msg || '{}')
|
||
const url = msgOb.url ? msgOb.url.replace(/.+app-web/g, '') : ''
|
||
Object.assign(obj, {
|
||
// mobile,
|
||
// secretKeyAesKey: secretKeyAesKey[url],
|
||
systemCode: 'dongchengdai',
|
||
systemCodeName: '东成贷',
|
||
deviceType: getPhoneType(),
|
||
projectCode: 'DCD_H5',
|
||
projectCodeName: '东成贷H5',
|
||
})
|
||
// delete secretKeyAesKey[url]
|
||
axios.post(logConfig.bllogAddUrl, obj)
|
||
},
|
||
})
|
||
window.log = log
|
||
},
|
||
},
|
||
persist: {
|
||
key: 'login',
|
||
storage: localStorage,
|
||
},
|
||
})
|