IndexController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // $contentpages=$ga->contentpages($store);
  56. // $this->addContentpages($contentpages,$store);
  57. }
  58. public function addContentpages($contentpages,$store){
  59. $i=1;
  60. $limit=20;
  61. $modelKey=[
  62. 'pagePath',
  63. 'store',
  64. 'date',
  65. 'pageviews',
  66. 'uniquePageviews',
  67. 'avgTimeOnPage',
  68. 'entrances',
  69. 'bounceRate',
  70. 'exitRate',
  71. 'pageValue',
  72. 'created_at',
  73. ];
  74. if(!empty($contentpages)){
  75. $list=[];
  76. foreach($contentpages as $k=>$v){
  77. $item=[
  78. $v['pagePath'],
  79. $store,
  80. date('Y-m-d',strtotime($v['date'])),
  81. $v['pageviews'],
  82. $v['uniquePageviews'],
  83. $v['avgTimeOnPage'],
  84. $v['entrances'],
  85. $v['bounceRate'],
  86. $v['exitRate'],
  87. $v['pageValue'],
  88. time(),
  89. ];
  90. $list[]=$item;
  91. if($i%$limit==0||$i==count($contentpages)){
  92. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  93. $list=[];
  94. }
  95. $i++;
  96. }
  97. }
  98. }
  99. }