Grabgift.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 = ['test'];
  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. $info = Db::name('grabgift')->where('status',1)->order('id desc')->find();
  37. }
  38. $userlist = Db::name('grabgift_log')->alias('log')
  39. ->field('log.user_id,user.nickname,user.avatar')
  40. ->join('user','log.user_id = user.id','LEFT')
  41. ->where('log.grab_id',$info['id'])->order('log.id asc')->select();
  42. $userlist = list_domain_image($userlist,['avatar']);
  43. $result = [
  44. 'info' => $info,
  45. 'userlist' => $userlist,
  46. ];
  47. $this->success(1,$result);
  48. }
  49. //立即加入
  50. public function joinin(){
  51. $grab_id = input('grab_id',0);
  52. //先用redis看开奖状态
  53. $seat_number = $this->redis->get('kge_grabgift_'.$grab_id);
  54. if($seat_number === false){
  55. $this->error('此期已开奖');
  56. }
  57. //先加入,再去重
  58. $is_joined = $this->redis->hSet('kge_grabgift_data_'.$grab_id,$this->auth->id,$this->auth->id);
  59. if($is_joined == 0){
  60. //第一次设置,返回1;二次覆盖,返回0
  61. $this->error('您已经参与了此期');
  62. }
  63. //数量限制
  64. $count = $this->redis->incr('kge_grabgift_num_'.$grab_id);
  65. if ($count > $seat_number) {
  66. $this->redis->decr('kge_grabgift_num_'.$grab_id);
  67. $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id);
  68. $this->error('此期人数已满');
  69. }
  70. //扣钱
  71. Db::startTrans();
  72. $grab_info = Db::name('grabgift')->where('id',$grab_id)->find();
  73. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,$grab_info['price'],'-',0,'抽礼物第'.$grab_id.'期',38,'jewel');
  74. if($rs_wallet['status'] === false){
  75. Db::rollback();
  76. $this->redis->decr('kge_grabgift_num_'.$grab_id);
  77. $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id);
  78. $this->error($rs_wallet['msg']);
  79. }
  80. //写入日志
  81. $log_data = [
  82. 'grab_id' => $grab_id,
  83. 'user_id' => $this->auth->id,
  84. 'price' => $grab_info['price'],
  85. 'gift_id' => $grab_info['gift_id'],
  86. 'createtime' => time(),
  87. 'grabstatus' => 0,
  88. 'grabtime' => 0,
  89. ];
  90. $log_id = Db::name('grabgift_log')->insertGetId($log_data);
  91. if(!$log_id){
  92. Db::rollback();
  93. $this->redis->decr('kge_grabgift_num_'.$grab_id);
  94. $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id);
  95. $this->error('参与失败');
  96. }
  97. //如果是最后一个,开奖
  98. $gift_name = '';
  99. $winer_nickname = '';
  100. if($count == $seat_number){
  101. $rand_data_new = [];
  102. $rand_data = $this->redis->hGetAll('kge_grabgift_data_'.$grab_id);
  103. foreach($rand_data as $uid => $val){
  104. $rand_data_new[] = $uid;
  105. }
  106. $winer_uid = $rand_data_new[rand(0,4)];
  107. $winer_nickname = Db::name('user')->where('id',$winer_uid)->value('nickname');
  108. //修改
  109. $nowtime = time();
  110. $rs1 = Db::name('grabgift_log')->where('grab_id',$grab_id)->update(['grabtime'=>$nowtime]);
  111. $rs2 = Db::name('grabgift_log')->where('grab_id',$grab_id)->where('user_id',$winer_uid)->update(['grabstatus'=>1]);
  112. $rs3 = Db::name('grabgift')->where('id',$grab_id)->update(['status'=>1,'grabtime'=>$nowtime]);
  113. //发放到背包
  114. $gift_info = Db::name('gift')->where('id',$grab_info['gift_id'])->find();
  115. if(!empty($gift_info)){
  116. $giftdata = [
  117. 'user_id' => $this->auth->id,
  118. 'gift_id' => $grab_info['gift_id'],
  119. 'name' => $gift_info['name'],
  120. 'image' => $gift_info['image'],
  121. 'gif_image' => $gift_info['special'],
  122. 'value' => $gift_info['value'],
  123. 'number' => 1,
  124. 'get_way' => 1,
  125. 'createtime' => $nowtime,
  126. ];
  127. $rs4 = Db::name('gift_back')->insertGetId($giftdata);
  128. $gift_name = $gift_info['name'];
  129. }else{
  130. $rs4 = true;
  131. }
  132. if($rs1 === false || $rs2 === false || $rs3 === false || $rs4 === false){
  133. Db::rollback();
  134. $this->redis->decr('kge_grabgift_num_'.$grab_id);
  135. $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id);
  136. $this->error('参与失败');
  137. }
  138. //开奖了,用不到了
  139. $this->redis->del('kge_grabgift_num_'.$grab_id);
  140. $this->redis->del('kge_grabgift_data_'.$grab_id);
  141. $this->redis->del('kge_grabgift_'.$grab_id);
  142. //自动开启下一轮
  143. if(1 == 1){
  144. $grabgift_data = [
  145. 'price' => config('site.grabgift_price'),
  146. 'gift_id' => config('site.grabgift_giftid'),
  147. 'seat_num' => config('site.grabgift_seatnum'),
  148. 'status' => 0,
  149. 'grabtime' => 0,
  150. 'createtime' => time(),
  151. ];
  152. $grabgift_data['id'] = Db::name('grabgift')->insertGetId($grabgift_data);
  153. //redis标记状态,未开奖
  154. $this->redis->set('kge_grabgift_'.$grabgift_data['id'],$grabgift_data['seat_num']);
  155. }
  156. }
  157. Db::commit();
  158. $result = [
  159. 'grab_id' => $grab_id,
  160. 'count' => $count,
  161. 'is_finish' => $count == $seat_number ? 1 : 0,
  162. 'seat_number' => $seat_number,
  163. 'nickname' => $this->auth->nickname,
  164. 'user_id' => $this->auth->id,
  165. 'avatar' => localpath_to_netpath($this->auth->avatar),
  166. 'gift_name' => $gift_name,
  167. 'winer_nickname' => $winer_nickname,
  168. ];
  169. //socket推送
  170. $this->grabgift_notice($result);
  171. $this->success(1,$result);
  172. }
  173. //我的参与记录
  174. public function myjoinlog(){
  175. $userlist = Db::name('grabgift_log')->field('grab_id,grabtime,grabstatus')
  176. ->where('user_id',$this->auth->id)->order('id desc')->autopage()->select();
  177. $this->success(1,$userlist);
  178. }
  179. //送礼完成之后,大礼物飘屏
  180. private function grabgift_notice($result){
  181. $notice = new GatewayworkerTools();
  182. $notice->sendGrabgift($result);
  183. }
  184. public function test(){
  185. $result = [
  186. 'grab_id' => 1,
  187. 'count' => 4,
  188. 'is_finish' => 0,
  189. 'seat_number' => 5,
  190. 'nickname' => '加入者昵称',
  191. 'user_id' => 1,
  192. 'avatar' => '/1.jpg',
  193. 'gift_name' => '礼物名',
  194. 'winer_nickname' => '中奖者昵称',
  195. ];
  196. $this->grabgift_notice($result);
  197. }
  198. }