yii2 使用mysql ,实现点赞功能
来源:互联网 发布:热化学数据库建库情况 编辑:程序博客网 时间:2024/06/07 23:47
/** *进行点赞 */ 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
- yii2 使用mysql ,实现点赞功能
- 使用mysql、php和Ajax实现点赞功能
- yii2 redis队列,数据库表锁实现,点赞功能(10s内只可以点一次)
- 使用Notification实现点赞、收藏功能
- yii2随笔(五):mysql 防注入 实现 mysql_real_escape_string 功能
- Yii2实现跨mysql数据库关联查询排序功能
- [联表] -- Yii2实现跨mysql数据库关联查询排序功能
- yii2.0使用插件实现一个简单的上传功能
- yii2 缩略图功能的实现
- yii2 实现购物车功能
- 使用Yii2实现读写分离(MySQL主从数据库)
- 使用xml、php和Ajax实现点赞功能
- opensns点赞功能实现
- 点赞功能的实现
- JavaWeb 实现点赞功能
- JavaWeb 实现点赞功能
- 使用mysql实现分页功能
- yii2-分页,Yii2实现带搜索的分页功能
- C# 打印详解
- 排序
- Codeforces Round #436 (Div. 2) E. Fire(01背包+输出路径)
- React Native带你一步步实现热更新(CodePush-Android)
- Revit开发BuildingEnvelopeAnalyzer
- yii2 使用mysql ,实现点赞功能
- 最新最全的angular4.x、anuglar2入门实战视频教程
- linux 内核栈及crash信息介绍
- ◆练习题目◆◇区间动态规划◇ Brackets
- 我的第一篇blog
- 当 curl 返回的数据是 xml 怎么获取节点的值
- Shell语法
- 1.spring boot启动不了,application放置路径不对
- 面试问题spring boot自动配置是怎么实现的