File tree Expand file tree Collapse file tree 2 files changed +29
-13
lines changed Expand file tree Collapse file tree 2 files changed +29
-13
lines changed Original file line number Diff line number Diff line change @@ -162,19 +162,21 @@ export function param(json) {
162162 * @returns {Object }
163163 */
164164export 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/**
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments