Usersign.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 = ['config'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 配置
  14. */
  15. public function config()
  16. {
  17. $list = Db::name('signin')->order('id asc')->select();
  18. //连续签到次数
  19. $user_sign = Db::name('user_sign')->where(['uid' => $this->auth->id])->order('id desc')->find();
  20. $yesterday_time = strtotime('yesterday'); //昨天0点时间戳
  21. $today_time = $yesterday_time + 86400; //今日0点时间戳
  22. if ($user_sign) {
  23. if ($user_sign['createtime'] >= $yesterday_time) {
  24. $data['sign_times'] = $user_sign['times']; //连续签到天数
  25. } else {
  26. $data['sign_times'] = 0;
  27. }
  28. if ($user_sign['createtime'] >= $today_time) {
  29. $data['is_sign'] = 1; //今日是否签到: 1已签到 0未签到
  30. } else {
  31. $data['is_sign'] = 0; //今日是否签到: 1已签到 0未签到
  32. }
  33. } else {
  34. $data['sign_times'] = 0; //连续签到天数
  35. $data['is_sign'] = 0; //今日是否签到: 1已签到 0未签到
  36. }
  37. $data['sign_round_times'] = $data['sign_times'] % 7;
  38. $data['list'] = $list;
  39. $this->success('success',$data);
  40. }
  41. //签到
  42. public function signin(){
  43. $uid = $this->auth->id;
  44. //记录日志
  45. $data = [
  46. 'uid' => $uid,
  47. 'times' => 1,
  48. 'goldnum' => 0,
  49. 'createtime' => time(),
  50. ];
  51. $yesterday_time = strtotime('yesterday'); //昨天0点时间戳
  52. $today_time = $yesterday_time + 86400; //今日0点时间戳
  53. //修正当前次数
  54. $check = Db::name('user_sign')->where('uid',$uid)->order('id desc')->find();
  55. if($check){
  56. //今天只能签一次
  57. if($check['createtime'] >= $today_time){
  58. $this->error('今天已经签过了');
  59. }
  60. if ($check['createtime'] >= $yesterday_time) {
  61. $data['times'] = $check['times'] + 1; //连续签到次数
  62. }
  63. }
  64. $signin_id = $data['times'] % 7;
  65. if ($signin_id == 0) {
  66. $signin_id = 7;
  67. }
  68. //匹配对应金币数
  69. $list = Db::name('signin')->order('id asc')->column('id,goldnum');
  70. $data['goldnum'] = isset($list[$signin_id]) ? $list[$signin_id] : 0 ;
  71. Db::startTrans();
  72. //记录日志
  73. $log_id = Db::name('user_sign')->insertGetId($data);
  74. if(!$log_id){
  75. Db::rollback();
  76. $this->error('签到失败');
  77. }
  78. //加钱
  79. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,0,'gold',$data['goldnum'],41,'签到','user_sign',$log_id);
  80. if($rs['status'] === false){
  81. Db::rollback();
  82. $this->error($rs['msg']);
  83. }
  84. /*//第七天赠送vip
  85. $vip_rs = false;
  86. $user_sign_gift_vipdays = intval(config('site.user_sign_gift_vipdays'));
  87. if($data['times'] == 7 && $user_sign_gift_vipdays > 0){
  88. $vip_rs = $this->gift_vip($user_sign_gift_vipdays);
  89. }*/
  90. //系统消息
  91. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'签到奖励','签到成功,获得金币奖励');
  92. Db::commit();
  93. // $remark = '获得金币'.$data['goldnum'];
  94. /*if($vip_rs){
  95. $remark .= ',vip'.$user_sign_gift_vipdays.'天';
  96. }*/
  97. $this->success('签到成功', $data['goldnum']);
  98. }
  99. //第七天赠送vip
  100. //找出最近七天的次数
  101. private function gift_vip($user_sign_gift_vipdays){
  102. $start = strtotime(date('Y-m-d')) - 518400;
  103. $map = [
  104. 'uid' => $this->auth->id,
  105. 'createtime' => ['gt',$start],
  106. ];
  107. $count = Db::name('user_sign')->where($map)->count();
  108. if($count >= 7){
  109. //赠送vip三天
  110. $add_time = 86400 * $user_sign_gift_vipdays;
  111. //有vip的续费,没有的从现在开始
  112. $vip_endtime = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  113. if($vip_endtime <= time()){
  114. $new_vip_endtime = time() + $add_time;
  115. }else{
  116. $new_vip_endtime = $vip_endtime + $add_time;
  117. }
  118. $rs = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['vip_endtime'=>$new_vip_endtime]);
  119. if($rs === false){
  120. Db::rollback();
  121. $this->error('连续签到赠送vip失败');
  122. }
  123. return true;
  124. }
  125. return false;
  126. }
  127. }