Example #1
0
File: api.php Project: lughong/shop
 public function del()
 {
     $user_id = $_POST['user_id'];
     $activity_id = $_POST['activity_id'];
     if (!$user_id || !$activity_id) {
         die('却少参数');
     }
     $db = new DB('activity_killsec_user', 'killsec');
     $db->where(array('user_id' => $user_id))->delete();
     $redisKillsec = new RedisKillsec();
     //补充token
     $redisKillsec->save_token($activity_id, $redisKillsec->create_token(1));
 }
Example #2
0
 public function killsec_user_order_clear()
 {
     $db_instance = new DB('activity_killsec_instance', 'killsec');
     $db_user = new DB('activity_killsec_user', 'killsec');
     $redisKillsec = new RedisKillsec();
     $instance_list = $db_instance->where("status!=" . self::KILLSEC_STATUS_FINISH)->select();
     // var_dump( $instance_list );
     if ($instance_list) {
         foreach ($instance_list as $k => $v) {
             $total = $v['killsec_count'];
             if ($v['status'] == self::KILLSEC_STATUS_PREVIEW) {
                 //开始前2分钟生成token
                 if (strtotime($v['start_time']) - 2 * 60 <= time()) {
                     //更新实例表状态
                     $db_instance->where(array('instance_id' => $v['instance_id'], 'status' => self::KILLSEC_STATUS_PREVIEW))->data(array('status' => self::KILLSEC_STATUS_GOING))->update();
                     //生成token
                     $redisKillsec->save_token($v['activity_id'], $redisKillsec->create_token($v['killsec_count']));
                 }
             } elseif ($v['status'] == self::KILLSEC_STATUS_GOING) {
                 $user_list = $db_user->where(array('instance_id' => $v['instance_id']))->select();
                 // var_dump( $user_list );
                 $pay_count = 0;
                 $order_userIds = array();
                 $payment_userIds = array();
                 $user_count = count($user_list);
                 if ($user_list && $user_count > 0) {
                     foreach ($user_list as $sk => $sv) {
                         if (empty($sv['order_time'])) {
                             if (time() - strtotime($sv['join_time']) - self::ORDER_WAIT_TIME > 0) {
                                 //ORDER_WAIT_TIME后没下单
                                 $order_userIds[] = $sv['user_id'];
                             }
                         } elseif (empty($sv['pay_time'])) {
                             if (time() - strtotime($sv['join_time']) - self::PAYMENT_WAIT_TIME > 0) {
                                 //PAYMENT_WAIT_TIME后没支付
                                 $payment_userIds[] = $sv['user_id'];
                             }
                         } elseif (!empty($sv['pay_time'])) {
                             $pay_count += 1;
                         }
                     }
                 }
                 $userIds = $order_userIds + $payment_userIds;
                 $count = count($userIds);
                 if ($count > 0) {
                     //删除过期用户
                     $db_user->in("user_id", $userIds)->delete();
                     //更新参与数量
                     $db_instance->where(array('instance_id' => $v['instance_id']))->data(array('join_count' => $user_count - $count))->update();
                     //补充token
                     $redisKillsec->save_token($v['activity_id'], $redisKillsec->create_token($count));
                 } else {
                     //更新参与数量
                     $db_instance->where(array('instance_id' => $v['instance_id']))->data(array('join_count' => $user_count))->update();
                 }
                 if ($pay_count == $total) {
                     //秒杀完成
                     $db_instance->where(array('instance_id' => $v['instance_id'], 'status' => self::KILLSEC_STATUS_GOING))->data(array('status' => self::KILLSEC_STATUS_FINISH))->update();
                 }
             }
         }
     }
     echo 'ok';
 }
Example #3
0
 public function going($activity_id)
 {
     /*
     user_id int auto_increment primary key,
     instance_id tinyint(4),
     activity_id tinyint(4),
     join_time datetime,
     order_time datetime,
     pay_time datetime,
     create_time datetime,
     status tinyint(4)
     */
     $db_activity = new DB('activity_killsec', 'killsec');
     $db_instance = new DB('activity_killsec_instance', 'killsec');
     $db_user = new DB('activity_killsec_user', 'killsec');
     $redis = new RedisKillsec();
     $activity_info = $db_activity->where(array('activity_id' => $activity_id))->select();
     // var_dump( $activity_info );
     if (!empty($activity_info)) {
         $activity_info = $activity_info[0];
         $datetime = date('Y-m-d H:i:s', time());
         if ($activity_info['activity_start_time'] > $datetime) {
             echo 'activity not start';
             die;
         }
         if ($activity_info['killsec_end_time'] < $datetime) {
             echo 'killsec is over';
             die;
         }
         if ($activity_info['killsec_start_time'] > $datetime) {
             echo 'killsec not start';
             die;
         }
         $instance_id = $db_instance->where("activity_id='{$activity_id}' and status!=" . self::KILLSEC_STATUS_FINISH)->select('instance_id');
         if ($instance_id) {
             $instance_id = $instance_id[0]['instance_id'];
         } else {
             $instance_id = 0;
         }
         $user_id = mt_rand(1000, 9999);
         $user_exist = $redis->get_user($activity_id, $user_id);
         if ($user_exist) {
             echo 'user exist';
         } else {
             $number = $redis->getrand();
             if ($number == 5) {
                 $token = $redis->get_token($activity_id);
                 if ($token) {
                     //之前改用户不存在
                     //根据activity_id获取活动信息、实例信息
                     //模拟插入
                     $data['instance_id'] = $instance_id;
                     $data['activity_id'] = $activity_info['activity_id'];
                     $data['token'] = $token;
                     $data['join_time'] = date("Y-m-d H:i:s");
                     $data['create_time'] = date("Y-m-d H:i:s");
                     $data['status'] = self::STATUS_TRUE;
                     $db_user->data($data)->insert();
                     $redis->save_user($activity_id, $user_id);
                     echo 'ok';
                 } else {
                     echo 'token not exist';
                 }
             } else {
                 echo 'getrand fail';
             }
         }
     } else {
         echo 'not exist activity';
     }
 }