接口调用适配环境方案(一)

适用于前端请求后端的场景

一、前端函数



function (event) {
  var viewModel = this;
  var serviceUrl = cb.utils.getServiceUrl();
  var currrentParams = event.params.value;
  if(currrentParams===null)return;
  cb.rest.invokeFunction("GT31545AT9.openapi.salesDelegateOpenApi", {params:currrentParams,envUrl:serviceUrl},
  function(err, res) {
    //根据返回结果设置
  })
}

二、接口调用示例-销售委托关系列表查询

前端函数调用此API函数,此API函数内获取当前环境的配置信息函数


let AbstractAPIHandler = require('AbstractAPIHandler');
  class MyAPIHandler extends AbstractAPIHandler {
   execute(request){
     //获取当前环境的配置信息函数
     let envConfigFun = extrequire("GT31545AT9.openapi.getCurrentEnvConfig");
     let envConfig = envConfigFun.execute(request.envUrl).configParams;
     let url = envConfig.apiurl.salesDelegate;
     var pageIndex = "1";
     //公有云的客开项目一般不会存在大量的委托关系,如果确实出现了,
     //也不应该递归调用接口去查找默认委托关系,请提交工单接口支持传【是否默认】参数进行过滤
     //此处默认只查找至多100条数据
     var pageSize = "100";
     let body = {
       "pageIndex":pageIndex,
       "pageSize":pageSize,
       "salesOrg":request.params.salesOrgId
     };
     var salesDelegateDefaultData;
     let apiResponse = openLinker("POST", url, envConfig.appcode, JSON.stringify(body));
     apiResponse = JSON.parse(apiResponse);
     if(apiResponse.data===undefined||apiResponse.data===null)return;
     if(apiResponse.data.recordCount>0){
      for(let index = 0;index<apiResponse.data.recordCount;index++){
        let currentData = apiResponse.data.recordList[index];
        if(currentData.isDefault ==1){
          salesDelegateDefaultData = currentData;
          break;
        }
      } 
     }
     return {salesDelegateDefaultData};
 }
}
exports({"entryPoint":MyAPIHandler});

三、获取当前环境的配置信息函数

调用公共参数获取具体的请求地址


let AbstractTrigger = require('AbstractTrigger');
  class MyTrigger extends AbstractTrigger {
    execute(context,param){
      let envUrl = context;
      //对于客开而言,只有沙箱环境(商用开发环境)和生产环境
      let sandbox = "dbox.diwork.com";
      var envParam = "sandbox"; //默认沙箱
      if(envUrl.indexOf(sandbox)==-1){
        //说明当前环境是生产
        envParam = "production";
      }
      //调用公共参数
      let configParamsFun = extrequire("GT31545AT9.openapi.commonParams");
      let configParams = configParamsFun.execute(envParam).currentEnvParams;
     return {configParams};
    }
  }
  exports({"entryPoint":MyTrigger});

四、公共参数函数

地址信息都写在此处,方便配置和调整


let AbstractTrigger = require('AbstractTrigger');
  class MyTrigger extends AbstractTrigger {
    execute(context,param){
      let appcode = "GT31545AT9";
      let data = {
        "sandbox":{
          "appcode":appcode,
          "apiurl":{
            "salesDelegate":"https://dbox.diwork.com/iuap-api-gateway/yonbip/digitalModel/salesDelegate/list",
          }
        },
        "production":{
          "appcode":appcode,
          "apiurl":{
            "salesDelegate":"https://yonbip.diwork.com/iuap-api-gateway/yonbip/digitalModel/salesDelegate/list",
          }
        }
      }
      let currentEnvParams = data[context]
     return {currentEnvParams};
    }
  }
  exports({"entryPoint":MyTrigger});
Copyright © 用友 -【生态技术部】 2021 all right reserved,powered by Gitbook修订时间: 2022-06-29 15:37:46

results matching ""

    No results matching ""