Skip to content

🔌 前端 API

请开发者熟读 wendux/DSBridge-Android - Javascript API 章节,掌握与原生 API 交互的基本方式。

API 命名空间

在当前实现,没有定义 API 资源的情况下,不使用命名空间功能。

参数

返回值

钩子/生命周期

onWebViewCreated

示例

javascript
dsBridge.call('onWebViewCreated', (res)=>{
    //callback
})

notifyWebViewUpdated

参数

apiName: String

notifyWebViewUpdated

callback(updateObj: JSONObject)

执行 notifyWebViewUpdated 的函数,包含一个参数,描述什么业务数据更新的对象

json
{
  "name": "user"
}
name类型备注
userString用户信息
callback 返回值
返回值典型值类型备注
truetrue falseBoolean表示收到 updated

示例

javascript
dsBridge.register('notifyWebViewUpdated', ({ name } = res)=> {
    if (name === 'user') {
        //do something  
    } 
    return true
})

事件

notifyEvent

参数

apiName: String

notifyEvent

callback(eventObject: JSONObject)

执行 notifyEvent 的函数,包含一个参数

json
{
  "name": "shareSuccess"
}
eventName类型备注
shareSuccessString点击分享到社交应用
loginSuccessString关闭登录页之后
callback 返回值
返回值典型值类型备注
truetrue falseBoolean表示收到 notifyEvent

示例

javascript
dsBridge.register('notifyEvent', ({ name } = res)=> {
    if (name === 'loginSuccess') {
        //do something  
    } 
    return true
})

业务方法

getUserByNative

参数

apiName: String

getUserByNative

callback(user: JSON)
参数典型值类型备注
useruserJSONObject数据结构

示例

javascript
dsBridge.call('getUserByNative', (res)=>{
    //callback
})

getLoginStatusByNative

参数

apiName: String

getLoginStatusByNative

callback(isLogin: Boolean|String)
参数典型值类型备注
isLogintrue false / 1 ``Boolean/String两端返回值不一致,先经过 !!isLogin 处理

示例

javascript
dsBridge.call('getLoginStatusByNative', (res)=>{
    const isLogin = !!res
})

onSetShare

参数

apiName: String

onSetShare

shareObject: JSONObject
json
{
  "title": "分享标题",
  "desc" : "分享描述",
  "icon" : "http://images.infzm.com/medias/2018/0816/136580.jpeg@770x510",
  "url"  : "http://www.infzm.com/content/136473"
}
callback(res: Boolean)

API 调用成功后的回调

参数典型值类型备注
restrue falseBoolean

示例

javascript
const shareObject = {
      "title": "分享标题",
      "desc" : "分享描述",
      "icon" : "http://images.infzm.com/medias/2018/0816/136580.jpeg@770x510",
      "url"  : "http://www.infzm.com/content/136473"
}

dsBridge.call('onSetShare', shareObject, (res)=>{
    //callback
})

onOpenNativeLogin

WARNING

建议前端开发者使用 onOpenNativePage API 调起登录页

示例

javascript
const openPageObject  = {
    "link_type": "17",
    "force": "0" //仅打开登录页,无后续绑定手机逻辑
}

dsBridge.call('onOpenNativePage', openPageObject, (res)=>{
    //callback
})

onOpenNativePage

参数

apiName: String

onOpenNativePage

openPageObject: JSONObject

数据结构参考APP 路由参数支持表

json
{
  "version": "1.1.0",
  "link_type": "10",
  "terms_id": "6"
}
callback(res: Boolean)

API 调用成功后的回调

返回值典型值类型备注
resopenNativePage-Success / openNativePage-FailJSONObject数据结构

openNativePage-Success

json
{
  "code": "200",
  "msg": "路由存在"
}

openNativePage-Fail

json
{
  "code": "404",
  "msg": "路由不存在"
}

示例

javascript
const openPageObject  = {
    "version"  : "1.1.0",
    "link_type": "10",
    "terms_id" : "6"
}

dsBridge.call('onOpenNativePage', openPageObject, (code = { res })=>{
    
})