IndexController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\filters\AccessControl;
  5. use yii\web\Controller;
  6. use yii\web\Response;
  7. use yii\filters\VerbFilter;
  8. use app\lib\GA;
  9. use app\models\ContentPage;
  10. use yii\data\Pagination;
  11. class IndexController extends Controller
  12. {
  13. public function actionIndex(){
  14. echo 111;exit;
  15. }
  16. public function actionContentpage(){
  17. $request = Yii::$app->request;
  18. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  19. $store = $request->get('store', "supernova");
  20. $limit = $request->get('limit', "10");
  21. $page = $request->get('page', "1");
  22. $page=$page-1>=0?$page-1:0;
  23. $where=[];
  24. if(!$store){
  25. return ['status'=>false,'msg'=>"参数异常"];
  26. }
  27. $where['store']=$store;
  28. $query=ContentPage::find()->where(["store"=>$where]);
  29. $count = $query->count();
  30. $pagination = new Pagination(['totalCount' => $count,'pageSize'=>$limit,'page'=>$page]);
  31. $items = $query->offset($pagination->offset)
  32. ->limit($pagination->limit)
  33. ->orderBy([
  34. 'pageviews'=>SORT_DESC,
  35. 'id'=>SORT_DESC
  36. ])
  37. ->all();
  38. foreach($items as $k=>$v){
  39. $items[$k]['created_at']=date('Y-m-d H:i:s',$v['created_at']);
  40. }
  41. $data=[
  42. 'total'=>$count,
  43. 'items'=>$items,
  44. ];
  45. return [
  46. 'code' => 200,
  47. 'data'=>$data,
  48. 'status'=>true
  49. ];
  50. }
  51. public function actionSynsdata(){
  52. $ga=new GA();
  53. $store="supernova";
  54. $data=$ga->trafficsource($store);
  55. exit;
  56. // $contentpages=$ga->contentpages($store);
  57. // $this->addContentpages($contentpages,$store);
  58. }
  59. public function addContentpages($contentpages,$store){
  60. $i=1;
  61. $limit=20;
  62. $modelKey=[
  63. 'pagePath',
  64. 'store',
  65. 'date',
  66. 'pageviews',
  67. 'uniquePageviews',
  68. 'avgTimeOnPage',
  69. 'entrances',
  70. 'bounceRate',
  71. 'exitRate',
  72. 'pageValue',
  73. 'created_at',
  74. ];
  75. if(!empty($contentpages)){
  76. $list=[];
  77. foreach($contentpages as $k=>$v){
  78. $item=[
  79. $v['pagePath'],
  80. $store,
  81. date('Y-m-d',strtotime($v['date'])),
  82. $v['pageviews'],
  83. $v['uniquePageviews'],
  84. $v['avgTimeOnPage'],
  85. $v['entrances'],
  86. $v['bounceRate'],
  87. $v['exitRate'],
  88. $v['pageValue'],
  89. time(),
  90. ];
  91. $list[]=$item;
  92. if($i%$limit==0||$i==count($contentpages)){
  93. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  94. $list=[];
  95. }
  96. $i++;
  97. }
  98. }
  99. }
  100. }