头一次做这种带评论带积分的公众号,记录一下,如果有大神看出漏洞了求指教23333
1.操作积分方法函数:
public function makeIntegral($openid,$cate,$num,$remark,$a_id=''){ $uid = $this->getUserId($openid); $oldScore = Db::name('member')->where(['id'=>$uid])->value('integral'); Db::startTrans(); try{ $map = [ 'u_id' =>$uid, #用户id 'openid' =>$openid, #用户openid 'cate' =>$cate, # 1+ 2- 'num' =>$num, #分值 'remark' =>$remark, #文字记录 'ctime' =>time(), #积分操作时间 'status' =>1, #数据状态 'a_id' =>$a_id ]; if($cate==1){ $newScore = $oldScore+$num; }else{$newScore = $oldScore-$num; #新的积分不能为负数 否则返回扣分失败 if($newScore<0){ Log::write('积分扣除失败,您没有足够的积分。时间:'.date('Y-m-d H:i:s').',openid:'.$openid,'info'); return ['code'=>500,'msg'=>'积分扣除失败,您没有足够的积分。']; } } $add = Db::name('integral')->insert($map); $Add = Db::name('member')->where(['openid'=>$openid])->update(['integral'=>$newScore]); Db::commit(); if($add && $Add){ return ['code'=>200,'msg'=>'积分操作成功']; }else{ return ['code'=>500,'msg'=>'积分操作失败']; } }catch(Exception $e){ Db::rollback(); return ['code'=>500,'msg'=>'错误']; } }
这个方法中要操作两张表,一张是积分表,新增数据,一张是用户表,修改用户的积分值,所以用了tp5的事务,操作错误的时候方便回滚。cate是分数操作类别,1是加分操作,2是减分操作。
2.签到方法
/** * 签到方法 * @return hink esponseJson */ public function sign_in(){ $pub=new PublicModel(); $openid = session('userInfo.openid'); $u_id = $pub->getUserId($openid); $today = date('Y-m-d'); #今天的年月日 $signList = $pub->getSignRecored($u_id); #之前签到的时间戳 $continueDay = Db::name('member') ->where(['openid'=>$openid,'status'=>1]) ->value('day'); #连续多少天 if(($continueDay+1)%7==0 && ($continueDay+1)<=7){#满一周 $today_sign_integral=10; }else if(($continueDay+1)%15==0 && ($continueDay+1)/15==1){#第一次满15天 $today_sign_integral=20; }else if(($continueDay+1)%30==0 && ($continueDay+1)/30==1){#第一次满30天 $today_sign_integral=50; }else{ $today_sign_integral=($continueDay%7)+1; } if(!empty($signList)){#如果签到列表不为空,则查询今天是否完成签到 foreach ($signList as $k=>$v){ $date = date('Y-m-d',$v['ctime']); if($date==$today){#今天签到过 $arr = ['code'=>300,'msg'=>'您今天已经完成签到']; return json($arr); } } } Db::startTrans(); try{ $data = [ 'u_id'=>$u_id, 'ctime'=>time(), 'integral'=>$today_sign_integral ]; $signIn = Db::name('sign_in')->insert($data);#签到表添加 $addScore = $pub->makeIntegral($openid,1,$today_sign_integral,'签到加分','');#积分记录修改 $addContinueDay = Db::name('member')->where(['status'=>1,'openid'=>$openid])->update(['day'=>($continueDay+1)]); Db::commit(); if($signIn&&$addScore&&$addContinueDay){ return json(['code'=>200,'msg'=>'签到成功','num'=>$today_sign_integral]); }else{ return json(['code'=>500,'msg'=>'操作异常']); } }catch (Exception $e){ Db::rollback(); return json(['code'=>'500','msg'=>'false']); } }
甲方粑粑要求签到积分,一周一轮,第一天签到+1分,连续第二天签到+2分,连续第三天签到+3分 以此类推,第一次满连续七天签到的当天可获得10分,第一次满连续签到15天的当天可获得20分,第一次满连续签到30天的当天可获得50分。所以我在用户表里给了个字段记录签到天数,有个检查是否断签的方法,断签就把字段清零。签到在签到表里记录,然后加分。
3.检查是否断签
/** * 检查是否断签 * @return array * @throws hinkException * @throws hinkexceptionPDOException */ public function checkContinueSign($openid){ $pub = new PublicModel(); $u_id = $pub->getUserId($openid); try{ #判断上一次的签到是否在昨天之内 $signList = Db::name('sign_in')->where(['u_id'=>$u_id,'status'=>1])->order(['ctime'=>'desc'])->select(); if(!$signList){ $arr = ['code'=>400,'msg'=>'第一天签到']; return $arr; } $last_sign = Db::name('sign_in')->where(['u_id'=>$u_id,'status'=>1])->order(['ctime'=>'desc'])->limit(0,1)->value('ctime'); /**昨天的时间戳时间范围*/ $t = time(); $last_start_time = mktime(0,0,0,date("m",$t),date("d",$t)-1,date("Y",$t)); $last_end_time = mktime(23,59,59,date("m",$t),date("d",$t)-1,date("Y",$t)); /**今天的时间戳时间范围*/ $now_start_time = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t)); $now_end_time = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t)); if($last_sign>$last_start_time && $last_sign < $last_end_time){#昨天签到了 $arr = ['code'=>200,'msg'=>'连续签到']; }elseif($last_sign>$now_start_time && $last_sign < $now_end_time){#今天的签到已完成 $arr = ['code'=>300,'msg'=>'您今天已经完成签到']; }else{#今天没签到,断签 Db::name('member')->where(['id'=>$u_id,'status'=>1])->update(['day'=>0]); $arr = ['code'=>500,'msg'=>'检测为断签']; } return $arr; }catch (Exception $e){ return ['code'=>$e->getCode(),'msg'=>$e->getMessage()]; } }
4.评论方法
/** * 评论 */ if(request()->isPost()){ $openid = session('userInfo.openid'); $pub = new PublicModel(); $u_id = $pub->getUserId($openid); $map = input('post.'); $data = [ 'ctime'=>time(), 'a_id'=>$map['a_id'], 'u_id'=>$u_id, 'content'=>$map['comment'], 'status'=>1 ]; list($start, $end) = Time::today(); $todayTopComment = Db::name('comment')->where(['status'=>1,'ctime'=>['<',$end],'u_id'=>$u_id])->where(['ctime'=>['>',$start]])->where(['back'=>null])->count(); if($todayTopComment<10){ Db::name('comment')->insert($data); $res = $pub->makeIntegral($openid,1,1,'发表评论',$map['a_id']);#加分 return json($res); }else{ $res = Db::name('comment')->insert($data); if($res){ return json(['code'=>2019,'msg'=>'评论成功,今日评论加分已到达上限']); }else{ return json(['code'=>500,'msg'=>'评论失败']); } } }
甲方爸爸要求用户每评论一个就加一个积分,每天上限10分,我自己给文章设置了一篇文章值只能发表一个评论,当前用户可以删除自己的评论,然后重新发。
5.点赞,取消
/** * 文章点赞/取消 * @return hink esponseJson */ public function dianzan(){ try{ $openid = session('userInfo.openid'); $pub = new PublicModel(); $data = input('post.'); $u_id = $pub->getUserId($openid); if($data['cate']==1){#点赞 $map = [ 'dz_time'=>time(), 'u_id' =>$u_id, 'a_id' =>$data['a_id'], 'status' =>1 ]; }else{#取消 $map = [ 'qx_time'=>time(), 'status' =>0 ]; } $isZanRecord = Db::name('zan')->where(['u_id'=>$u_id,'a_id'=>$data['a_id']])->find(); if(!$isZanRecord){ $res = Db::name('zan')->insert($map); if($res){ $num = Db::name('zan')->where(['a_id'=>$data['a_id'],'status'=>1])->count(); $arr = ['code'=>200,'data'=>'/static/home/images/article/07.png','msg'=>'点赞成功','num'=>$num,'zan'=>0]; }else{ $arr = ['code'=>500,'data'=>'','msg'=>'点赞失败']; } }else{ $res = Db::name('zan')->where(['u_id'=>$u_id,'a_id'=>$data['a_id']])->update($map); if($res){ $num = Db::name('zan')->where(['a_id'=>$data['a_id'],'status'=>1])->count(); if($data['cate']==1){ $arr = ['code'=>200,'data'=>'/static/home/images/article/07.png','msg'=>'点赞成功','num'=>$num,'zan'=>0]; }else{ $arr = ['code'=>200,'data'=>'/static/home/images/article/06.png','msg'=>'取消成功','num'=>$num,'zan'=>1]; } }else{ $arr = ['code'=>500,'data'=>'','msg'=>'点赞失败']; } } }catch (Exception $e) { $arr = ['code'=>$e->getCode(),'data'=>'','msg'=>$e->getMessage()]; } return json($arr); }