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] 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 eb760d5e..f411f2fc 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 } /**