1. let OS = ''
    2. let sdkFn = {}
    3. let SDKMap = {
    4. QYWX: {
    5. _isInit: false,
    6. _init(){
    7. return new Promise((resolve, reject) => {
    8. import('wecomjsdk').then( async (wx) => {
    9. let data = await fetchConfigData()
    10. const {corpId, agentId, timestamp, nonceStr, signature} = data
    11. wx.agentConfig({
    12. corpid: corpId, // 必填,企业微信的corpid,必须与当前登录的企业一致
    13. agentid: agentId, // 必填,企业微信的应用id (e.g. 1000247)
    14. timestamp, // 必填,生成签名的时间戳
    15. nonceStr, // 必填,生成签名的随机串
    16. signature,// 必填,签名,见附录-JS-SDK使用权限签名算法
    17. jsApiList: [
    18. 'closeWindow', 'scanQRCode', 'openDefaultBrowser',
    19. ], //必填,传入需要使用的接口名称
    20. success: () => {
    21. sdkFn = wx.default
    22. this._isInit = true
    23. resolve()
    24. },
    25. fail: (err) => {
    26. reject(err)
    27. if(err.errMsg.indexOf('function not exist') > -1){
    28. alert('企微版本过低请升级')
    29. }
    30. },
    31. });
    32. })
    33. })
    34. },
    35. exit(){
    36. sdkFn.closeWindow()
    37. },
    38. openDefaultBrowser(url){
    39. sdkFn.invoke('openDefaultBrowser', {
    40. 'url': url,
    41. }, function(res){
    42. console.log('err res: ', res);
    43. if(res.err_msg !== "openDefaultBrowser:ok"){
    44. //错误处理
    45. }
    46. });
    47. },
    48. },
    49. DD: {
    50. _isInit: false,
    51. _init(){
    52. return import('dingtalk-jsapi').then((dd) => {
    53. sdkFn = dd.default
    54. })
    55. },
    56. exit(){
    57. sdkFn.biz.navigation.close({
    58. onSuccess : function() {},
    59. onFail : function() {},
    60. })
    61. },
    62. hideBar(){
    63. sdkFn.biz.navigation.hideBar({
    64. hidden: true,
    65. onSuccess : function() {},
    66. onFail : function() {},
    67. })
    68. },
    69. openDefaultBrowser(url){
    70. sdkFn.biz.util.openLink({
    71. url: url,//要打开链接的地址
    72. onSuccess : function() {
    73. /**/
    74. },
    75. onFail : function() {},
    76. })
    77. },
    78. },
    79. FS: {
    80. _isInit: false,
    81. exit(){},
    82. },
    83. };
    84. function SDK(){
    85. let sdk = SDKMap[OS] || {}
    86. let handler = {
    87. get: function(target, property) {
    88. let origin = target[property]
    89. if(property[0] !== '_'){ // 不代理_属性
    90. // 代理sdk上的方法,使其确保在init以后调用
    91. if(typeof origin === 'function'){
    92. origin = new Proxy(origin, {
    93. apply: async function(fnTarget, thisArg, argumentsList){
    94. if(!target._isInit){
    95. await target._init()
    96. }
    97. return fnTarget(...argumentsList)
    98. },
    99. })
    100. } else { // 若没有定义,直接返回空函,避免报错,影响程序
    101. return () => {}
    102. }
    103. }
    104. return origin
    105. },
    106. }
    107. let sdkP = new Proxy(sdk, handler)
    108. return sdkP
    109. }
    110. export default new SDK()