Skip to content

Commit

Permalink
feat: uni.webview.js 支持 uni x
Browse files Browse the repository at this point in the history
  • Loading branch information
hdx committed Jul 3, 2023
1 parent ca62d04 commit 310bb49
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build/rollup.config.web-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
input: 'src/core/runtime/web-view/index.js',
output: {
name: 'uni',
file: 'dist/uni.webview.1.5.4.js',
file: 'dist/uni.webview.1.5.5.js',
format: 'umd'
},
plugins: [
Expand Down
1 change: 1 addition & 0 deletions dist/uni.webview.1.5.5.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/platforms/app-plus/runtime/web-view-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ function isNvue () {
return (window.__dcloud_weex_postMessage || window.__dcloud_weex_)
}

function isUvue () {
return (window.__uniapp_x_postMessage || window.__uniapp_x_)
}

const publish = function (method, params) {
const paramsObj = {
options: {
Expand All @@ -16,6 +20,33 @@ const publish = function (method, params) {
arg: params
}

if (isUvue()) { // uvue web-view
if (method === 'postMessage') {
const message = {
data: [params]
}
if (window.__uniapp_x_postMessage) {
return window.__uniapp_x_postMessage(message)
} else {
return window.__uniapp_x_.postMessage(JSON.stringify(message))
}
}

const serviceMessage = {
type: WEB_INVOKE_APPSERVICE,
args: {
data: paramsObj,
webviewIds
}
}
if (window.__uniapp_x_postMessage) {
window.__uniapp_x_postMessageToService(serviceMessage)
} else {
window.__uniapp_x_.postMessageToService(JSON.stringify(serviceMessage))
}
return
}

if (isNvue()) { // nvue web-view
if (method === 'postMessage') {
const message = {
Expand All @@ -40,6 +71,7 @@ const publish = function (method, params) {
} else {
window.__dcloud_weex_.postMessageToService(JSON.stringify(serviceMessage))
}
return
}

if (!window.plus) { // h5 web-view
Expand Down

0 comments on commit 310bb49

Please sign in to comment.