{"version":3,"sources":["tplus-api/../src/api/request-interceptor.ts"],"names":[],"mappings":"AA+BA,QAAA,MAAM,UAAU,sBAoFf,CAAC;AAEF,eAAe,UAAU,CAAC","file":"request-interceptor.d.ts","sourcesContent":["import _findIndex from 'lodash/findIndex'; \nvar crypto = require('crypto');\nimport User from '../model/User';\nimport {\n  cspAk,\n  tplusHostArray,\n  ciaHostArray,\n  devProxyHost,\n  prodProxyHost,\n  noProxyHostArray\n} from '../const/app';\nimport {env} from 'mutants-microfx';\nimport { url } from 'inspector';\nconst {constant,platform,browser,os} = env;\n\nfunction getAppCloudAuthInfo(orgId) {\n\n  const user = User.restore();\n  if (!user) {\n    return '';\n  }\n  var params = {\n    appKey: atob(cspAk),\n    orgId: orgId ? orgId : user.orgId,\n    userUid: user.userId\n  }\n  var params_string = JSON.stringify(params);\n  var hash = crypto.createHash('md5').update(params_string).digest(\"hex\");\n  return new Buffer(hash + ',' + params_string).toString('base64');\n}\n\nconst middleware = function (axios) {\n  axios.interceptors.request.use(\n    function proxyRequest(cfg) {\n      let user = User.restore();\n      //使用在线的代理服务器 解决跨域问题\n      let proxyURL = user && user.proxyserver?user.proxyserver:prodProxyHost;\n      //如果调试时在启动本地代理\n      //_DEV_是否启用模拟数据\n      // if (__DEV__) {\n      //   proxyURL = devProxyHost;\n      // }else{\n      //   proxyURL = prodProxyHost;\n      // }\n      if (cfg.url.indexOf('http') == -1) {\n        cfg.url = `${cfg.baseURL}${cfg.url}`\n      }\n      let noProxyHostIndex = _findIndex(noProxyHostArray, (url) => {\n        return cfg.url.indexOf(url) !== -1;\n      });\n      if (proxyURL && noProxyHostIndex === -1) {\n        if(platform == constant.platform.chanjet && window.location.protocol.indexOf('file') > -1){\n           // 工作圈模式下  http转为file协议  不用使用代理\n        }else{\n          cfg.url = `${proxyURL}/?url=${cfg.url}`;\n        }\n      }\n      \n        let tplusHostIndex = _findIndex(tplusHostArray, (url) => {\n          return cfg.baseURL.indexOf(url) !== -1;\n        });\n        if (tplusHostIndex !== -1) {\n          let authInfo = getAppCloudAuthInfo(cfg.data && cfg.data.targetOrgID ? cfg.data.targetOrgID : '');\n          cfg.headers['authInfo'] = authInfo;\n        }\n\n        \n        \n        if (user.channel && user.channel.throughProxy) {\n          cfg.headers['appkey'] = atob(cspAk);\n          if (user) {\n            cfg.headers['userid'] = user.userId;\n            cfg.headers['orgid'] = user.orgId;\n            cfg.headers['token'] = user.accessToken;\n          }\n        }\n        if(cfg.headers['checkThroughProxy'] && cfg.headers['checkOrgId']){\n          cfg.headers['appkey'] = atob(cspAk);\n          if (user) {\n            cfg.headers['userid'] = user.userId;\n            cfg.headers['orgid'] = cfg.headers['checkOrgId'];\n            cfg.headers['token'] = user.accessToken;\n          }\n        }\n       \n        //支持saas版本\n        if (user && user.sid && !cfg.headers['checkThroughProxy']) {\n          cfg.headers['tplussid'] = user.sid;\n        }\n\n        if(cfg.url.indexOf('/api/v2/')>0){\n          cfg.headers['openToken'] = getAppCloudAuthInfo('');\n          cfg.headers['appkey'] = atob(cspAk);\n        }\n\n        delete cfg.headers['checkThroughProxy'];\n        delete cfg.headers['checkOrgId'];\n        \n        let ciaHostIndex = _findIndex(ciaHostArray, (url) => {\n          return cfg.baseURL.indexOf(url) !== -1;\n        });\n        if (ciaHostIndex === -1 && user && user.tplusToken) {\n          if(!!cfg.headers['Authorization']){\n            //Authorization如果有值  用调用方的值  不覆盖\n          }else{\n            cfg.headers['Authorization'] = `token ${user.tplusToken}`;\n          }\n        }\n      return cfg;\n    },\n\n    function promiseError(error) {\n      return Promise.reject(error);\n    }\n  );\n};\n\nexport default middleware;\n"]}