예제 #1
0
 /**
  * 逻辑
  * 插入前,判断 排名 < 1000,执行插入。
  * 插入后,判断 排名 < 1000,更新总数;否则,删除。
  */
 public function handle_request()
 {
     $apf = APF::get_instance();
     $request = $apf->get_request();
     $response = $apf->get_response();
     $cache = APF_Cache_Factory::get_instance()->get_memcache();
     // CurrentRoundTime & NextRoundTime
     $currentRoundTime = null;
     $nextRoundTime = null;
     $startDate = Util_Christmas::hammerRushStartDate();
     $endDate = Util_Christmas::hammerRushEndDate();
     if (!Util_Christmas::hammerRushStarted()) {
         $currentRoundTime = null;
         $nextRoundTime = date("{$startDate} 10:00:00");
     } elseif (!Util_Christmas::hammerRushEnded()) {
         $currentHour = date('H');
         if ($currentHour < 10) {
             if (date('Y-m-d') != $startDate) {
                 $currentRoundTime = date('Y-m-d 18:00:00', strtotime('-1 day'));
             }
             $nextRoundTime = date('Y-m-d 10:00:00');
         } elseif ($currentHour >= 10 && $currentHour < 14) {
             $currentRoundTime = date('Y-m-d 10:00:00');
             $nextRoundTime = date('Y-m-d 14:00:00');
         } elseif ($currentHour >= 14 && $currentHour < 18) {
             $currentRoundTime = date('Y-m-d 14:00:00');
             $nextRoundTime = date('Y-m-d 18:00:00');
         } elseif ($currentHour >= 18) {
             $currentRoundTime = date('Y-m-d 18:00:00');
             if (date('Y-m-d') != $endDate) {
                 $nextRoundTime = date('Y-m-d 10:00:00', strtotime('+1 day'));
             }
         }
     }
     // IsRushable
     $isRushable = false;
     // 当前在可抢时间范围内
     if ($currentRoundTime && time() < strtotime($currentRoundTime) + 30 * 60) {
         $isRushable = true;
     }
     // 当前可抢开关
     $result = $cache->get(Util_Christmas::hammerIsRushableCacheKey($currentRoundTime));
     if ($result !== FALSE) {
         $isRushable = (bool) $result;
     }
     try {
         // Not Rushable
         if (!$isRushable) {
             throw new Exception_ChristmasException('Not rushable.', self::CODE_FAILED);
         }
         // 判断活动是否已开始
         if (!Util_Christmas::hammerRushStarted()) {
             throw new Exception_ChristmasException('Hammer rush is not started.', self::CODE_NOT_START);
         }
         // 判断活动是否已结束
         if (Util_Christmas::hammerRushEnded()) {
             throw new Exception_ChristmasException('Hammer rush is ended.', self::CODE_ENDED);
         }
         // 判断经纪人是否已经登陆
         if (!$this->isBrokerLogin()) {
             throw new Exception_ChristmasException('Login first.', self::CODE_NOT_LOGIN);
         }
         // 获取经纪人详情
         self::$BrokerInfo = Bll_BrokerBaseBll::get_instance()->get_broker_info(self::$intBrokerID);
         $brokerId = self::$BrokerInfo['BaseInfo'][strtoupper('brokerid')];
         $userId = self::$BrokerInfo['BaseInfo'][strtoupper('userId')];
         // 判断经纪人是否通过双证验证
         if (self::$BrokerInfo['BaseInfo'][strtoupper('checkstate')] == 0) {
             throw new Exception_ChristmasException('Check state first.', self::CODE_NOT_CHECK_STATE);
         }
         // 插入前,判断是否本轮是否已经抢过
         try {
             $hammerLog = Model_Christmas_HammerLog::data_access()->force_master()->filter_by_op('brokerId', '=', $brokerId)->filter_by_op('type', '=', Model_Christmas_HammerLog::TYPE_HAMMER_RUSH)->filter_by_op('createdAt', '>=', $currentRoundTime)->find_one();
         } catch (Exception $e) {
             $hammerLog = false;
         }
         if ($hammerLog) {
             throw new Exception_ChristmasException('Already rushed.', self::CODE_RUSHED);
         }
         // 经纪人添加1把锤子
         $hammerLog = new Model_Christmas_HammerLog();
         $hammerLog->brokerId = $brokerId;
         $hammerLog->type = Model_Christmas_HammerLog::TYPE_HAMMER_RUSH;
         $hammerLog->number = 1;
         $hammerLog->memo = '整点抢锤子';
         $hammerLog->createdAt = date('Y-m-d H:i:s');
         $hammerLog->updatedAt = $hammerLog->createdAt;
         $hammerLog->save();
         // 本轮有效锤子数
         $hammerRoundNumber = 1000;
         // 插入后,判断 排名 < 1000,更新总数;否则,删除。
         $rank = $this->current_round_rank($hammerLog->id, $currentRoundTime);
         if ($rank >= $hammerRoundNumber) {
             $hammerLog->delete();
             $isRushable = false;
             $cache->set(Util_Christmas::hammerIsRushableCacheKey($currentRoundTime), false);
             throw new Exception_ChristmasException('Not rushed.', self::CODE_FAILED);
         }
         // 更新经纪人总锤子
         $brokerHammer = Model_Christmas_BrokerHammer::incrBrokerHammerNumber($brokerId, 1);
         // 发送系统消息
         $messages = Util_Christmas::messages();
         if (isset($messages[Model_Christmas_HammerLog::TYPE_HAMMER_RUSH])) {
             Bll_Service_Message::sendSystem(array('receiverIds' => $userId, 'message' => $messages[Model_Christmas_HammerLog::TYPE_HAMMER_RUSH]));
         }
         $result = array('code' => self::CODE_SUCCESS, 'message' => 'Success.', 'number' => $brokerHammer->number, 'currentTime' => strtotime($currentRoundTime) * 1000, 'nextTime' => $isRushable ? strtotime($currentRoundTime) * 1000 : strtotime($nextRoundTime) * 1000, 'isRushable' => intval($isRushable));
     } catch (Exception_ChristmasException $e) {
         $result = array('code' => $e->getCode(), 'message' => $e->getMessage(), 'nextTime' => $isRushable ? strtotime($currentRoundTime) * 1000 : strtotime($nextRoundTime) * 1000, 'isRushable' => intval($isRushable), 'isLogin' => intval($this->isBrokerLogin()), 'loginUrl' => Uri_ChristmasUrl::login_url(Uri_ChristmasUrl::hammer_url()));
     } catch (Exception $e) {
         // 500 Error
         // http_response_code('500');
         $result = array('code' => '500', 'message' => 'Server Error.');
     }
     $response->set_content_type('application/json', 'UTF-8');
     $response->add_header('Cache-Control', 'no-cache, no-store, must-revalidate');
     $response->add_header('Pragma', 'no-cache');
     $response->add_header('Expires', 0);
     echo json_encode($result);
     die;
 }
