From ff9f45e6dd989f06d93b720d5b7562a5966d3997 Mon Sep 17 00:00:00 2001 From: MaYuanhai <414199639@qq.com> Date: Thu, 9 Apr 2020 10:20:30 +0800 Subject: [PATCH 1/3] Update index.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重写 param2Obj 避免 get 入参值存在 ‘=’ ,微信分享链接url可能自带含有 ‘=’ ‘==’的入参,导致原 param2Obj 解析出现问题 --- src/utils/index.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index eb760d5e06e..f411f2fce76 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -162,19 +162,21 @@ export function param(json) { * @returns {Object} */ export function param2Obj(url) { - const search = url.split('?')[1] - if (!search) { - return {} - } - return JSON.parse( - '{"' + - decodeURIComponent(search) - .replace(/"/g, '\\"') - .replace(/&/g, '","') - .replace(/=/g, '":"') - .replace(/\+/g, ' ') + - '"}' - ) + const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ') + if (!search) { + return {} + } + const obj = {} + const searchArr = search.split('&') + searchArr.forEach(v => { + const index = v.indexOf('=') + if (index !== -1) { + const name = v.substring(0, index) + const val = v.substring(index + 1, v.length) + obj[name] = val + } + }) + return obj } /** From ceb70e95f6f74c2f4ebd103057bd7501b3fe015f Mon Sep 17 00:00:00 2001 From: MaYuanhai <414199639@qq.com> Date: Fri, 10 Apr 2020 11:35:49 +0800 Subject: [PATCH 2/3] Create param2Obj.spec.js param2Obj test --- tests/unit/utils/param2Obj.spec.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/unit/utils/param2Obj.spec.js diff --git a/tests/unit/utils/param2Obj.spec.js b/tests/unit/utils/param2Obj.spec.js new file mode 100644 index 00000000000..3bc56ee2fdd --- /dev/null +++ b/tests/unit/utils/param2Obj.spec.js @@ -0,0 +1,13 @@ +import { param2Obj } from '@/utils/index.js' +describe('Utils:param2Obj', () => { + const url = 'https://github.com/PanJiaChen/vue-element-admin?name=bill&age=29&sex=1&field=dGVzdA==' + + it('param2Obj test', () => { + expect(param2Obj(url)).toEqual({ + name: 'bill', + age: '29', + sex: '1', + field: window.btoa('test') + }) + }) +}) From 32ad600d336a7dffa3e95bfd3016d2040e2dec01 Mon Sep 17 00:00:00 2001 From: MaYuanhai <414199639@qq.com> Date: Mon, 13 Apr 2020 11:33:31 +0800 Subject: [PATCH 3/3] Update param2Obj.spec.js --- tests/unit/utils/param2Obj.spec.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/utils/param2Obj.spec.js b/tests/unit/utils/param2Obj.spec.js index 3bc56ee2fdd..e106ed88b54 100644 --- a/tests/unit/utils/param2Obj.spec.js +++ b/tests/unit/utils/param2Obj.spec.js @@ -1,13 +1,14 @@ import { param2Obj } from '@/utils/index.js' describe('Utils:param2Obj', () => { - const url = 'https://github.com/PanJiaChen/vue-element-admin?name=bill&age=29&sex=1&field=dGVzdA==' + const url = 'https://github.com/PanJiaChen/vue-element-admin?name=bill&age=29&sex=1&field=dGVzdA==&key=%E6%B5%8B%E8%AF%95' it('param2Obj test', () => { expect(param2Obj(url)).toEqual({ name: 'bill', age: '29', sex: '1', - field: window.btoa('test') + field: window.btoa('test'), + key: '测试' }) }) })