const system = require("../../../system");
var settings = require("../../../../config/settings");
class MetaService {
   constructor() {
      this.cacheManager = system.getObject("db.common.cacheManager");
      this.restS=system.getObject("util.restClient");
   }
   async getApiDoc(appid) {
      var p = settings.basepath + "/app/base/db/impl/common/apiDocManager.js";
      var ClassObj = require(p);
      var obj = new ClassObj();
      return obj.doc;
   }
   async getUiConfig(appid) {
      const cfg = await this.cacheManager["UIConfigCache"].cache(appid, null, 60);
      return cfg;
   }
   async getBaseComp() {
      var basecomp = await this.apiCallWithAk(settings.paasUrl() + "api/meta/baseComp/getBaseComp", {});
      return basecomp.basecom;
   }
   async findAuthsByRole(rolesarray, appid) {
      var result = await this.apiCallWithAk(settings.paasUrl() + "api/auth/roleAuth/findAuthsByRole", {
         roles: rolesarray,
         appid: appid
      });
      return result;
   }
   async apiCallWithAk(url,params){
      var acckapp=await this.cacheManager["ApiAccessKeyCache"].cache(settings.appKey);
      var acck=acckapp.accessKey;
      //按照访问token
      var restResult=await this.restS.execPostWithAK(params,url,acck);
      if(restResult){
        if(restResult.status==0){
          var resultRtn=restResult.data;
          return resultRtn;
        }else{
          await  this.cacheManager["ApiAccessKeyCache"].invalidate(settings.appKey);
          return null;
        }
      }
      return null;
    }
}
module.exports = MetaService;