예제 #2
0
 /**
  * 写锤子日志
  * @param $broker_id
  */
 function write_hammer_log($broker_id, $num, $mome)
 {
     $hammerLog = new Model_Christmas_HammerLog();
     $hammerLog->brokerId = $broker_id;
     $hammerLog->type = Model_Christmas_HammerLog::TYPE_USE_HAMMER;
     $hammerLog->number = $num;
     $hammerLog->memo = $mome;
     $hammerLog->createdAt = date('Y-m-d H:i:s');
     $hammerLog->updatedAt = $hammerLog->createdAt;
     $hammerLog->save();
 }
예제 #3
0
 /**
  * 积分换锤子
  *
  * @param $brokerId
  * @param $number
  *
  * @return Model_Christmas_BrokerHammer
  */
 public static function incrBrokerHammerForGrade($brokerId, $number = 1)
 {
     // 创建锤子日志
     $hammerLog = new Model_Christmas_HammerLog();
     $hammerLog->brokerId = $brokerId;
     $hammerLog->type = Model_Christmas_HammerLog::TYPE_GRADE_EXCHANGE;
     $hammerLog->number = $number;
     $hammerLog->memo = '积分换锤子';
     $hammerLog->createdAt = date('Y-m-d H:i:s');
     $hammerLog->updatedAt = $hammerLog->createdAt;
     $hammerLog->save();
     // 更新锤子总数
     $brokerHammer = Model_Christmas_BrokerHammer::incrBrokerHammerNumber($brokerId, $number);
     return $brokerHammer;
 }