yii2 使用mysql ,实现点赞功能

    /**
     *进行点赞
     */
    public function actionLike(){
        Yii::$app->response->format=Response::FORMAT_JSON;
        $data = @json_decode(file_get_contents("php://input","r"),true);
        $topic_id=$data['topic_id'];
        $like = new LikeLog();
        if(Yii::$app->request->isGet){
            return [
                'msg'=>'Request Error!',
                'state'=>(int)-1,
            ];
        }
        $result=$like->apiLike($topic_id);
        if ($result && Yii::$app->cache->exists($topic_id)){
            return [
                'msg'=>'ok',
                'state'=>(int)0,
                'likes'=>(int)Yii::$app->cache->get($topic_id),
            ];
        }else{
            $data=self::likes($topic_id);
            if (!empty($like->error)){
                $data['msg']=$like->error;
            }
            return $data;
        }
    }

    /**
     *获取点赞条数
     */
    public function actionGetlikes(){

        Yii::$app->response->format=Response::FORMAT_JSON;
        if (Yii::$app->request->isPost){
            return [
                'msg'=>'Request Error!',
                'state'=>(int)-1,
            ];
        }
        $topic_id=Yii::$app->request->get('topic_id');
        if (Yii::$app->cache->exists($topic_id)){
            $likes=Yii::$app->cache->get($topic_id);
            return [
                'msg'=>'ok',
                'state'=>(int)0,
                'likes'=>$likes,
//                'from'=>'cache',
            ];
        }else{
            return self::likes($topic_id);
        }
    }

    private static function likes($topic_id){
        $content=MediaCloudContent::find()->where(['topic_id'=>$topic_id])->select(['topic_id','likes'])->asArray()->one();
        if (!$content){
            return [
                'msg'=>'话题不存在',
                'state'=>'-1',
            ];
        }
        Yii::$app->cache->set($topic_id,(int)$content['likes']);
        return [
            'msg'=>'ok',
            'state'=>(int)0,
            'likes'=>$content['likes'],
//                'from'=>'sql',
        ];
    }














 public function apiLike($topic_id){
        $content=MediaCloudContent::find()->where(['topic_id'=>$topic_id])->select(['topic_id','likes'])->asArray()->one();
        if (empty($content)){
            $this->error='话题不存在!';
            return false;
        }
        $r = $this->findOne(['topic_id'=>$topic_id,'ip'=>Yii::$app->request->getUserIP()],'created desc');
        if ($r && time()-($r->created) < 10){
            $this->error='两次点赞间隔不能低于10秒';
            return false;
        }else{
            $transaction=Yii::$app->db->beginTransaction();
            try{
                $this->topic_id = $topic_id;
                $this->save();
                //锁定行
                $sql="select likes from {{%mediacloud_content}} where topic_id='$topic_id' for update";
                $data=Yii::$app->db->createCommand($sql)->query()->read();
                $sql="update {{%mediacloud_content}} set likes=likes+1 where topic_id='$topic_id'";
                Yii::$app->db->createCommand($sql)->execute();
                $transaction->commit();
                Yii::$app->cache->set($topic_id,$data['likes']+1);
            }catch (Exception $e){
                Yii::error($e->getMessage());
                $this->error=json_encode($e->getMessage());
                $transaction->rollBack();
                return false;
            }

        }
        return true;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值