public function actionLike1(){
Yii::$app->response->format=Response::FORMAT_JSON;
if(Yii::$app->request->isGet){
return [
'msg'=>'Request Error!',
'state'=>(int)-1,
];
}
$data = @json_decode(file_get_contents("php://input","r"),true);
$topic_id=$data['topic_id'];
$bool=self::checkTopicId($topic_id);
if (!$bool){
return [
'msg'=>'话题不存在',
'state'=>'-1',
];
}
$redis = Yii::$app->redis;
$ip=Yii::$app->request->getUserIP();
$bool=$redis->exists($topic_id.$ip);
$max_leng=3;
$time=1;
$data=json_encode(['ip'=>$ip,'topic_id'=>$topic_id,'created'=>time()],true);
if (!$bool){
if ($leng=$redis->llen($topic_id.'log')<$max_leng){
$redis->lpush($topic_id.'log',$data);
if (!$redis->exists($topic_id.'likes')){
$likes=self::redis_likes($topic_id);
$redis->set($topic_id.'likes',$likes+1);
}else{
$redis->incr($topic_id.'likes');
}
$likes=$redis->get($topic_id.'likes');
}else{
for ($i=0;$i<$max_leng;$i++){
$data=$redis->rpop($topic_id.'log');
$arr[]=json_decode($data,JSON_UNESCAPED_UNICODE);
}
$redis->lpush($topic_id.'log',$data);
$transaction=Yii::$app->db->beginTransaction(\yii\db\Transaction::SERIALIZABLE);
try{
$sql="select likes from {{%mediacloud_content}} where topic_id=:topic_id for update";
$info=Yii::$app->db->createCommand($sql)->bindValue(':topic_id',$topic_id)->queryOne();
$likes=$info['likes']+$max_leng+1;
$sql="update {{%mediacloud_content}} set likes=likes+$max_leng WHERE topic_id=:topic_id";
Yii::$app->db->createCommand($sql)->bindValue(':topic_id',$topic_id)->execute();
$redis->set($topic_id.'likes',$likes);
$bool=Yii::$app->db->createCommand()->batchInsert(LikeLog::tableName(),['ip','topic_id','created'],$arr)->execute();
if ($bool==0){
throw new Exception('点赞日志入库失败');
}
$transaction->commit();
}catch (Exception $e){
$transaction->rollBack();
return '点赞失败'.$e->getMessage();
}
}
$redis->set($topic_id.$ip,'','ex',$time);
return [
'msg'=>'ok',
'state'=>(int)0,
'likes'=>(int)$likes,
];
}else{
$likes=$redis->get($topic_id.'likes');
if (empty($likes)){
$likes=self::redis_likes($topic_id);
}
return [
'msg'=>'请勿重复点赞',
'state'=>(int)0,
'likes'=>(int)$likes,
];
}
}
public function actionGetlikes1(){
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');
$bool=self::checkTopicId($topic_id);
if (!$bool){
return [
'msg'=>'话题不存在',
'state'=>'-1',
];
}
$redis=Yii::$app->redis;
$likes=$redis->get($topic_id.'likes');
if (empty($likes)){
$likes=self::redis_likes($topic_id);
$redis->set($topic_id.'likes',$likes);
}
return [
'msg'=>'ok',
'state'=>(int)0,
'likes'=>(int)$likes,
];
}
public function redis_likes($topic_id){
$likes=MediaCloudContent::find()->where(['topic_id'=>$topic_id])->select('likes')->asArray()->one()['likes'];
return $likes;
}
public function checkTopicId($topic_id){
$content=MediaCloudContent::find()->where(['topic_id'=>$topic_id])->select(['topic_id'])->asArray()->one();
if (!$content){
return false;
}else{
return true;
}
}