后台投票 接口

/**
* 投票
* uid
* vote 票数
*/
public function vote(){
$param=input('param.');
$param['pid']=session::get('tid');

//根据用户id来查投票详情表判断他有没有投票
$data=db('txb')->where('pid',$param['pid'])->find();
if(empty($data)){
//如果数据查找不到则说明该用户还没有投票,添加一条数据到投票详情表
$param['state']='投票';
$data1=db('txb')->insertGetId($param);
if($data1){
//总票数 根据作品表查下他的状态,每投票成功一次就给该字段+1
$num=db('user')->where('id',$param['uid'])->find();
$num1 = $num['vote']+1;
$count=db('user')->where('id',$param['uid'])->update(['vote'=>$num1]);

return json(['code'=>1,'messgae'=>'投票成功']);
}else{
return json(['code'=>2,'message'=>'投票失败']);
}
}else{
//如果数据库查找到该数据,但该数据的状态为未投票,则给他修改为投票
if($data['state']=='未投票'){
$data1=db('txb')->where('pid',$param['pid'])->update(['state'=>'投票']);
if($data1){
$num=db('user')->where('id',$param['uid'])->find();
$num1 = $num['vote']+1;
$count=db('user')->where('id',$param['uid'])->update(['vote'=>$num1]);

return json(['code'=>1,'message'=>'投票成功']);
}else{
return json(['code'=>2,'message'=>'投票失败']);
}
}
return json(['code'=>3,'message'=>'您已经投过一次票啦']);
}
}

posted on 2018-06-29 16:58  旺仔小裤头  阅读(385)  评论(0编辑  收藏  举报