|
@@ -45,11 +45,14 @@ class Grabgift extends Api
|
|
|
'price' => config('site.grabgift_price'),
|
|
|
'gift_id' => config('site.grabgift_giftid'),
|
|
|
'status' => 0,
|
|
|
- 'grabtime' => time(),
|
|
|
+ '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')
|
|
@@ -66,4 +69,134 @@ class Grabgift extends Api
|
|
|
$this->success(1,$result);
|
|
|
}
|
|
|
|
|
|
+ //立即加入
|
|
|
+ public function joinin(){
|
|
|
+ $grab_id = input('grab_id',0);
|
|
|
+ $kai_count = 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 > $kai_count) {
|
|
|
+ $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('参与失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果是最后一个,开奖
|
|
|
+ if($count == $kai_count){
|
|
|
+
|
|
|
+ //此期已开奖
|
|
|
+ $this->redis->del('kge_grabgift_'.$grab_id);
|
|
|
+
|
|
|
+ //
|
|
|
+ $rand_data_new = [];
|
|
|
+ $rand_data = $this->redis->hGetAll('kge_grabgift_data_'.$grab_id);
|
|
|
+ foreach($rand_data as $uid => $val){
|
|
|
+ $rand_data_new[] = $uid;
|
|
|
+ }
|
|
|
+ $zhongjiang_uid = $rand_data_new[rand(0,4)];
|
|
|
+
|
|
|
+ //修改
|
|
|
+ $rs1 = Db::name('grabgift_log')->where('grab_id',$grab_id)->where('user_id',$zhongjiang_uid)->update(['grabstatus'=>1,'grabtime'=>time()]);
|
|
|
+ if($rs1 === false){
|
|
|
+ Db::rollback();
|
|
|
+
|
|
|
+ $this->redis->decr('kge_grabgift_num_'.$grab_id);
|
|
|
+ $this->redis->hDel('kge_grabgift_data_'.$grab_id,$this->auth->id);
|
|
|
+ $this->error('参与失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ $rs2 = Db::name('grabgift')->where('id',$grab_id)->update(['status'=>1,'grabtime'=>time()]);
|
|
|
+ if($rs2 === 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);
|
|
|
+
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+
|
|
|
+ $result = [
|
|
|
+ 'grab_id'=>$grab_id,
|
|
|
+ 'count' => $count,
|
|
|
+ 'is_finish' => $count == $kai_count ? 1 : 0,
|
|
|
+ ];
|
|
|
+ $this->success(1,$result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test(){
|
|
|
+ $grab_id = 3;
|
|
|
+ $rs = $this->redis->hSet('kge_grabgift_data_'.$grab_id,1,11);
|
|
|
+ dump($rs);
|
|
|
+ $rs = $this->redis->hSet('kge_grabgift_data_'.$grab_id,2,11);
|
|
|
+ dump($rs);
|
|
|
+ $rs = $this->redis->hSet('kge_grabgift_data_'.$grab_id,3,11);
|
|
|
+ dump($rs);
|
|
|
+ $rs = $this->redis->hSet('kge_grabgift_data_'.$grab_id,4,11);
|
|
|
+ dump($rs);
|
|
|
+
|
|
|
+
|
|
|
+ $count = $this->redis->incrby('kge_grabgift_num_'.$grab_id,4);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|