1. <?php
  2.  
  3.  
  4. namespace app\controller;
  5.  
  6.  
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9.  
  10. class Zan
  11. {
  12.     public $redis = null;
  13.  
  14.     //60*60*24/20=4320,每个点赞得到的分数。
  15.     public $score = 0;
  16.  
  17.     //点赞增加数,或者
  18.     public $num = 1;
  19.  
  20.  
  21.     //init redis
  22.     const COMMENT_RECORD = 'comment:record';
  23.  
  24.     public function __construct()
  25.     {
  26.         $this->redis = Cache::store('redis');
  27. //        $this->redis->connect($this->redis_host,$this->redis_port);
  28. //        $this->redis->auth($this->redis_pass);
  29.     }
  30.  
  31.     /**
  32.      * @param int $user_id 用户id
  33.      * @param int $type 点击的类型 1.点like,2.点hate
  34.      * @param int $comment_id 文章id
  35.      * @return string json;
  36.      */
  37.     public function click($user_id,$type,$comment_id)
  38.     {
  39.         //判断是否需要更新数据
  40.         if (!$this->ifUploadList($comment_id)) {
  41.             return ['code'=>400,'msg'=>'文章不存在'];
  42.         }
  43.         $type == 1 ? $key = "like" : $key = "hate";
  44.         //判断redis是否已经缓存了该文章数据
  45.         //使用:分隔符对redis管理是友好的
  46.         //这里使用redis zset-> zscore()方法
  47.         $data = [];
  48.         if ($this->redis->zscore("comment:like", $comment_id) || 
  49.             $this->redis->zscore("comment:hate", $comment_id)) {
  50.             //已经存在
  51.             //判断点的是什么
  52.             $rel = $this->redis->hget(self::COMMENT_RECORD, $user_id . ":" . $comment_id);
  53.             //redis hash-> hget()
  54.             //判断以前是否点过,点的是什么?
  55.             switch ($rel) {
  56.                 case '':
  57.                     //什么都没点过
  58.                     //点赞加1
  59.                     $this->redis->zincrby("comment:".$key, $this->num, $comment_id);
  60.                     //记录上次操作
  61.                     $this->redis->hset(self::COMMENT_RECORD, $user_id . ":" . $comment_id, $type);
  62.  
  63.                     $data = ["code" => 1,"msg" => $key."+1"];
  64.                     break;
  65.                 case $type:
  66.                     //点过赞了
  67.                     //点赞减1
  68.                     $this->redis->zincrby("comment:".$key, -($this->num), $comment_id);
  69.                     //删除记录
  70.                     $this->redis->hDel(self::COMMENT_RECORD, $user_id . ":" . $comment_id, $type);
  71.                     //删除缓存 数据库再-1
  72.                     $data = ["code" => 2,"msg" => $key."-1"];
  73.                     break;
  74.                 case $type == 1 ? 2 : 1:
  75.                     //点赞加1
  76.                     $this->redis->zincrby("comment:".$key, $this->num, $comment_id);
  77.                     //记录上次操作
  78.                     $this->redis->hset(self::COMMENT_RECORD, $user_id . ":" . $comment_id, $type);
  79.                     $data = ["code" => 1,"msg" => $key."+1"];
  80.                     break;
  81.  
  82.             }
  83.         } else {
  84.             //未存在
  85.             //点赞加一
  86.             $this->redis->zincrby("comment:".$key, $this->num, $comment_id);
  87.             $data = ["code" => 1,"msg" => $key."+1"];
  88.             //记录
  89.             $this->redis->hset(self::COMMENT_RECORD, $user_id . ":" . $comment_id, $type);
  90.         }
  91.         $list = $this->redis->zRange("comment:like",0,-1,1);
  92.         foreach ($list as $k =>$v){if ($k==$comment_id){$data['like']=$v;}}
  93.         return $data;
  94.     }
  95.  
  96.     /**
  97.      * 获取文章点赞数
  98.      * @param $cid
  99.      * @return mixed
  100.      */
  101.     public function getVodZanNum($cid)
  102.     {
  103.         $data = $this->redis->zRange("comment:like",0,-1,'WITHSCORES');
  104.         if(empty($data)){
  105.             $this->redis->zincrby("comment:like", $this->num, $cid);
  106.             $data = Db::name('user_info_0')->where('id',$cid)->value('comic_id');
  107.         }
  108.         return $data;
  109.     }
  110.  
  111.     /**
  112.      * 点赞
  113.      * @param $uid
  114.      * @param $cid
  115.      * @param int $type
  116.      * @return bool
  117.      */
  118.     public function uidZan($uid,$cid)
  119.     {
  120.         $bool = $this->redis->sismember('zan'.$uid,$cid);
  121.         if(!$bool){
  122.             $this->redis->sadd('zan'.$uid,$cid);
  123.             return true;
  124.         }else{
  125.             $this->redis->spop('zan'.$uid);
  126.             return false;
  127.         }
  128.     }
  129.  
  130.     /**
  131.      * 判断是否点过赞
  132.      * @param $uid
  133.      * @param $cid
  134.      * @return bool
  135.      */
  136.     public function checkZan($uid,$cid)
  137.     {
  138.         $bool = $this->redis->sismember('zan'.$uid,$cid);
  139.         if($bool) return true;
  140.         return false;
  141.     }
  142.  
  143.     /**
  144.      * 取消点赞
  145.      * @param $uid
  146.      * @return bool
  147.      */
  148.     public function delZan($uid)
  149.     {
  150.         $this->redis->spop('zan'.$uid);
  151.         return false;
  152.     }
  153.  
  154.     /**
  155.      * 判断文章是否存在
  156.      * @param $comment_id
  157.      * @return string
  158.      */
  159.     public function ifUploadList($comment_id)
  160.     {
  161.         date_default_timezone_set("Asia/Shanghai");
  162.         $time = strtotime(date('Y-m-d H:i:s'));
  163.         if(!$this->redis->sismember("comment:uploadset",$comment_id))
  164.         {
  165.             //文章不存在集合里,需要更新
  166.             $this->redis->sadd("comment:uploadset",$comment_id);
  167.             //更新到队列
  168.             $data = ["id" => $comment_id,"time" => $time];
  169.             $json = json_encode($data);
  170.             $this->redis->lpush("comment:uploadlist",$json);
  171.             return true;
  172.         }
  173.         return true;
  174.     }
  175.  
  176.     public function dell(){
  177.         $this->redis->flushAll();
  178.     }
  179.  
  180. }

调用

  1. $user_id = $_GET['uid'] ??25;
  2. $comment_id= $_GET['cid'] ?? 1;
  3. $like = 'like';
  4. if (isset($like))
  5.     $type = 1;
  6. if ($user_id && $comment_id){
  7.     $good = new Zan();
  8.     $rel = $good->click($user_id,$type,$comment_id);
  9.     if($rel['code'] == 1 || $rel['code'] == 7){
  10.  
  11.         Db::name('user_info_0')->where('id',$comment_id)->inc('comic_id')->update();
  12.         echo '点赞成功';exit;
  13.     }else{
  14.         $where[] = ['id','=',$comment_id];
  15.         $where[] = ['comic_id','>',0];
  16.         Db::name('user_info_0')->where($where)->dec('comic_id')->update();
  17.         $good->delZan($user_id);
  18.         echo '取消点赞成功';
  19.     }
  20.  
  21. }else{
  22.     echo '用户ID和文章id不能为空';
  23. }


相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!