Usersign.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. /**
  7. * 签到
  8. */
  9. class Usersign extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 配置
  15. */
  16. public function config()
  17. {
  18. $list = Db::name('signin')->order('id asc')->select();
  19. //连续签到次数
  20. $user_sign = Db::name('user_sign')->where(['uid' => $this->auth->id])->order('id desc')->find();
  21. $yesterday_time = strtotime('yesterday'); //昨天0点时间戳
  22. $today_time = $yesterday_time + 86400; //今日0点时间戳
  23. if ($user_sign) {
  24. if ($user_sign['createtime'] >= $yesterday_time) {
  25. $data['sign_times'] = $user_sign['times']; //连续签到天数
  26. } else {
  27. $data['sign_times'] = 0;
  28. }
  29. if ($user_sign['createtime'] >= $today_time) {
  30. $data['is_sign'] = 1; //今日是否签到: 1已签到 0未签到
  31. } else {
  32. $data['is_sign'] = 0; //今日是否签到: 1已签到 0未签到
  33. }
  34. } else {
  35. $data['sign_times'] = 0; //连续签到天数
  36. $data['is_sign'] = 0; //今日是否签到: 1已签到 0未签到
  37. }
  38. $data['sign_round_times'] = $data['sign_times'] % 7;
  39. $data['list'] = $list;
  40. $this->success('success',$data);
  41. }
  42. //签到
  43. public function signin(){
  44. Db::startTrans();
  45. try {
  46. $uid = $this->auth->id;
  47. //记录日志
  48. $data = [
  49. 'uid' => $uid,
  50. 'type' => 1,
  51. 'times' => 1,
  52. 'goldnum' => 0,
  53. 'createtime' => time(),
  54. ];
  55. $yesterday_time = strtotime('yesterday'); //昨天0点时间戳
  56. $today_time = $yesterday_time + 86400; //今日0点时间戳
  57. //修正当前次数
  58. $check = Db::name('user_sign')->where('uid',$uid)->order('id desc')->find();
  59. if($check){
  60. //今天只能签一次
  61. if($check['createtime'] >= $today_time){
  62. throw new Exception('今天已经签过了');
  63. }
  64. if ($check['createtime'] >= $yesterday_time) {
  65. $data['times'] = $check['times'] + 1; //连续签到次数
  66. }
  67. }
  68. /*$signin_id = $data['times'] % 7;
  69. if ($signin_id == 0) {
  70. $signin_id = 7;
  71. }
  72. //匹配对应金币数
  73. $list = Db::name('signin')->order('id asc')->column('id,goldnum');
  74. $data['goldnum'] = isset($list[$signin_id]) ? $list[$signin_id] : 0 ;
  75. Db::startTrans();
  76. //记录日志
  77. $log_id = Db::name('user_sign')->insertGetId($data);
  78. if(!$log_id){
  79. Db::rollback();
  80. $this->error('签到失败');
  81. }*/
  82. //加经验
  83. /*$userWhere['id'] = $uid;
  84. $user = model('User')->where($userWhere)->lock(true)->find();
  85. $userNewEmpirical = $user['empirical'] + $data['goldnum'];
  86. $userUpdate['empirical'] = $userNewEmpirical;
  87. // 获取用户当前经验值对应等级
  88. $userconfigModel = new \app\common\model\UserLevelConfig();
  89. $where = [];
  90. $where["empirical"] = ["elt",$userNewEmpirical];
  91. $userexplainstart = $userconfigModel->where($where)->order("empirical","desc")->find();
  92. if (!empty($userexplainstart)) {
  93. if ($userexplainstart['level'] != $user['level']) {
  94. $userUpdate['level'] = $userexplainstart['level'];
  95. }
  96. }
  97. $res = $user->update($userUpdate,$userWhere);
  98. if($res === false){
  99. Db::rollback();
  100. $this->error('经验更新失败');
  101. }*/
  102. //第七天赠送礼物
  103. $gift = [];
  104. $sendGift = config('site.sign_gift');
  105. $userSignGift = model('UserSignGift')->with(['gift'])->find();
  106. if ($data['times'] == 7 && $sendGift == '1' && !empty($userSignGift)) {
  107. $giftData = isset($userSignGift['gift']) ? $userSignGift['gift'] : [];
  108. if (!empty($giftData)) {
  109. $gift = [
  110. 'image' => $giftData['image'],
  111. 'num' => $userSignGift['num'],
  112. ];
  113. }
  114. $giftName = isset($giftData['name']) ? $giftData['name'] : '';
  115. $data['goldnum'] = $gift['num'];
  116. $data['type'] = 2;
  117. $data['remark'] = '签到奖励'.$giftName.$gift['num'];
  118. } else {//随机获取钻石
  119. $signJewel = config('site.sign_jewel');
  120. $signJewelArr = [];
  121. if (!empty($signJewel)) {
  122. $signJewelArr = explode('-', $signJewel);
  123. }
  124. $min = isset($signJewelArr[0]) ? $signJewelArr[0] : 1;
  125. $max = isset($signJewelArr[1]) ? $signJewelArr[1] : 1;
  126. $gift['num'] = rand($min,$max);
  127. if ($gift['num'] < 1) {
  128. throw new Exception('签到功能已被关闭');
  129. }
  130. $data['goldnum'] = $gift['num'];
  131. $data['remark'] = '签到钻石'.$gift['num'];
  132. $user = model('User')->find($uid);
  133. $userJewel = isset($user['jewel']) ? $user['jewel'] : 0;
  134. $rs_wallet = model('wallet')->lockChangeAccountRemain($uid,$gift['num'],'+',$userJewel,$data['remark'],16,'jewel');
  135. if (!$rs_wallet['status']) {
  136. throw new Exception($rs_wallet['msg']);
  137. }
  138. }
  139. //记录日志
  140. $log_id = Db::name('user_sign')->insertGetId($data);
  141. if(!$log_id){
  142. throw new Exception('签到失败');
  143. }
  144. //系统消息
  145. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'签到奖励','签到成功,获得奖励');
  146. $signImg = config('site.sign_img');
  147. $result = [
  148. 'image' => isset($gift['image']) ? $gift['image'] : cdnurl($signImg),
  149. 'goldnum' => isset($gift['num']) ? $gift['num'] : $data['goldnum'],
  150. ];
  151. Db::commit();
  152. $this->success('签到成功', $result);
  153. } catch (Exception $e) {
  154. Db::rollback();
  155. $this->error($e->getMessage());
  156. }
  157. }
  158. //第七天赠送vip
  159. //找出最近七天的次数
  160. private function gift_vip($user_sign_gift_vipdays){
  161. $start = strtotime(date('Y-m-d')) - 518400;
  162. $map = [
  163. 'uid' => $this->auth->id,
  164. 'createtime' => ['gt',$start],
  165. ];
  166. $count = Db::name('user_sign')->where($map)->count();
  167. if($count >= 7){
  168. //赠送vip三天
  169. $add_time = 86400 * $user_sign_gift_vipdays;
  170. //有vip的续费,没有的从现在开始
  171. $vip_endtime = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  172. if($vip_endtime <= time()){
  173. $new_vip_endtime = time() + $add_time;
  174. }else{
  175. $new_vip_endtime = $vip_endtime + $add_time;
  176. }
  177. $rs = Db::name('user_wallet')->where('user_id',$this->auth->id)->update(['vip_endtime'=>$new_vip_endtime]);
  178. if($rs === false){
  179. Db::rollback();
  180. $this->error('连续签到赠送vip失败');
  181. }
  182. return true;
  183. }
  184. return false;
  185. }
  186. }