Grabgift.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use Redis;
  6. use app\common\library\GatewayworkerTools;
  7. /**
  8. * 示例接口
  9. */
  10. class Grabgift extends Api
  11. {
  12. protected $noNeedLogin = [];
  13. // 无需鉴权的接口,*表示全部
  14. protected $noNeedRight = ['*'];
  15. protected $redis;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. //redis
  20. $redis = new Redis();
  21. $redisconfig = config("redis");
  22. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  23. if ($redisconfig['redis_pwd']) {
  24. $redis->auth($redisconfig['redis_pwd']);
  25. }
  26. if($redisconfig['redis_selectdb'] > 0){
  27. $redis->select($redisconfig['redis_selectdb']);
  28. }
  29. $this->redis = $redis;
  30. }
  31. //首页
  32. public function index()
  33. {
  34. $info = Db::name('grabgift')->where('status',0)->order('id desc')->find();
  35. if(empty($info)){
  36. $grabgift_data = [
  37. 'price' => config('site.grabgift_price'),
  38. 'gift_id' => config('site.grabgift_giftid'),
  39. 'status' => 0,
  40. 'grabtime' => 0,
  41. 'createtime' => time(),
  42. ];
  43. $grabgift_data['id'] = Db::name('grabgift')->insertGetId($grabgift_data);
  44. $info = $grabgift_data;
  45. //redis标记状态,未开奖
  46. $this->redis->set('kge_grabgift_'.$grabgift_data['id'],0);
  47. }
  48. $userlist = Db::name('grabgift_log')->alias('log')
  49. ->field('log.user_id,user.nickname,user.avatar')
  50. ->join('user','log.user_id = user.id','LEFT')
  51. ->where('log.grab_id',$info['id'])->order('log.id asc')->select();
  52. $userlist = list_domain_image($userlist,['avatar']);
  53. $result = [
  54. 'info' => $info,
  55. 'userlist' => $userlist,
  56. ];
  57. $this->success(1,$result);
  58. }
  59. //立即加入
  60. public function joinin(){
  61. $grab_id = input('grab_id',0);
  62. $seat_number = 5;
  63. //先用redis看开奖状态
  64. $status = $this->redis->get('kge_grabgift_'.$grab_id);
  65. //dump($status);
  66. if($status === false){
  67. $this->error('此期已开奖');
  68. }
  69. if($status == 1){
  70. $this->redis->del('kge_grabgift_'.$grab_id);
  71. $this->error('此期已经开奖了');
  72. }
  73. //先加入,再去重
  74. $is_joined = $this->redis->hSet('kge_grabgift_data_'.$grab_id,$this->auth->id,$this->auth->id);
  75. if($is_joined == 0){
  76. //第一次设置,返回1;二次覆盖,返回0
  77. $this->error('您已经参与了此期');
  78. }
  79. //数量限制
  80. $count = $this->redis->incr('kge_grabgift_num_'.$grab_id);
  81. if ($count > $seat_number) {
  82. $this->redis->decr('kge_grabgift_num_'.$grab_id);
  83. $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id);
  84. $this->error('此期人数已满');
  85. }
  86. //扣钱
  87. Db::startTrans();
  88. $grab_info = Db::name('grabgift')->where('id',$grab_id)->find();
  89. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,$grab_info['price'],'-',0,'抽礼物第'.$grab_id.'期',38,'jewel');
  90. if($rs_wallet['status'] === false){
  91. Db::rollback();
  92. $this->redis->decr('kge_grabgift_num_'.$grab_id);
  93. $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id);
  94. $this->error($rs_wallet['msg']);
  95. }
  96. //写入日志
  97. $log_data = [
  98. 'grab_id' => $grab_id,
  99. 'user_id' => $this->auth->id,
  100. 'price' => $grab_info['price'],
  101. 'gift_id' => $grab_info['gift_id'],
  102. 'createtime' => time(),
  103. 'grabstatus' => 0,
  104. 'grabtime' => 0,
  105. ];
  106. $log_id = Db::name('grabgift_log')->insertGetId($log_data);
  107. if(!$log_id){
  108. Db::rollback();
  109. $this->redis->decr('kge_grabgift_num_'.$grab_id);
  110. $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id);
  111. $this->error('参与失败');
  112. }
  113. //如果是最后一个,开奖
  114. $gift_name = '';
  115. $winer_nickname = '';
  116. if($count == $seat_number){
  117. $rand_data_new = [];
  118. $rand_data = $this->redis->hGetAll('kge_grabgift_data_'.$grab_id);
  119. foreach($rand_data as $uid => $val){
  120. $rand_data_new[] = $uid;
  121. }
  122. $winer_uid = $rand_data_new[rand(0,4)];
  123. $winer_nickname = Db::name('user')->where('id',$winer_uid)->value('nickname');
  124. //修改
  125. $nowtime = time();
  126. $rs1 = Db::name('grabgift_log')->where('grab_id',$grab_id)->update(['grabtime'=>$nowtime]);
  127. $rs2 = Db::name('grabgift_log')->where('grab_id',$grab_id)->where('user_id',$winer_uid)->update(['grabstatus'=>1]);
  128. $rs3 = Db::name('grabgift')->where('id',$grab_id)->update(['status'=>1,'grabtime'=>$nowtime]);
  129. //发放到背包
  130. $gift_info = Db::name('gift')->where('id',$grab_info['gift_id'])->find();
  131. if(!empty($gift_info)){
  132. $giftdata = [
  133. 'user_id' => $this->auth->id,
  134. 'gift_id' => $grab_info['gift_id'],
  135. 'name' => $gift_info['name'],
  136. 'image' => $gift_info['image'],
  137. 'gif_image' => $gift_info['special'],
  138. 'value' => $gift_info['value'],
  139. 'number' => 1,
  140. 'get_way' => 1,
  141. 'createtime' => $nowtime,
  142. ];
  143. $rs4 = Db::name('gift_back')->insertGetId($giftdata);
  144. $gift_name = $gift_info['name'];
  145. }else{
  146. $rs4 = true;
  147. }
  148. if($rs1 === false || $rs2 === false || $rs3 === false || $rs4 === false){
  149. Db::rollback();
  150. $this->redis->decr('kge_grabgift_num_'.$grab_id);
  151. $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id);
  152. $this->error('参与失败');
  153. }
  154. //开奖了,用不到了
  155. $this->redis->del('kge_grabgift_num_'.$grab_id);
  156. $this->redis->del('kge_grabgift_data_'.$grab_id);
  157. $this->redis->del('kge_grabgift_'.$grab_id);
  158. }
  159. Db::commit();
  160. $result = [
  161. 'grab_id' => $grab_id,
  162. 'count' => $count,
  163. 'is_finish' => $count == $seat_number ? 1 : 0,
  164. 'seat_number' => $seat_number,
  165. 'nickname' => $this->auth->nickname,
  166. 'user_id' => $this->auth->id,
  167. 'avatar' => localpath_to_netpath($this->auth->avatar),
  168. 'gift_name' => $gift_name,
  169. 'winer_nickname' => $winer_nickname,
  170. ];
  171. //socket推送
  172. $this->grabgift_notice($result);
  173. $this->success(1,$result);
  174. }
  175. //我的参与记录
  176. public function myjoinlog(){
  177. $userlist = Db::name('grabgift_log')->field('grab_id,grabtime,grabstatus')
  178. ->where('user_id',$this->auth->id)->order('id desc')->autopage()->select();
  179. $this->success(1,$userlist);
  180. }
  181. //送礼完成之后,大礼物飘屏
  182. private function grabgift_notice($result){
  183. $notice = new GatewayworkerTools();
  184. $notice->sendGrabgift($result);
  185. }
  186. }