ソースを参照

记录视频的pv,uv

lizhen_gitee 9 ヶ月 前
コミット
c65ed0232a

+ 46 - 3
application/api/controller/Tvindex.php

@@ -4,7 +4,7 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use think\Db;
-
+use Redis;
 class Tvindex extends Api
 {
     protected $noNeedLogin = ['indexdata','video_list','videoinfo','zhuanti','tudingc','search','history'];
@@ -165,7 +165,7 @@ class Tvindex extends Api
             ->orderRaw('rand()')->limit(4)->select();
         $guess_list = list_domain_image($guess_list, ['image']);
 
-        //记录浏览历史
+        //记录浏览历史,给浏览历史列表用的,即时的
         if($this->auth->isLogin()){
             $view_map = [
                 'user_id'  => $this->auth->id,
@@ -181,6 +181,9 @@ class Tvindex extends Api
             }
         }
 
+        //记录pv,uv
+        $this->video_pvuv($id);
+
         //
         $result = [
             'video_info' => $info,
@@ -213,7 +216,7 @@ class Tvindex extends Api
         $info['is_collection'] = Db::name('video_collection')->where(['user_id' => $this->auth->id, 'video_id' => $id])->count('id');
         $info['is_good']       = Db::name('video_good')->where(['user_id' => $this->auth->id, 'video_id' => $id])->count('id');
 
-        //记录浏览历史
+        //记录浏览历史,给浏览历史列表用的,即时的
         if($this->auth->isLogin()){
             $view_map = [
                 'user_id'  => $this->auth->id,
@@ -229,6 +232,9 @@ class Tvindex extends Api
             }
         }
 
+        //记录pv,uv
+        $this->video_pvuv($id);
+
         //
         $result = [
             'video_info' => $info,
@@ -624,4 +630,41 @@ class Tvindex extends Api
         $decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
         return $decrypted;
     }
+
+    //共有方法,
+    private function video_pvuv($video_id = 0){
+        if(empty($video_id)) {return true;}
+
+        //开启redis
+        $redis = new Redis();
+        $config = config('redis');
+        $redis->connect($config['redis_host'], $config['redis_port']);
+        if ($config['redis_pwd']) {
+            $redis->auth($config['redis_pwd']);
+        }
+        if($config['redis_selectdb'] > 0){
+            $redis->select($config['redis_selectdb']);
+        }
+
+        //今天
+        $today = date('Y-m-d');
+
+        //今日pv+1
+        $today_pv_key = $today.'_video_pv_'.$video_id;
+        $redis->incr($today_pv_key,1);
+
+        //今日uv+1
+        if($this->auth->isLogin() && $this->auth->id){
+            $today_uv_key = $today.'_video_uv_'.$video_id;
+            $uid = $this->auth->id;
+            $redis->sAdd($today_uv_key,$uid);
+        }
+
+        //今日视频id列表
+        $today_video_key = $today.'_video_list';
+        $redis->sAdd($today_video_key,$video_id);
+    }
+    /*public function test(){
+        $this->video_pvuv(input('video_id',0));
+    }*/
 }

+ 8 - 5
application/config.php

@@ -334,11 +334,14 @@ return [
     ],
     //logo
     'logo' => 'https://jiankang.com/logo.png',
-    //Redis配置
-    'redis_host' => '127.0.0.1', //ip地址
-    'redis_port' => '6379', //端口
-    'redis_pwd'  => '', //密码
-    'redis_selectdb' => 8,
+    //Redis配置,pv,uv之类用的
+    'redis' => [
+        'redis_host' => '127.0.0.1', //ip地址
+        'redis_port' => '6379', //端口
+        'redis_pwd'  => '', //密码
+        'redis_selectdb' => 5,
+    ],
+
     //银河配置
     'cp_id' => 126, //银河编码ID
     'cspid' => 'dafujujiajkej', //Cspid华为

+ 74 - 0
application/index/controller/Plantask.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace app\index\controller;
+
+use think\Controller;
+use think\Db;
+use Redis;
+
+class Plantask extends Controller
+{
+
+
+    public function index()
+    {
+        exit;
+    }
+
+    //昨天的pvuv数据从redis转移到database 并清空昨天的redis
+    public function auto_pvuv_redis_to_database(){
+        //开启redis
+        $redis = new Redis();
+        $config = config('redis');
+        $redis->connect($config['redis_host'], $config['redis_port']);
+        if ($config['redis_pwd']) {
+            $redis->auth($config['redis_pwd']);
+        }
+        if($config['redis_selectdb'] > 0){
+            $redis->select($config['redis_selectdb']);
+        }
+
+        //日期
+        $today = date('Y-m-d');
+        $yestodaytime = strtotime($today)-86400;
+        $yestoday = date('Y-m-d',$yestodaytime);
+
+        //$yestoday = $today;//测试临时用
+
+        //视频id 列表
+        $yestoday_video_key = $yestoday.'_video_list';
+        $yestoday_video_list = $redis->sMembers($yestoday_video_key);
+
+        if(empty($yestoday_video_list)){
+            echo $yestoday_video_key.': empty';exit;
+        }
+
+        foreach($yestoday_video_list as $key => $video_id){
+
+            //准备数据
+            $date_pvuv = [
+                'video_id' => $video_id,
+                'datetime' => $yestodaytime,
+            ];
+
+            //昨日视频pv
+            $yestoday_pv_key = $yestoday.'_video_pv_'.$video_id;
+            $date_pvuv['video_pv'] = $redis->get($yestoday_pv_key);
+            $redis->del($yestoday_pv_key);
+
+            //昨日视频uv
+            $yestoday_uv_key = $yestoday.'_video_uv_'.$video_id;
+            $date_pvuv['video_uv'] = $redis->sCard($yestoday_uv_key);  //数量
+            $redis->del($yestoday_uv_key);
+
+            //写入数据
+            Db::name('video_pvuv')->where($date_pvuv)->delete();
+            Db::name('video_pvuv')->insertGetId($date_pvuv);
+
+        }
+
+        $redis->del($yestoday_video_key);
+
+    }
+
+}