queryUtils.js 2.42 KB

import {loadModules} from 'esri-loader'

export default{

    methods: {
        queryByWhere(url,queryWhere,geometry,returnGeometry,outFields ,outSpatialReference ,callBackFunction){
            var self = this;
            loadModules([
                "esri/tasks/QueryTask",
                "esri/tasks/support/Query"
            ]).then(([
                QueryTask,
                Query
            ]) => {
                var queryTask = new QueryTask({
                    url: url
                  }),
                 query = new Query(),
                  where = "";
                  if (queryWhere){
                    var parames = self.parseObj2Arr(queryWhere);
                    //var isFrist = false;
                    for(var i = 0;i < parames.length; i++ ){
                        if(i == 0){

                        }else {
                            where += " and ";
                        }
                        if(Array.isArray(parames[i].value)){
                            where += parames[i].name + " in (" + parames[i].value.toString() + ")";
                        }else if(typeof parames[i].value == 'number'){
                            where +=  parames[i].name + " =" + parames[i].value ;
                        }else{
                            where += parames[i].name  + " like " + "'%" + parames[i].value +"%'";
                        }
                    }
                  } else {
                     where = "1=1";
                  }

                  query.where = where;
                  if(geometry){
                    query.geometry = geometry;
                  }
                  query.outSpatialReference = outSpatialReference ? outSpatialReference : {wkid:3857}
                  query.returnGeometry = returnGeometry ? true : false;
                  query.outFields = outFields ? outFields : ["*"];
                  queryTask.execute(query).then(function(results){
                     if(callBackFunction && typeof callBackFunction == 'function'){
                        callBackFunction(results);
                     }
                  });
            }).catch( err => {
                throw(err);
            })
        },
        parseObj2Arr(object){
          var arr = [];
          for(var key in object){
            var obj = {};
            obj.name = key;
            obj.value = object[key];
            arr.push(obj);
          }
          return arr;
        }        
    },
}