Sign.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\UserSignCard;
  4. use app\common\model\UserWallet;
  5. use think\Db;
  6. use app\common\model\UserSign;
  7. /**
  8. * @title 会员签到
  9. * @controller Sign
  10. * @group base
  11. */
  12. class Sign extends Base
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. parent::checkLogin();
  18. }
  19. /**
  20. * @title 验证会员当天是否签到
  21. * @desc 验证会员当天是否签到
  22. * @author qc
  23. * @url /api/Sign/checkSign
  24. * @method GET
  25. * @header name:Authorization require:1 desc:Token
  26. * @return name:is_sign type:int default:0 desc:是否签到0未签到,1已签到
  27. */
  28. public function checkSign()
  29. {
  30. $year = date('Y');
  31. $month = date('m');
  32. $day = date('d');
  33. $ck_sign = UserSign::where(['user_id'=>$this->user_id,'year'=>$year,'month'=>$month,'day'=>$day])->count();
  34. $this->success('ok',['is_sign'=>$ck_sign]);
  35. }
  36. /**
  37. * @title 获取会员连续签到天数
  38. * @desc 获取会员连续签到天数
  39. * @author qc
  40. * @url /api/Sign/getContinuousSign
  41. * @method GET
  42. * @header name:Authorization require:1 desc:Token
  43. * @return name:is_sign type:int default:-- desc:当天是否签到(0签到,1已签到)
  44. * @return name:sign_num type:int default:-- desc:连续签到天数
  45. * @return name:sign_card type:int default:-- desc:补签卡数量
  46. */
  47. public function getContinuousSign()
  48. {
  49. $today_sign = UserSign::where(['user_id'=>$this->user_id,'day_time'=>date('Y-m-d')])->find();
  50. $is_sign = !empty($today_sign) ? 1:0;
  51. $sign_num = 0;
  52. $week_sign = UserSign::where('user_id','=',$this->user_id)
  53. ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
  54. ->where('status','=',0)
  55. ->order('create_at desc')
  56. ->select()->toArray();
  57. $sign_date = array_column($week_sign,'day_time');
  58. for($i=0;$i<=6;$i++) {
  59. if(in_array(date('Y-m-d',strtotime("-$i days")),$sign_date)){
  60. $sign_num++;
  61. }else if($i>0){
  62. break;
  63. }
  64. }
  65. $sign_card = UserSignCard::where(['user_id'=>$this->user_id,'status'=>0])->count();
  66. $this->success('ok',['sign_num'=>$sign_num,'is_sign'=>$is_sign,'sign_card'=>$sign_card]);
  67. }
  68. /**
  69. * @title 会员签到
  70. * @desc 会员签到
  71. * @author qc
  72. * @url /api/Sign/userSign
  73. * @method POST
  74. * @header name:Authorization require:1 desc:Token
  75. * @return name:integral type:int default:-- desc:签到获得积分数
  76. * @return name:next_integral type:int default:-- desc:明日签到积分
  77. * @return name:sign_num type:int default:-- desc:连续签到天数
  78. */
  79. public function userSign()
  80. {
  81. $year = date('Y');
  82. $month = date('m');
  83. $day = date('d');
  84. $ck_sign = UserSign::where(['user_id'=>$this->user_id,'year'=>$year,'month'=>$month,'day'=>$day])->count();
  85. if($ck_sign) $this->error('已签到');
  86. $sign_data=[
  87. 'user_id' =>$this->user_id,
  88. 'year' =>$year,
  89. 'month' =>$month,
  90. 'day' =>$day,
  91. 'day_time'=>date('Y-m-d'),
  92. 'create_at' => date('Y-m-d H:i:s'),
  93. 'desc' => '每日签到',
  94. ];
  95. UserSign::create($sign_data);
  96. $sign_num = 0;
  97. // 最近七天签到情况
  98. $week_sign = UserSign::where('user_id','=',$this->user_id)
  99. ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
  100. ->where('status','=',0)
  101. ->order('create_at desc')
  102. ->select()->toArray();
  103. $sign_date = array_column($week_sign,'day_time');
  104. for($i=0;$i<=6;$i++) {
  105. if(in_array(date('Y-m-d',strtotime("-$i days")),$sign_date)){
  106. $sign_num++;
  107. }else if($i>0){
  108. break;
  109. }
  110. }
  111. $integral = in_array($sign_num,[2,3]) ? $sign_num:1;
  112. UserWallet::userIntegralChange($this->user_id,$integral,'签到奖励',6, 1);
  113. UserWallet::userExpChange($this->user_id,$integral,'签到奖励');
  114. $next_integral = 1;
  115. if(in_array($sign_num,[1,2]))$next_integral += $sign_num;
  116. $this->success('签到成功',['integral'=>$integral,'sign_num'=>$sign_num,'next_integral'=>$next_integral]);
  117. }
  118. /**
  119. * @title 会员领取签到卡
  120. * @desc 会员领取签到卡
  121. * @author qc
  122. * @url /api/Sign/getSignCard
  123. * @method POST
  124. * @header name:Authorization require:1 desc:Token
  125. */
  126. public function getSignCard()
  127. {
  128. $sign_num = 0;
  129. // 最近七天签到情况
  130. $week_sign = UserSign::where('user_id','=',$this->user_id)
  131. ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
  132. ->where('status','=',0)
  133. ->order('create_at desc')
  134. ->count();
  135. $sign_date = array_column($week_sign,'day_time');
  136. for($i=0;$i<=6;$i++) {
  137. if(in_array(date('Y-m-d',strtotime("-$i days")),$sign_date)){
  138. $sign_num++;
  139. }else if($i>0){
  140. break;
  141. }
  142. }
  143. if($sign_num != 7) $this->error('连续签到天数不足7天');
  144. UserSign::where('user_id','=',$this->user_id)
  145. ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
  146. ->where('status','=',0)
  147. ->update(['status'=>1]);
  148. UserSignCard::create([ 'user_id' => $this->user_id]);
  149. $this->success('领取成功');
  150. }
  151. /**
  152. * @title 会员补签
  153. * @desc 会员补签
  154. * @author qc
  155. * @url /api/Sign/retroactive
  156. * @method POST
  157. * @header name:Authorization require:1 desc:Token
  158. * @param name:date_time type:int default:-- desc:补签日期(2022-04-07)
  159. * @return name:integral type:int default:-- desc:签到获得积分数
  160. * @return name:next_integral type:int default:-- desc:明日签到积分
  161. * @return name:sign_num type:int default:-- desc:连续签到天数
  162. */
  163. public function retroactive()
  164. {
  165. $date_time = input('post.date_time',date('Y-m-d'));
  166. $year = date('Y',strtotime($date_time));
  167. $month = date('m',strtotime($date_time));
  168. $day = date('d',strtotime($date_time));
  169. $ck_sign = UserSign::where(['user_id'=>$this->user_id,'year'=>$year,'month'=>$month,'day'=>$day])->count();
  170. if($ck_sign) $this->error('已签到');
  171. $user_card = UserSignCard::where(['user_id'=>$this->user_id,'status'=>0])->min('id');
  172. if(!$user_card) $this->error('补签卡不足');
  173. $sign_data=[
  174. 'user_id' =>$this->user_id,
  175. 'year' =>$year,
  176. 'month' =>$month,
  177. 'day' =>$day,
  178. 'day_time'=>$date_time,
  179. 'create_at' => $date_time . date('H:i:s'),
  180. 'desc' => '每日签到(补签)',
  181. ];
  182. UserSign::create($sign_data);
  183. $sign_num = 0;
  184. // 最近七天签到情况
  185. $week_sign = UserSign::where('user_id','=',$this->user_id)
  186. ->where('create_at','>= time',date('Y-m-d 00:00:00',strtotime('-6 days')))
  187. ->where('status','=',0)
  188. ->order('create_at desc')
  189. ->select()->toArray();
  190. $sign_date = array_column($week_sign,'day_time');
  191. for($i=0;$i<=6;$i++) {
  192. if(in_array(date('Y-m-d',strtotime("-$i days")),$sign_date)){
  193. $sign_num++;
  194. }else if($i>0){
  195. break;
  196. }
  197. }
  198. $integral = in_array($sign_num,[2,3]) ? $sign_num:1;
  199. $next_integral = 1;
  200. if(in_array($sign_num,[1,2]))$next_integral += $sign_num;
  201. $this->success('补签成功',['integral'=>$integral,'sign_num'=>$sign_num,'next_integral'=>$next_integral]);
  202. }
  203. /**
  204. * @title 会员签到列表
  205. * @desc 会员签到列表
  206. * @author qc
  207. * @url /api/Sign/getSignList
  208. * @method GET
  209. * @header name:Authorization require:1 desc:Token
  210. * @param name:page type:int default:1 desc:页数
  211. * @param name:page_num type:int : default:20 desc:每页数
  212. * @return name:create_at type:string default:-- desc:时间
  213. * @return name:desc type:string default:-- desc:描述
  214. */
  215. public function getSignList()
  216. {
  217. $list = UserSign::field('id,create_at,desc')
  218. ->where('user_id','=',$this->user_id)
  219. ->order('id desc')
  220. ->limit($this->off_set,$this->page_num)
  221. ->select();
  222. $this->success('ok',['list'=>$list ? $list->toAray() :[]]);
  223. }
  224. }