From 87bbde3d46c2c41b681f7d515caf9b41a316dc55 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 6 Mar 2020 21:42:08 +0800 Subject: [PATCH] fix(v3): refactor debug refresh --- packages/uni-app-plus/dist/index.v3.js | 135 +++++++++++++++--- .../app-plus/service/framework/page.js | 6 +- .../service/framework/webview/index.js | 36 +++-- src/shared/query.js | 10 +- 4 files changed, 144 insertions(+), 43 deletions(-) diff --git a/packages/uni-app-plus/dist/index.v3.js b/packages/uni-app-plus/dist/index.v3.js index f58d17dbf7..1cfcffbc0e 100644 --- a/packages/uni-app-plus/dist/index.v3.js +++ b/packages/uni-app-plus/dist/index.v3.js @@ -199,6 +199,7 @@ var serviceContext = (function () { 'checkSession', 'getUserInfo', 'share', + 'shareWithSystem', 'showShareMenu', 'hideShareMenu', 'requestPayment', @@ -307,6 +308,17 @@ var serviceContext = (function () { } } + const encodeReserveRE = /[!'()*]/g; + const encodeReserveReplacer = c => '%' + c.charCodeAt(0).toString(16); + const commaRE = /%2C/g; + + // fixed encodeURIComponent which is more conformant to RFC3986: + // - escapes [!'()*] + // - preserve commas + const encode = str => encodeURIComponent(str) + .replace(encodeReserveRE, encodeReserveReplacer) + .replace(commaRE, ','); + const decode = decodeURIComponent; function parseQuery (query) { @@ -335,6 +347,38 @@ var serviceContext = (function () { }); return res + } + + function stringifyQuery (obj, encodeStr = encode) { + const res = obj ? Object.keys(obj).map(key => { + const val = obj[key]; + + if (val === undefined) { + return '' + } + + if (val === null) { + return encodeStr(key) + } + + if (Array.isArray(val)) { + const result = []; + val.forEach(val2 => { + if (val2 === undefined) { + return + } + if (val2 === null) { + result.push(encodeStr(key)); + } else { + result.push(encodeStr(key) + '=' + encodeStr(val2)); + } + }); + return result.join('&') + } + + return encodeStr(key) + '=' + encodeStr(val) + }).filter(x => x.length > 0).join('&') : null; + return res ? `?${res}` : '' } let id = 0; @@ -1284,9 +1328,9 @@ var serviceContext = (function () { ARRAYBUFFER: 'arraybuffer' }; - const encode = encodeURIComponent; + const encode$1 = encodeURIComponent; - function stringifyQuery (url, data) { + function stringifyQuery$1 (url, data) { let str = url.split('#'); const hash = str[1] || ''; str = str[0].split('?'); @@ -1301,9 +1345,9 @@ var serviceContext = (function () { for (let key in data) { if (data.hasOwnProperty(key)) { if (isPlainObject(data[key])) { - query[encode(key)] = encode(JSON.stringify(data[key])); + query[encode$1(key)] = encode$1(JSON.stringify(data[key])); } else { - query[encode(key)] = encode(data[key]); + query[encode$1(key)] = encode$1(data[key]); } } } @@ -1334,7 +1378,7 @@ var serviceContext = (function () { isPlainObject(params.data) && Object.keys(params.data).length ) { // 将 method,data 校验提前,保证 url 校验时,method,data 已被格式化 - params.url = stringifyQuery(value, params.data); + params.url = stringifyQuery$1(value, params.data); } } }, @@ -7125,6 +7169,39 @@ var serviceContext = (function () { }); } ); + } + + function shareWithSystem (params, callbackId, method = 'shareWithSystem') { + let { + type, + imageUrl, + summary: content, + href + } = params; + type = type || 'text'; + const allowedTypes = ['text', 'image']; + if (allowedTypes.indexOf(type) < 0) { + invoke$1(callbackId, { + errMsg: method + ':fail:分享参数 type 不正确' + }); + } + if (typeof imageUrl === 'string' && imageUrl) { + imageUrl = getRealPath$1(imageUrl); + } + plus.share.sendWithSystem({ + type, + pictures: imageUrl ? [imageUrl] : [], + content, + href + }, function (res) { + invoke$1(callbackId, { + errMsg: method + ':ok' + }); + }, function (err) { + invoke$1(callbackId, { + errMsg: method + ':fail:' + err.message + }); + }); } function restoreGlobal ( @@ -7802,7 +7879,22 @@ var serviceContext = (function () { preloadWebview = webview; } - function createWebview (path, routeOptions) { + function noop$1 (str) { + return str + } + + function getDebugRefresh (path, query, routeOptions) { + const queryString = query ? stringifyQuery(query, noop$1) : ''; + return { + isTab: routeOptions.meta.isTabBar, + arguments: JSON.stringify({ + path: path.substr(1), + query: queryString ? queryString.substr(1) : queryString + }) + } + } + + function createWebview (path, routeOptions, query) { if (routeOptions.meta.isNVue) { const webviewId = id$1++; const webviewStyle = parseWebviewStyle( @@ -7810,6 +7902,7 @@ var serviceContext = (function () { path, routeOptions ); + webviewStyle.debugRefresh = getDebugRefresh(path, query, routeOptions); if (process.env.NODE_ENV !== 'production') { console.log(`[uni-app] createWebview`, webviewId, path, webviewStyle); } @@ -7824,7 +7917,7 @@ var serviceContext = (function () { return webview } - function initWebview (webview, routeOptions, url = '') { + function initWebview (webview, routeOptions, path, query) { // 首页或非 nvue 页面 if (webview.id === '1' || !routeOptions.meta.isNVue) { const webviewStyle = parseWebviewStyle( @@ -7832,17 +7925,7 @@ var serviceContext = (function () { '', routeOptions ); - if (url) { - const part = url.split('?'); - webviewStyle.debugRefresh = { - isTab: routeOptions.meta.isTabBar, - arguments: JSON.stringify({ - path: part[0].substr(1), - query: part[1] || '' - }) - }; - } - + webviewStyle.debugRefresh = getDebugRefresh(path, query, routeOptions); if (process.env.NODE_ENV !== 'production') { console.log(`[uni-app] updateWebview`, webviewStyle); } @@ -8038,7 +8121,7 @@ var serviceContext = (function () { /** * 首页需要主动registerPage,二级页面路由跳转时registerPage */ - function registerPage ({ + function registerPage ({ url, path, query, @@ -8060,7 +8143,7 @@ var serviceContext = (function () { } if (!webview) { - webview = createWebview(path, routeOptions); + webview = createWebview(path, routeOptions, query); } else { webview = plus.webview.getWebviewById(webview.id); webview.nvue = routeOptions.meta.isNVue; @@ -8078,7 +8161,7 @@ var serviceContext = (function () { console.log(`[uni-app] registerPage`, path, webview.id); } - initWebview(webview, routeOptions, url); + initWebview(webview, routeOptions, path, query); const route = path.slice(1); @@ -9095,6 +9178,7 @@ var serviceContext = (function () { requireNativePlugin: requireNativePlugin$1, shareAppMessageDirectly: shareAppMessageDirectly, share: share, + shareWithSystem: shareWithSystem, restoreGlobal: restoreGlobal, getSubNVueById: getSubNVueById, getCurrentSubNVue: getCurrentSubNVue, @@ -10911,7 +10995,14 @@ var serviceContext = (function () { } function request$1 (args, callbackId) { - if (args.method !== 'GET' && args.header['Content-Type'].indexOf('application/json') === 0 && isPlainObject(args.data)) { + let contentType; + for (const name in args.header) { + if (name.toLowerCase() === 'content-type') { + contentType = args.header[name]; + break + } + } + if (args.method !== 'GET' && contentType.indexOf('application/json') === 0 && isPlainObject(args.data)) { args.data = JSON.stringify(args.data); } const { diff --git a/src/platforms/app-plus/service/framework/page.js b/src/platforms/app-plus/service/framework/page.js index 843d62432e..7f87c32945 100644 --- a/src/platforms/app-plus/service/framework/page.js +++ b/src/platforms/app-plus/service/framework/page.js @@ -24,7 +24,7 @@ export function getCurrentPages (returnAll) { /** * 首页需要主动registerPage,二级页面路由跳转时registerPage */ -export function registerPage ({ +export function registerPage ({ url, path, query, @@ -46,7 +46,7 @@ export function registerPage ({ } if (!webview) { - webview = createWebview(path, routeOptions) + webview = createWebview(path, routeOptions, query) } else { webview = plus.webview.getWebviewById(webview.id) webview.nvue = routeOptions.meta.isNVue @@ -64,7 +64,7 @@ export function registerPage ({ console.log(`[uni-app] registerPage`, path, webview.id) } - initWebview(webview, routeOptions, url) + initWebview(webview, routeOptions, path, query) const route = path.slice(1) diff --git a/src/platforms/app-plus/service/framework/webview/index.js b/src/platforms/app-plus/service/framework/webview/index.js index 11cb191688..628f5c5740 100644 --- a/src/platforms/app-plus/service/framework/webview/index.js +++ b/src/platforms/app-plus/service/framework/webview/index.js @@ -1,3 +1,7 @@ +import { + stringifyQuery +} from 'uni-shared' + import { parseWebviewStyle } from './parser/webview-style-parser' @@ -41,7 +45,22 @@ export function setPreloadWebview (webview) { preloadWebview = webview } -export function createWebview (path, routeOptions) { +function noop (str) { + return str +} + +function getDebugRefresh (path, query, routeOptions) { + const queryString = query ? stringifyQuery(query, noop) : '' + return { + isTab: routeOptions.meta.isTabBar, + arguments: JSON.stringify({ + path: path.substr(1), + query: queryString ? queryString.substr(1) : queryString + }) + } +} + +export function createWebview (path, routeOptions, query) { if (routeOptions.meta.isNVue) { const webviewId = id++ const webviewStyle = parseWebviewStyle( @@ -49,6 +68,7 @@ export function createWebview (path, routeOptions) { path, routeOptions ) + webviewStyle.debugRefresh = getDebugRefresh(path, query, routeOptions) if (process.env.NODE_ENV !== 'production') { console.log(`[uni-app] createWebview`, webviewId, path, webviewStyle) } @@ -63,7 +83,7 @@ export function createWebview (path, routeOptions) { return webview } -export function initWebview (webview, routeOptions, url = '') { +export function initWebview (webview, routeOptions, path, query) { // 首页或非 nvue 页面 if (webview.id === '1' || !routeOptions.meta.isNVue) { const webviewStyle = parseWebviewStyle( @@ -71,17 +91,7 @@ export function initWebview (webview, routeOptions, url = '') { '', routeOptions ) - if (url) { - const part = url.split('?') - webviewStyle.debugRefresh = { - isTab: routeOptions.meta.isTabBar, - arguments: JSON.stringify({ - path: part[0].substr(1), - query: part[1] || '' - }) - } - } - + webviewStyle.debugRefresh = getDebugRefresh(path, query, routeOptions) if (process.env.NODE_ENV !== 'production') { console.log(`[uni-app] updateWebview`, webviewStyle) } diff --git a/src/shared/query.js b/src/shared/query.js index f24e6807b8..db0865c630 100644 --- a/src/shared/query.js +++ b/src/shared/query.js @@ -39,7 +39,7 @@ export function parseQuery (query) { return res } -export function stringifyQuery (obj) { +export function stringifyQuery (obj, encodeStr = encode) { const res = obj ? Object.keys(obj).map(key => { const val = obj[key] @@ -48,7 +48,7 @@ export function stringifyQuery (obj) { } if (val === null) { - return encode(key) + return encodeStr(key) } if (Array.isArray(val)) { @@ -58,15 +58,15 @@ export function stringifyQuery (obj) { return } if (val2 === null) { - result.push(encode(key)) + result.push(encodeStr(key)) } else { - result.push(encode(key) + '=' + encode(val2)) + result.push(encodeStr(key) + '=' + encodeStr(val2)) } }) return result.join('&') } - return encode(key) + '=' + encode(val) + return encodeStr(key) + '=' + encodeStr(val) }).filter(x => x.length > 0).join('&') : null return res ? `?${res}` : '' } -- GitLab