Skip to content

Commit a50180f

Browse files
authored
fix[utils]: param2Obj bug when url params includes ==(PanJiaChen#3100)
1 parent fc68f56 commit a50180f

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/utils/index.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,21 @@ export function param(json) {
162162
* @returns {Object}
163163
*/
164164
export function param2Obj(url) {
165-
const search = url.split('?')[1]
166-
if (!search) {
167-
return {}
168-
}
169-
return JSON.parse(
170-
'{"' +
171-
decodeURIComponent(search)
172-
.replace(/"/g, '\\"')
173-
.replace(/&/g, '","')
174-
.replace(/=/g, '":"')
175-
.replace(/\+/g, ' ') +
176-
'"}'
177-
)
165+
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
166+
if (!search) {
167+
return {}
168+
}
169+
const obj = {}
170+
const searchArr = search.split('&')
171+
searchArr.forEach(v => {
172+
const index = v.indexOf('=')
173+
if (index !== -1) {
174+
const name = v.substring(0, index)
175+
const val = v.substring(index + 1, v.length)
176+
obj[name] = val
177+
}
178+
})
179+
return obj
178180
}
179181

180182
/**

tests/unit/utils/param2Obj.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { param2Obj } from '@/utils/index.js'
2+
describe('Utils:param2Obj', () => {
3+
const url = 'https://github.com/PanJiaChen/vue-element-admin?name=bill&age=29&sex=1&field=dGVzdA==&key=%E6%B5%8B%E8%AF%95'
4+
5+
it('param2Obj test', () => {
6+
expect(param2Obj(url)).toEqual({
7+
name: 'bill',
8+
age: '29',
9+
sex: '1',
10+
field: window.btoa('test'),
11+
key: '测试'
12+
})
13+
})
14+
})

0 commit comments

Comments
 (0)