chengwl 4 vuotta sitten
vanhempi
commit
44269b9208
4 muutettua tiedostoa jossa 60 lisäystä ja 13 poistoa
  1. 12 0
      config/alipearl.json
  2. 0 0
      config/supernova.json
  3. 16 11
      controllers/IndexController.php
  4. 32 2
      lib/GA.php

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 12 - 0
config/alipearl.json


config/savvy-theory-278005-43552bf9fe94.json → config/supernova.json


+ 16 - 11
controllers/IndexController.php

@@ -185,22 +185,27 @@ class IndexController extends \app\controllers\HomeController
     public function actionStatistic(){
       $ga=new GA();
       $request = Yii::$app->request;
-      $store=$request->get('store',"supernova");
+      $store=$request->get('store');
       $start_day=$request->get('start', date('Y-m-d',time()-3600*24*8));
       $end_day = $request->get('end', date('Y-m-d',time()-3600*24));
+      if(!$store){
+        $this->json(0,[],"缺少参数");
+      }
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;     
       //销量
-      $sells=$ga->visitors_geo($store,$start_day,$end_day);
-      $product=$ga->product($store,$start_day,$end_day);
-      $visitors=$ga->visitors_type($store,$start_day,$end_day);
-      return [
-        'status'=>1,
-        'data'=>[
-          'sell'=>$sells,
-          'product'=>$product,
-          'visitor'=>$visitors,
-        ],
+      try{
+        $sells=$ga->visitors_geo($store,$start_day,$end_day);
+        $product=$ga->product($store,$start_day,$end_day);
+        $visitors=$ga->visitors_type($store,$start_day,$end_day);
+      }catch(\Exception $e){
+         $this->json(0,[],$e->getMessage());
+      }
+      $data=[
+        'sell'=>$sells,
+        'product'=>$product,
+        'visitor'=>$visitors,
       ];
+      $this->json(1,$data);
     }
     public function actionProductinfo(){
       $ga=new GA();

+ 32 - 2
lib/GA.php

@@ -1,9 +1,15 @@
 <?php
 namespace app\lib;
 class GA{
+    const SUPERNOVA_KEY_PATH= BASE_DIR . 'config/supernova.json';
+    const ALIPEARL_KEY_PATH= BASE_DIR . 'config/alipearl.json';
     private static $insances=[];
     private static function initializeAnalytics($dp){
-        $KEY_FILE_LOCATION = BASE_DIR . 'config/savvy-theory-278005-43552bf9fe94.json';
+
+        $KEY_FILE_LOCATION = self::getPath($dp);
+        if(!$KEY_FILE_LOCATION){
+            throw new  Exception("store 配置不存在");
+        }
         $client = new \Google_Client();
         $client->setApplicationName("Hello Analytics Reporting");
         $client->setAuthConfig($KEY_FILE_LOCATION);
@@ -21,7 +27,31 @@ class GA{
         }
     }
     private function getViewId($dp){
-        return "155703065";
+        $VIEW_ID="";
+        switch($dp){
+            case "1"://supernovahair
+                $VIEW_ID="155703065";
+            break;
+            case "3"://alipearlhair
+                $VIEW_ID="148377136";
+            break;
+        }
+        return $VIEW_ID;
+    }
+    public static function getPath($dp){
+        $path="";
+        switch($dp){
+            case "1"://supernovahair
+                $path=SUPERNOVA_KEY_PATH;
+            break;
+            case "3"://alipearlhair
+                $path=ALIPEARL_KEY_PATH;
+            break;
+        }
+        return $path;
+        
+        
+
     }
     public function contentpages($dp,$start_at,$end_at){
         $analytics=self::getInstance($dp);