feat: 添加手动解析 url 的 params 内容
- 程序启动的时候获取 token
This commit is contained in:
@@ -3,17 +3,24 @@ import config from '@/config'
|
||||
|
||||
export default function getUrl(url, domainType = 'admin') {
|
||||
let domain = process.env.VUE_APP_ADMIN || ''
|
||||
// domain = "/api"s
|
||||
// if (domainType === 'admin') {
|
||||
// domain = config.admin
|
||||
// }
|
||||
// console.log(`request url is :`,domain+ url);
|
||||
|
||||
return domain + url
|
||||
}
|
||||
|
||||
export function getParamsFromUrl(url) {
|
||||
const search = new URL(url).search
|
||||
const params = new URLSearchParams(search)
|
||||
let search = new URL(url).search
|
||||
let params = new URLSearchParams(search)
|
||||
const length = Object.keys(params).length
|
||||
if (!length) {
|
||||
// 如果获取不到参数,尝试手动解析 url 地址
|
||||
// 解析从 ? 到 #/|/ 中间的字符串,如果没有 #/|/ ,返回后面所有的字符串
|
||||
const arr = url.split('?')
|
||||
search = arr[1]
|
||||
// 如果存在后面哈希路由参数内容,就开始舍弃
|
||||
if (/\#\/?/.test(search)) {
|
||||
search = search.split('#')[0]
|
||||
}
|
||||
params = new URLSearchParams(search)
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
23
src/main.js
23
src/main.js
@@ -30,6 +30,11 @@ for (let item in generatedComponents) {
|
||||
Vue.component(item, generatedComponents[item])
|
||||
}
|
||||
|
||||
const params = getParamsFromUrl(globalThis.location.href)
|
||||
const token = params.get('token')
|
||||
console.log(params)
|
||||
token && globalThis.localStorage.setItem('token', token)
|
||||
|
||||
Vue.use(Image)
|
||||
.use(Toast)
|
||||
.use(Form)
|
||||
@@ -138,21 +143,3 @@ const vm = new Vue({
|
||||
render: h => h(App)
|
||||
}).$mount('#app')
|
||||
window.page = vm
|
||||
|
||||
// fetchToken({
|
||||
// apikey: 'a6sdngp99fd0zokkddusob7dd58e6f',
|
||||
// customerNo: '12',
|
||||
// id: '',
|
||||
// name: 'test-user',
|
||||
// openid: '12',
|
||||
// sex: '1',
|
||||
// status: 0,
|
||||
// type: '1'
|
||||
// }).then(res => {
|
||||
// const { content } = res.contents
|
||||
// debugger
|
||||
// const token = getParamsFromUrl(content).get('token')
|
||||
// console.log(token, 'token')
|
||||
|
||||
// globalThis.localStorage.setItem('token', token)
|
||||
// })
|
||||
|
||||
Reference in New Issue
Block a user