原生APP跳转到H5页面时,往往需要携带一些用户信息,之前做法是在跳转的地址中拼接H5页面需要的参数,现在通过window.WebViewJavascriptBridge悄悄的进行数据交互。
需要在app里面测试才能看到效果
1.注册全局方法
export const savePhoto = (options) => {
console.log(options)
let params = '{}';
if (typeof (options) == 'object') {
params = JSON.stringify(options)
}
window.WebViewJavascriptBridge.callHandler(
'savePhoto',
encodeURIComponent(params),
function (responseData) {
if (options && options.onResponse) {
options.onResponse(responseData);
}
}
);
}
2.使用全局方法,保存图片至相册(以下是使用vue3点击事件)
const saveButton = url =>{
const imgName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."))
savePhoto({
url : url,
name :imgName+index.value+'.png',
onResponse : function (res) {
const ress = JSON.parse(res);
if (ress && ress.code == '200') {
setTimeout(()=>{
alert("保存图片成功") //可用组件,样式美化些
index.value ++
},500)
}
}
})
}