123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace app\api\controller;
- use app\common\model\UserSignCard;
- use app\common\model\UserWallet;
- use think\Db;
- use app\common\model\UserSign;
- /**
- * @title 会员签到
- * @controller Sign
- * @group base
- */
- class Sign extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 验证会员当天是否签到
- * @desc 验证会员当天是否签到
- * @author qc
- * @url /api/Sign/checkSign
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @return name:is_sign type:int default:0 desc:是否签到0未签到,1已签到
- */
- public function checkSign()
- {
- $year = date('Y');
- $month = date('m');
- $day = date('d');
- $ck_sign = UserSign::where(['user_id'=>$this->user_id,'year'=>$year,'month'=>$month,'day'=>$day])->count();
- $this->success('ok',['is_sign'=>$ck_sign]);
- }
- /**
- * @title 获取会员连续签到天数
- * @desc 获取会员连续签到天数
- * @author qc
- * @url /api/Sign/getContinuousSign
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @return name:is_sign type:int default:-- desc:当天是否签到(0签到,1已签到)
- * @return name:sign_num type:int default:-- desc:连续签到天数
- * @return name:sign_card type:int default:-- desc:补签卡数量
- */
- public function getContinuousSign()
- {
- $today_sign = UserSign::where(['user_id'=>$this->user_id,'day_time'=>date('Y-m-d')])->find();
- $is_sign = !empty($today_sign) ? 1:0;
- $sign_num = 0;
- $week_sign = UserSign::where('user_id','=',$this->user_id)
- ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
- ->where('status','=',0)
- ->order('create_at desc')
- ->select()->toArray();
- $sign_date = array_column($week_sign,'day_time');
- for($i=0;$i<=6;$i++) {
- if(in_array(date('Y-m-d',strtotime("-$i days")),$sign_date)){
- $sign_num++;
- }else if($i>0){
- break;
- }
- }
- $sign_card = UserSignCard::where(['user_id'=>$this->user_id,'status'=>0])->count();
- $this->success('ok',['sign_num'=>$sign_num,'is_sign'=>$is_sign,'sign_card'=>$sign_card]);
- }
- /**
- * @title 会员签到
- * @desc 会员签到
- * @author qc
- * @url /api/Sign/userSign
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @return name:integral type:int default:-- desc:签到获得积分数
- * @return name:next_integral type:int default:-- desc:明日签到积分
- * @return name:sign_num type:int default:-- desc:连续签到天数
- */
- public function userSign()
- {
- $year = date('Y');
- $month = date('m');
- $day = date('d');
- $ck_sign = UserSign::where(['user_id'=>$this->user_id,'year'=>$year,'month'=>$month,'day'=>$day])->count();
- if($ck_sign) $this->error('已签到');
- $sign_data=[
- 'user_id' =>$this->user_id,
- 'year' =>$year,
- 'month' =>$month,
- 'day' =>$day,
- 'day_time'=>date('Y-m-d'),
- 'create_at' => date('Y-m-d H:i:s'),
- 'desc' => '每日签到',
- ];
- UserSign::create($sign_data);
- $sign_num = 0;
- // 最近七天签到情况
- $week_sign = UserSign::where('user_id','=',$this->user_id)
- ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
- ->where('status','=',0)
- ->order('create_at desc')
- ->select()->toArray();
- $sign_date = array_column($week_sign,'day_time');
- for($i=0;$i<=6;$i++) {
- if(in_array(date('Y-m-d',strtotime("-$i days")),$sign_date)){
- $sign_num++;
- }else if($i>0){
- break;
- }
- }
- $integral = in_array($sign_num,[2,3]) ? $sign_num:1;
- UserWallet::userIntegralChange($this->user_id,$integral,'签到奖励',6, 1);
- UserWallet::userExpChange($this->user_id,$integral,'签到奖励');
- $next_integral = 1;
- if(in_array($sign_num,[1,2]))$next_integral += $sign_num;
- $this->success('签到成功',['integral'=>$integral,'sign_num'=>$sign_num,'next_integral'=>$next_integral]);
- }
- /**
- * @title 会员领取签到卡
- * @desc 会员领取签到卡
- * @author qc
- * @url /api/Sign/getSignCard
- * @method POST
- * @header name:Authorization require:1 desc:Token
- */
- public function getSignCard()
- {
- $sign_num = 0;
- // 最近七天签到情况
- $week_sign = UserSign::where('user_id','=',$this->user_id)
- ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
- ->where('status','=',0)
- ->order('create_at desc')
- ->count();
- $sign_date = array_column($week_sign,'day_time');
- for($i=0;$i<=6;$i++) {
- if(in_array(date('Y-m-d',strtotime("-$i days")),$sign_date)){
- $sign_num++;
- }else if($i>0){
- break;
- }
- }
- if($sign_num != 7) $this->error('连续签到天数不足7天');
- UserSign::where('user_id','=',$this->user_id)
- ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
- ->where('status','=',0)
- ->update(['status'=>1]);
- UserSignCard::create([ 'user_id' => $this->user_id]);
- $this->success('领取成功');
- }
- /**
- * @title 会员补签
- * @desc 会员补签
- * @author qc
- * @url /api/Sign/retroactive
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:date_time type:int default:-- desc:补签日期(2022-04-07)
- * @return name:integral type:int default:-- desc:签到获得积分数
- * @return name:next_integral type:int default:-- desc:明日签到积分
- * @return name:sign_num type:int default:-- desc:连续签到天数
- */
- public function retroactive()
- {
- $date_time = input('post.date_time',date('Y-m-d'));
- $year = date('Y',strtotime($date_time));
- $month = date('m',strtotime($date_time));
- $day = date('d',strtotime($date_time));
- $ck_sign = UserSign::where(['user_id'=>$this->user_id,'year'=>$year,'month'=>$month,'day'=>$day])->count();
- if($ck_sign) $this->error('已签到');
- $user_card = UserSignCard::where(['user_id'=>$this->user_id,'status'=>0])->min('id');
- if(!$user_card) $this->error('补签卡不足');
- $sign_data=[
- 'user_id' =>$this->user_id,
- 'year' =>$year,
- 'month' =>$month,
- 'day' =>$day,
- 'day_time'=>$date_time,
- 'create_at' => $date_time . date('H:i:s'),
- 'desc' => '每日签到(补签)',
- ];
- UserSign::create($sign_data);
- $sign_num = 0;
- // 最近七天签到情况
- $week_sign = UserSign::where('user_id','=',$this->user_id)
- ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
- ->where('status','=',0)
- ->order('create_at desc')
- ->select()->toArray();
- $sign_date = array_column($week_sign,'day_time');
- for($i=0;$i<=6;$i++) {
- if(in_array(date('Y-m-d',strtotime("-$i days")),$sign_date)){
- $sign_num++;
- }else if($i>0){
- break;
- }
- }
- $integral = in_array($sign_num,[2,3]) ? $sign_num:1;
- $next_integral = 1;
- if(in_array($sign_num,[1,2]))$next_integral += $sign_num;
- $this->success('补签成功',['integral'=>$integral,'sign_num'=>$sign_num,'next_integral'=>$next_integral]);
- }
- /**
- * @title 会员签到列表
- * @desc 会员签到列表
- * @author qc
- * @url /api/Sign/getSignList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int : default:20 desc:每页数
- * @return name:create_at type:string default:-- desc:时间
- * @return name:desc type:string default:-- desc:描述
- */
- public function getSignList()
- {
- $list = UserSign::field('id,create_at,desc')
- ->where('user_id','=',$this->user_id)
- ->order('id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $this->success('ok',['list'=>$list ? $list->toAray() :[]]);
- }
- }
|