let OS = ''
let sdkFn = {}
let SDKMap = {
QYWX: {
_isInit: false,
_init(){
return new Promise((resolve, reject) => {
import('wecomjsdk').then( async (wx) => {
let data = await fetchConfigData()
const {corpId, agentId, timestamp, nonceStr, signature} = data
wx.agentConfig({
corpid: corpId, // 必填,企业微信的corpid,必须与当前登录的企业一致
agentid: agentId, // 必填,企业微信的应用id (e.g. 1000247)
timestamp, // 必填,生成签名的时间戳
nonceStr, // 必填,生成签名的随机串
signature,// 必填,签名,见附录-JS-SDK使用权限签名算法
jsApiList: [
'closeWindow', 'scanQRCode', 'openDefaultBrowser',
], //必填,传入需要使用的接口名称
success: () => {
sdkFn = wx.default
this._isInit = true
resolve()
},
fail: (err) => {
reject(err)
if(err.errMsg.indexOf('function not exist') > -1){
alert('企微版本过低请升级')
}
},
});
})
})
},
exit(){
sdkFn.closeWindow()
},
openDefaultBrowser(url){
sdkFn.invoke('openDefaultBrowser', {
'url': url,
}, function(res){
console.log('err res: ', res);
if(res.err_msg !== "openDefaultBrowser:ok"){
//错误处理
}
});
},
},
DD: {
_isInit: false,
_init(){
return import('dingtalk-jsapi').then((dd) => {
sdkFn = dd.default
})
},
exit(){
sdkFn.biz.navigation.close({
onSuccess : function() {},
onFail : function() {},
})
},
hideBar(){
sdkFn.biz.navigation.hideBar({
hidden: true,
onSuccess : function() {},
onFail : function() {},
})
},
openDefaultBrowser(url){
sdkFn.biz.util.openLink({
url: url,//要打开链接的地址
onSuccess : function() {
/**/
},
onFail : function() {},
})
},
},
FS: {
_isInit: false,
exit(){},
},
};
function SDK(){
let sdk = SDKMap[OS] || {}
let handler = {
get: function(target, property) {
let origin = target[property]
if(property[0] !== '_'){ // 不代理_属性
// 代理sdk上的方法,使其确保在init以后调用
if(typeof origin === 'function'){
origin = new Proxy(origin, {
apply: async function(fnTarget, thisArg, argumentsList){
if(!target._isInit){
await target._init()
}
return fnTarget(...argumentsList)
},
})
} else { // 若没有定义,直接返回空函,避免报错,影响程序
return () => {}
}
}
return origin
},
}
let sdkP = new Proxy(sdk, handler)
return sdkP
}
export default new SDK()