当我尝试在离子3项目中获取数据时响应延迟

时间:2017-09-04 10:09:07

标签: angular typescript ionic3

我在离子项目中使用 codeigniter 的restful服务。当我尝试获取数据时,从我的服务器获取数据需要4-8秒。任何人都可以建议我使用什么而不是宁静的服务。我有 mysql 作为我的数据库。我已经包含了我的角度组件,角度服务,codeigniter控制器和codeigniter模型代码。请帮助我开发我的Android应用程序作为新的离子。

这是我的角度组件代码: -

         ngOnInit() {
            this.form = new FormGroup({
                state: new FormControl('', [Validators.required]),
                city: new FormControl('', [Validators.required]),
                capacity: new FormControl('', [Validators.required]),
                fuelType: new FormControl('', [Validators.required]),
                duration: new FormControl(2, [Validators.required]),
                timeRange: new FormControl('weeklyrates', [Validators.required]),
            });

            this.vehicleType = this.navParams.get('vehicleType');

            this.getStates();

            this.form.controls['state'].valueChanges.subscribe((state) => {
                this.mainService.getCities(state, this.vehicleType).subscribe((cities: any) => {
                    this.cities = cities;
                    //console.log(this.cities);
                });
            });


            this.mainService.getCapacityFuel(this.vehicleType).subscribe(
                (data) => {
                    this.capacities = data[0];
                    this.fuelType = data[1];
                }
            );


        }

        public getStates() {
            this.mainService.getState(this.vehicleType).subscribe((states: any) => {
                this.states = states;
                // console.log(states);

            });
        }

角度服务代码:

        public getState(vehicleType) {
             vehicleType = JSON.stringify(vehicleType);
             return this.http.post(this.path + "Api_iphone/index.php/api/Forklift_Controller/getStates", vehicleType)
                 .do((data) => console.log(data))
                 .map((data) => {
                     return data.json();

                 });
         }

         public getCities(state, vehicleType) {

             let data = JSON.stringify({
                 state: state,
                 vehicleType: vehicleType
             });

             return this.http.post(this.path + "Api_iphone/index.php/api/Forklift_Controller/getCities", data)
                 .do((data) => console.log(data))
                 .map((data) => {
                     return data.json();

                 });
         }

         public getCapacityFuel(vehicleType) {
             vehicleType = JSON.stringify(vehicleType);
             return this.http.post(this.path + "Api_iphone/index.php/api/Forklift_Controller/getCapacityFuel", vehicleType)
                 .map((data) => {
                     return data.json();

                 });
         }

         public calculateHireRate(formData) {
             formData = JSON.stringify(formData);
             return this.http.post(this.path + "Api_iphone/index.php/api/Forklift_Controller/calculateHireRate", formData)
                 .map((data) => {
                     return data.json();

                 });
         }

CI控制器代码:

<?php
        defined('BASEPATH') OR exit('No direct script access allowed');
        require APPPATH . 'libraries/REST_Controller.php';
        class Forklift_Controller extends REST_Controller
        {

            function __construct(   )
            {
                parent::__construct();
                $this->load->model('forklift_Model');
            }


        public function getStates_post() {
            $vehicleType = json_decode(file_get_contents('php://input'));
        echo $vehicleType;
            $this->response(json_encode($this->forklift_Mokdel->getStates($vehicleType)));
        }

        public function getCities_post() {
            $data = json_decode(file_get_contents('php://input'));
            //echo json_encode($data);
            $this->response(json_encode($this->forklift_Model->getCities($data->{'state'}, $data->{'vehicleType'})));
        }

        public function getCapacityFuel() {
            $vehicleType = json_decode(file_get_contents('php://input'));
            echo json_encode($this->forklift_Model->getCapacityFuel($vehicleType));

        } 

        public function calculateHireRate() {
            $formData = json_decode(file_get_contents('php://input'));
            echo json_encode($this->forklift_Model->calculateHireRate($formData));
        }


        }//Class ends...          

这是我的CI型号代码: -

<?php

    class Forklift_Model extends CI_Model
    {

        function __construct()
        {
            parent::__construct();
        }



    public function getStates($vehicleType) {
        $query = $this->db->select('statename')->get('wp_forklift_state_master');
                          //->where(['type'=> $vehicleType])

        return $query->result();              

    }

    public function getCities($state, $vehicleType) {
        $query = $this->db->select('cityname')
                          ->where(['statename'=>$state])
                          ->get('wp_forklift_city_master')
                          ->result();

                          return $query;
    }


    public function getCapacityFuel($vehicleType) {
        $capacity = $this->db->select('capacity')
                              ->where(['type'=> $vehicleType])                        
                              ->order_by('capacity', 'ASC')
                              ->distinct()
                              ->get('wp_forklift_master')

                          ->result();

        $fuel =  $this->db->select('fuel')
                          ->where(['type'=> $vehicleType])
                          ->distinct()
                          ->get('wp_forklift_master')
                          ->result();   


                          return [$capacity, $fuel];
    }


     public function calculateHireRate($formData) {

        if($formData->{'timeRange'} == 'dailyrates') {

            $rate = $this->db->select('*')
                         ->from('wp_forklift_master fm')
                         ->join('wp_forklift_company_master fcm', 'fcm.companyid = fm.companyid')
                         ->where(['city'=> $formData->{'city'},
                                  'state'=> $formData->{'state'},
                                  'capacity' => $formData->{'capacity'},
                                  'fuel'=> $formData->{'fuelType'}, ])
                         ->distinct()
                         ->order_by('dailyrates', 'ASC')
                         ->get()
                         ->result();

                         return $rate;

        }elseif($formData->{'timeRange'} == 'weeklyrates') {

            $rate = $this->db->select('*')
                         ->where(['city'=> $formData->{'city'},
                                  'state'=> $formData->{'state'},
                                  'capacity' => $formData->{'capacity'},
                                  'fuel'=> $formData->{'fuelType'}, ])
                         ->distinct()
                         ->order_by('weeklyrates', 'ASC')
                         ->get('wp_forklift_master')
                         ->result();

                         return $rate;

        }elseif($formData->{'timeRange'} == 'quaterlyrates') {

            $rate = $this->db->select('*')
                         ->where(['city'=> $formData->{'city'},
                                  'state'=> $formData->{'state'},
                                  'capacity' => $formData->{'capacity'},
                                  'fuel'=> $formData->{'fuelType'}, ])
                         ->distinct()
                         ->order_by('quaterlyrates', 'ASC')
                         ->get('wp_forklift_master')
                         ->result();

                         return $rate;

        }elseif($formData->{'timeRange'} == 'yearlyrates') {

            $rate = $this->db->select('*')
                         ->where(['city'=> $formData->{'city'},
                                  'state'=> $formData->{'state'},
                                  'capacity' => $formData->{'capacity'},
                                  'fuel'=> $formData->{'fuelType'}, ])
                         ->distinct()
                         ->order_by('yearlyrates', 'ASC')
                         ->get('wp_forklift_master')
                         ->result();

                         return $rate;
        }                                                   
     }
    } //Class Ends...

0 个答案:

没有答案