Usersign.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 签到
  7. */
  8. class Usersign extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 配置
  14. */
  15. public function config()
  16. {
  17. $list = Db::name('signin')->order('id asc')->select();
  18. foreach($list as $key => &$val){
  19. if($this->auth->gender == 0){
  20. $val['goldnum'] = $val['woman_goldnum'];
  21. }
  22. unset($val['woman_goldnum']);
  23. }
  24. //连续签到次数
  25. $user_sign = Db::name('user_sign')->where(['uid' => $this->auth->id])->order('id desc')->find();
  26. $yesterday_time = strtotime('yesterday'); //昨天0点时间戳
  27. $today_time = $yesterday_time + 86400; //今日0点时间戳
  28. if ($user_sign) {
  29. if ($user_sign['createtime'] >= $yesterday_time) {
  30. $data['sign_times'] = $user_sign['times']; //连续签到天数
  31. } else {
  32. $data['sign_times'] = 0;//昨天没签到,归零
  33. }
  34. if ($user_sign['createtime'] >= $today_time) {
  35. $data['is_sign'] = 1; //今日是否签到: 1已签到 0未签到
  36. } else {
  37. $data['is_sign'] = 0; //今日是否签到: 1已签到 0未签到
  38. }
  39. } else {
  40. $data['sign_times'] = 0; //连续签到天数
  41. $data['is_sign'] = 0; //今日是否签到: 1已签到 0未签到
  42. }
  43. $data['sign_round_times'] = $data['sign_times'] % 7;
  44. $data['list'] = $list;
  45. $this->success('success',$data);
  46. }
  47. //签到
  48. public function signin(){
  49. $uid = $this->auth->id;
  50. //记录日志
  51. $data = [
  52. 'uid' => $uid,
  53. 'times' => 1,
  54. 'goldnum' => 0,
  55. 'createtime' => time(),
  56. ];
  57. $yesterday_time = strtotime('yesterday'); //昨天0点时间戳
  58. $today_time = $yesterday_time + 86400; //今日0点时间戳
  59. //修正当前次数
  60. $check = Db::name('user_sign')->where('uid',$uid)->order('id desc')->find();
  61. if($check){
  62. //今天只能签一次
  63. if($check['createtime'] > $today_time){
  64. $this->error('今天已经签过了');
  65. }
  66. if ($check['createtime'] >= $yesterday_time) {
  67. $data['times'] = $check['times'] + 1; //连续签到次数
  68. }else{
  69. //没有连起来,默认1
  70. }
  71. }
  72. if($data['times'] > 7){
  73. $data['times'] = 1;
  74. }
  75. //匹配对应金币数
  76. $list = Db::name('signin')->order('id asc')->column('id,goldnum');
  77. if($this->auth->gender == 0){
  78. $list = Db::name('signin')->order('id asc')->column('id,woman_goldnum as goldnum');
  79. }
  80. $data['goldnum'] = isset($list[$data['times']]) ? $list[$data['times']] : 0 ;
  81. Db::startTrans();
  82. //记录日志
  83. $log_id = Db::name('user_sign')->insertGetId($data);
  84. if(!$log_id){
  85. Db::rollback();
  86. $this->error('签到失败');
  87. }
  88. //加钱
  89. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'jewel',$data['goldnum'],33,'签到奖励','user_sign',$log_id);
  90. if($rs['status'] === false){
  91. Db::rollback();
  92. $this->error($rs['msg']);
  93. }
  94. //第七天赠送vip
  95. /*$vip_rs = false;
  96. $user_sign_gift_vipdays = intval(config('site.user_sign_gift_vipdays'));
  97. if($data['times'] == 7 && $user_sign_gift_vipdays > 0){
  98. $vip_rs = $this->gift_vip($user_sign_gift_vipdays);
  99. }*/
  100. Db::commit();
  101. $remark = '获得金币'.$data['goldnum'];
  102. /*if($vip_rs){
  103. $remark .= ',vip'.$user_sign_gift_vipdays.'天';
  104. }*/
  105. $this->success('签到成功',$remark);
  106. }
  107. //第七天赠送vip
  108. //找出最近七天的次数
  109. private function gift_vip($user_sign_gift_vipdays){
  110. $start = strtotime(date('Y-m-d')) - 518400;
  111. $map = [
  112. 'uid' => $this->auth->id,
  113. 'createtime' => ['gt',$start],
  114. ];
  115. $count = Db::name('user_sign')->where($map)->count();
  116. if($count >= 7){ //七天内签到次数 >= 7
  117. //赠送vip三天
  118. $add_time = 86400 * $user_sign_gift_vipdays;
  119. //有vip的续费,没有的从现在开始
  120. $vip_endtime = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  121. if($vip_endtime <= time()){
  122. $new_vip_endtime = time() + $add_time;
  123. $vip_type = 1;
  124. }else{
  125. $new_vip_endtime = $vip_endtime + $add_time;
  126. $vip_type = 2;
  127. }
  128. $rs = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['vip_endtime'=>$new_vip_endtime]);
  129. //记录日志
  130. $log_data = [
  131. 'user_id' => $this->auth->id,
  132. 'before' => $vip_endtime,
  133. 'change_value' => $add_time,
  134. 'remain' => $new_vip_endtime,
  135. 'remark' => '签到送vip',
  136. 'createtime' => time(),
  137. 'vip_type' => $vip_type,
  138. ];
  139. Db::name('user_vip_log')->insertGetId($log_data);
  140. if($rs === false){
  141. Db::rollback();
  142. $this->error('连续签到赠送vip失败');
  143. }
  144. return true;
  145. }
  146. return false;
  147. }
  148. }