import
引入 import {cxJsBridgeWebApiInit} from 'xxx/cx-sdk'
let cxApi=null;
export const getCxApi=async (appKey)=>{
return cxApi || await cxJsBridgeWebApiInit(appKey);
}
script
标签引入 【不推荐】ES6
语法的项目。 <script src="xxx/cx-sdk.js"></script>
var cxApi=null;
cxSdk.cxJsBridgeWebApiInit(appKey,function(data){
cxApi=data;
},function(error){
console.log(error)
})
Promise
链式调用cxApi.login(params).then((data)=>{
// toDo
}).error(e=>{
// toDo
)
async/await
【捕捉错误信息需要在 catch
中】 async function getOAuthCode (params){
try{
const data=await cxApi.login(params);
return data;
}catch(e){
return false;
}
}
cxApi.login(
{
appKey:"xxx",
enterpriseId:'xxxx',
onSuccess:(data)=>{
// toDo
},
onFail:(error)=>{
// toDo
}
}
);
data
中内容,不返回code=0
,不需要 判断 code=0
,如果调用失败返回格式·{code:xx,msg:'xxxx'}