let isAndroid = navigator.userAgent.indexOf('BudaiAPPAndroid') > -1;
let isiOS = navigator.userAgent.indexOf('BudaiAPPiOS') > -1;
let isAPPHybrid = isAndroid || isiOS;
// app调h5初始化方法
const appH5Init = function(data) {
if (isAndroid) {
console.log('tag', '安卓');
sessionStorage.phoneType = 'android';
}
//iOS使用
if (isiOS) {
console.log('tag', 'iOS');
sessionStorage.phoneType = 'iOS';
}
console.log('调用成功:' + JSON.stringify(data))
if (typeof(data) == "string") {
console.log('这个JSONstring,需要转换为JSON对象' + data)
data = JSON.parse(data)
}
const jsonUserData = JSON.stringify(data)
const currentToken = data.usersession.usertoken
const appUserData = window.localStorage.getItem('appUserData')
console.log('appH5InitJSON' + data)
console.log('555' + '测试一下')
if (appUserData) {
console.log('1、appUserData已经初始化登陆过一次了,只需要更新以下localStorage数据即可' + data.usersession.usertoken)
window.localStorage.setItem('ShopMemberUserToken', data.usersession.usertoken)
window.localStorage.setItem('appUserData', jsonUserData)
console.log(window.localStorage.getItem('ShopMemberUserToken'));
//console.log(2, window.localStorage.getItem('ShopMemberUserToken'))
return
}
console.log('1、appUserData第一次登陆了,需要重新定义一个localStorage数据' + data.usersession.usertoken)
window.localStorage.setItem('ShopMemberUserToken', data.usersession.usertoken)
window.localStorage.setItem('appUserData', jsonUserData)
console.log(window.localStorage.getItem('ShopMemberUserToken'));
}
// h5 调用app的方法
const onNativePage = function(name, data, callback) {
// console.log('开始调用APP方法:' + name + ",参数是:" + JSON.stringify(data) + ",callback是:" + callback)
const params = {}
params.nativePage = name
params.callbackURL = callback
params.data = data
if (typeof(params) == "object") {
console.log('这个object,需要转换为JSONstring' + data)
}
if (isAndroid) {
console.log('tag', '安卓');
window.phoneFunc.toNativePage(JSON.stringify(params))
return true
}
//iOS使用
if (isiOS) {
console.log('tag', 'iOS');
window.webkit.messageHandlers.toNativePage.postMessage(JSON.stringify(params));
return true
}
return false
}
// h5 调用app支付的方法
const onNativePay = function(name, data, callback) {
console.log('开始调用APP方法:' + name + ",参数是:" + JSON.stringify(data) + ",callback是:" + callback)
const params = data
params.nativePage = name
params.callbackURL = callback
console.log('params' + JSON.stringify(params))
if (typeof(params) == "object") {
console.log('这个object,需要转换为JSONstring' + params)
}
if (isAndroid) {
console.log('tag', '安卓');
params.payMode = params.payMode + "androidApp"
window.phoneFunc.appPay(JSON.stringify(params))
return true
}
//iOS使用
if (isiOS) {
console.log('tag', 'iOS');
params.payMode = params.payMode + "iosAPP"
window.webkit.messageHandlers.appPay.postMessage(JSON.stringify(params));
return true
}
return false
}
export default {
isAndroid,
isiOS,
isAPPHybrid
}
window.appH5Init = appH5Init
window.onNativePage = onNativePage
window.onNativePay = onNativePay
//调用支付方法
if (window.onNativePay("toPayH5", params, callbackURL)) {
return;
}
//如果是APP则跳转至APP方法
if (window.onNativePage("appHome", {}, "")) {
return;
}
前端调用app方法 封装
最新推荐文章于 2024-04-10 08:54:43 发布