connect($redisconfig["host"], $redisconfig["port"]); if ($redisconfig['redis_pwd']) { $redis->auth($redisconfig['redis_pwd']); } if($redisconfig['redis_selectdb'] > 0){ $redis->select($redisconfig['redis_selectdb']); } $this->redis = $redis; } //首页 public function index() { $info = Db::name('grabgift')->where('status',0)->order('id desc')->find(); if(empty($info)){ $grabgift_data = [ 'price' => config('site.grabgift_price'), 'gift_id' => config('site.grabgift_giftid'), 'status' => 0, 'grabtime' => 0, 'createtime' => time(), ]; $grabgift_data['id'] = Db::name('grabgift')->insertGetId($grabgift_data); $info = $grabgift_data; //redis标记状态,未开奖 $this->redis->set('kge_grabgift_'.$grabgift_data['id'],0); } $userlist = Db::name('grabgift_log')->alias('log') ->field('log.user_id,user.nickname,user.avatar') ->join('user','log.user_id = user.id','LEFT') ->where('log.grab_id',$info['id'])->order('log.id asc')->select(); $userlist = list_domain_image($userlist,['avatar']); $result = [ 'info' => $info, 'userlist' => $userlist, ]; $this->success(1,$result); } //立即加入 public function joinin(){ $grab_id = input('grab_id',0); $seat_number = 5; //先用redis看开奖状态 $status = $this->redis->get('kge_grabgift_'.$grab_id); //dump($status); if($status === false){ $this->error('此期已开奖'); } if($status == 1){ $this->redis->del('kge_grabgift_'.$grab_id); $this->error('此期已经开奖了'); } //先加入,再去重 $is_joined = $this->redis->hSet('kge_grabgift_data_'.$grab_id,$this->auth->id,$this->auth->id); if($is_joined == 0){ //第一次设置,返回1;二次覆盖,返回0 $this->error('您已经参与了此期'); } //数量限制 $count = $this->redis->incr('kge_grabgift_num_'.$grab_id); if ($count > $seat_number) { $this->redis->decr('kge_grabgift_num_'.$grab_id); $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id); $this->error('此期人数已满'); } //扣钱 Db::startTrans(); $grab_info = Db::name('grabgift')->where('id',$grab_id)->find(); $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,$grab_info['price'],'-',0,'抽礼物第'.$grab_id.'期',38,'jewel'); if($rs_wallet['status'] === false){ Db::rollback(); $this->redis->decr('kge_grabgift_num_'.$grab_id); $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id); $this->error($rs_wallet['msg']); } //写入日志 $log_data = [ 'grab_id' => $grab_id, 'user_id' => $this->auth->id, 'price' => $grab_info['price'], 'gift_id' => $grab_info['gift_id'], 'createtime' => time(), 'grabstatus' => 0, 'grabtime' => 0, ]; $log_id = Db::name('grabgift_log')->insertGetId($log_data); if(!$log_id){ Db::rollback(); $this->redis->decr('kge_grabgift_num_'.$grab_id); $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id); $this->error('参与失败'); } //如果是最后一个,开奖 $gift_name = ''; $winer_nickname = ''; if($count == $seat_number){ $rand_data_new = []; $rand_data = $this->redis->hGetAll('kge_grabgift_data_'.$grab_id); foreach($rand_data as $uid => $val){ $rand_data_new[] = $uid; } $winer_uid = $rand_data_new[rand(0,4)]; $winer_nickname = Db::name('user')->where('id',$winer_uid)->value('nickname'); //修改 $nowtime = time(); $rs1 = Db::name('grabgift_log')->where('grab_id',$grab_id)->update(['grabtime'=>$nowtime]); $rs2 = Db::name('grabgift_log')->where('grab_id',$grab_id)->where('user_id',$winer_uid)->update(['grabstatus'=>1]); $rs3 = Db::name('grabgift')->where('id',$grab_id)->update(['status'=>1,'grabtime'=>$nowtime]); //发放到背包 $gift_info = Db::name('gift')->where('id',$grab_info['gift_id'])->find(); if(!empty($gift_info)){ $giftdata = [ 'user_id' => $this->auth->id, 'gift_id' => $grab_info['gift_id'], 'name' => $gift_info['name'], 'image' => $gift_info['image'], 'gif_image' => $gift_info['special'], 'value' => $gift_info['value'], 'number' => 1, 'get_way' => 1, 'createtime' => $nowtime, ]; $rs4 = Db::name('gift_back')->insertGetId($giftdata); $gift_name = $gift_info['name']; }else{ $rs4 = true; } if($rs1 === false || $rs2 === false || $rs3 === false || $rs4 === false){ Db::rollback(); $this->redis->decr('kge_grabgift_num_'.$grab_id); $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id); $this->error('参与失败'); } //开奖了,用不到了 $this->redis->del('kge_grabgift_num_'.$grab_id); $this->redis->del('kge_grabgift_data_'.$grab_id); $this->redis->del('kge_grabgift_'.$grab_id); } Db::commit(); $result = [ 'grab_id' => $grab_id, 'count' => $count, 'is_finish' => $count == $seat_number ? 1 : 0, 'seat_number' => $seat_number, 'nickname' => $this->auth->nickname, 'user_id' => $this->auth->id, 'avatar' => localpath_to_netpath($this->auth->avatar), 'gift_name' => $gift_name, 'winer_nickname' => $winer_nickname, ]; //socket推送 $this->grabgift_notice($result); $this->success(1,$result); } //我的参与记录 public function myjoinlog(){ $userlist = Db::name('grabgift_log')->field('grab_id,grabtime,grabstatus') ->where('user_id',$this->auth->id)->order('id desc')->autopage()->select(); $this->success(1,$userlist); } //送礼完成之后,大礼物飘屏 private function grabgift_notice($result){ $notice = new GatewayworkerTools(); $notice->sendGrabgift($result); } }