Created
March 6, 2018 03:15
-
-
Save bhaltair/5c8a0376aafafbc90647bef1cef26d38 to your computer and use it in GitHub Desktop.
合码 js bridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mustFunc = (v) => { | |
return getObjectType(v) === 'Function' | |
? v | |
: () => {} | |
} | |
const getObjectType = (v) => { | |
return Object.prototype.toString.call(v).slice(8, -1) | |
} | |
class HemaJSBridge { | |
/** | |
* 版本 | |
*/ | |
static version () { | |
return '1.0.3' | |
} | |
/** | |
* h5 主动发起交互 | |
* @param {string} action 触发的事件 | |
* @param {object/function} option 传参,当 option 是 function,则 callback = option | |
* @param {function} callback 操作完成后 APP 回调事件 | |
*/ | |
invoke (action, option, callback) { | |
let iframe = document.createElement('iframe') | |
iframe.style.width = '1px' | |
iframe.style.height = '1px' | |
iframe.style.display = 'none' | |
if (getObjectType(option) === 'Function') { | |
this.callback = mustFunc(option) | |
iframe.src = `jsbridge://${action}` | |
} else { | |
this.callback = mustFunc(callback) | |
iframe.src = `jsbridge://${action}${Object.keys(option).length > 0 ? `?${JSON.stringify(option)}` : ''}` | |
} | |
document.body.appendChild(iframe) | |
setTimeout(() => { | |
iframe.remove() | |
}, 100) | |
} | |
/** | |
* header 头部导航条 | |
* @param { | |
left, | |
center, | |
right, | |
autoTitle, | |
type, | |
noborderBottom | |
} | |
*/ | |
header (option) { | |
if (getObjectType(option) !== 'Object') option = {} | |
if (getObjectType(option.left) === 'Function') { | |
this.headerLeft = option.left | |
} else if (getObjectType(option.left) === 'Object') { | |
this.headerLeft = mustFunc(option.left.click) | |
delete option.left.click | |
option.left = Object.assign({ | |
text: null, | |
color: null | |
}, option.left) | |
} | |
if (getObjectType(option.center) === 'Function') { | |
this.headerCenter = option.center | |
} else if (getObjectType(option.center) === 'String') { | |
option.center = { | |
text: option.center | |
} | |
} else if (getObjectType(option.center) === 'Object') { | |
this.headerCenter = mustFunc(option.center.click) | |
delete option.center.click | |
option.center = Object.assign({ | |
text: null, | |
color: null | |
}, option.center) | |
} | |
if (getObjectType(option.right) === 'Function') { | |
this.headerRight = option.right | |
} else if (getObjectType(option.right) === 'Object') { | |
this.headerRight = mustFunc(option.right.click) | |
delete option.right.click | |
option.right = Object.assign({ | |
text: null, | |
color: null | |
}, option.right) | |
} | |
this.invoke('header', { | |
left: option.left, | |
center: option.center, | |
right: option.right, | |
autoTitle: option.autoTitle === undefined ? true : option.autoTitle, | |
type: option.type === undefined ? '' : option.type, | |
noborderBottom: option.noborderBottom === undefined ? false : option.noborderBottom | |
}, this.callback) | |
} | |
} | |
window.HemaJSBridge = new HemaJSBridge() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
window.HemaJSBridge
前端
初始化获取数据
h5 主动发起交互
APP 头部导航条
APP 主动发起交互
APP 部分
拦截的url = "jsbridge://methodName?jsonObj"
h5 主动发起交互
APP 头部导航条