JSBridge
独立包,功能强大,使用简单
使用
此文件放在 app 中,构建 bridge
WebViewJavascriptBridge 是 Webview和 Html 交互通信的桥梁
import Bridge '@deepjs/bridge'
const bridge = new Bridge({
isInApp: true,
methods: [
'getAppConfig',
'getNativePath',
'login',
'logout',
'getUserInfo',
'getSystemInfo',
'getLocationInfo',
'navigateTo',
'redirectTo',
'navigateBack',
'switchTab',
'showToast',
'showAlert',
'showLoading',
'hideLoading',
'scanCode',
'enableRefresh',
'disableRefresh',
'stopRefresh',
'setNavigationBar',
'addNavigationBarMenu',
'removeNavigationBarMenu',
'showShare',
'checkUpdate',
'securityGuard',
'showKeyboard',
'vueLoadFinish',
],
events: [
'shake',
'back',
'pageShow',
'pageHide',
'pullRefresh',
'locationChanged',
'loginStatusChanged',
'togglefollow',
],
})
customSchema://native/<页面名>?query
customSchema://native/webview?url=encoded(<url>)
方法
import WebviewJsBridge from '@/utils/bridge';
bridge.ready(birdge => {
birdge.xxx();
})
常见方法
bridge.getSystemInfo({
success(res) {
const { data } = res;
},
});
bridge.showToast({
content: 'toast 信息, 必填字段,最长15字',
success(res) {
console.log(res);
},
});
bridge.showAlert({
title: '可选字段',
content: '必填字段',
btns: [
{
id: 1,
type: 'cancel',
text: '取消',
},
{
id: 2,
type: 'sure',
text: '确定',
},
],
success(res) {
console.log(res);
},
});
bridge.navigateTo({
url: location.href,
success(res) {
console.log(res);
},
});
bridge.redirectTo({
url: location.href,
success(res) {
console.log(res);
},
});
bridge.navigateBack({
deep: 1,
success(res) {
console.log(res);
},
});
bridge.getNativePath({
success(res) {
console.log(res);
},
});
bridge.switchTab({
url: location.href,
success(res) {
console.log(res);
},
});
bridge.setNavigationBar({
title: '这是标题',
color: '#ff0000',
backgroundColor: '#1d33ee44',
success(res) {
console.log(res);
},
});
bridge.addNavigationBarMenu({
left: [
{
id: 1,
icon: 'back',
text: '返回',
color: '#ff0000',
fontSize: '',
},
],
right: [
{
id: 2,
icon: '',
text: '保存',
color: '#ff0000',
fontSize: '',
},
{
type: 'share',
id: 3,
icon: 'share',
text: '分享',
color: '',
fontSize: '',
},
],
success(res) {
if (res.data.type === 'share') {
bridge.showShare({
type: 1,
title: '分享的标题',
desc: '分享的描述信息',
link: 'https://baidu.com',
imgUrl:
'http://img02.tooopen.com/images/20160509/tooopen_sy_161967094653.jpg',
success(res2) {
console.log(res2);
},
});
}
console.log(res);
},
});
bridge.removeNavigationBarMenu({
ids: [1],
success(res) {
console.log(res);
},
fail() {},
complete() {},
});
bridge.showShare({
type: 1,
url: '当前url',
title: '分享的标题',
desc: '分享的描述信息',
link: 'https://baidu.com',
wakeup: 'weixin',
imgUrl:
'http://img02.tooopen.com/images/20160509/tooopen_sy_161967094653.jpg',
weixin: {
title: '不一样的标题,覆盖通用分享数据',
},
weixin_timeline: {
},
onlySelectChannel: [
'weixin',
'weixin_timeline',
'qq',
'qzone',
'weibo',
'copy_link',
'image',
'delete',
'report',
],
});
bridge.startShare({
onlySelectChannel: [
'Weibo',
'ALPContact',
'ALPTimeLine',
'SMS',
'Weixin',
'WeixinTimeLine',
'QQ',
'QQZone',
'DingDing',
'OpenInBrowser',
'Favorite',
],
channels: {
weibo: {},
},
}, data => {
console.log('当前用户点击的分享渠道名为:' + data.channelName);
bridge.shareToChannel({
name: data.channelName,
param: shareData,
})
});
function connectWebViewJavascriptBridge() {
if (window.WebViewJavascriptBridge) {
onWebViewJavascriptBridgeReady();
}
else {
document.addEventListener('WebViewJavascriptBridgeReady', function(event) {
}, false)
}
}
bridge.ready(function(bridge) {
bridge.hideNavigationBar({
animation: true
});
});
bridge.share = function(obj){
}
bridge.ready(function(bridge){
bridge.method({
})
})
常见日志 or 报错
- WeixinJSBridge exec time 9 执行时间
- window.WeixinJSBridge already exists 已存在
参看: