public function handle_request_internal() { /** * 签到小区详情 * * - 验证参数 * * * - 获取经纪人 * * - 获取小区(AjkCommunity) * - * * - 获取小区签到人数 * * - 判断经纪人是否可签到(经纪人是否已签到、当前是否在签到时间段) * - 校验小区坐标和lat、lng的距离5公里,超出不可签到(如小区无坐标,不可签到) * * - 计算距离下一时间点的签到倒计时(单位:s) * * - 获取小区各个时间段签到前3名 * * - 返回数据 */ // 获取参数 $brokerId = $this->_params['brokerId']; $commId = $this->_params['commId']; $lat = $this->_params['lat']; $lng = $this->_params['lng']; // 根据ID获取经纪人 $broker = Bll_Commsign::getBrokerInfo($brokerId); if (!$broker) { $broker = Model_Broker_AjkBrokerExtend::data_access()->filter('brokerId', $brokerId)->find_only(); if (!$broker) { throw new Exception('经纪人不存在', Const_APIStatus::E_BROKER_NOT_EXISTS); } $broker_photo = Util_ImageUtils::get_broker_photo_url($broker['userPhoto'], '200x200'); $brokerInfo = array('brokerId' => $broker['brokerId'], 'username' => $broker['trueName'], 'userPhoto' => $broker_photo, 'cityId' => $broker['cityId']); Bll_Commsign::redisBrokerInfo($brokerId, $brokerInfo); } // 根据ID获取小区 $community = Bll_Commsign::getCommInfo($commId); if (empty($community)) { $community = Bll_Community_APIComm::getInstance()->getInfoByIdMapping($commId, 2); $commInfo = array(); $commInfo['commId'] = $community['commId']; $commInfo['commName'] = $community['commName']; $commInfo['sosolng'] = $community['sosolng']; $commInfo['sosolat'] = $community['sosolat']; Bll_Commsign::setCommInfo($commId, $commInfo); } if (!$community || !$community['sosolat'] || !$community['sosolng']) { throw new Exception('小区不存在', Const_APIStatus::E_COMMUNITY_NOT_EXISTS); } // 获取小区签到人数 $communitySignCount = Bll_Commsign::getCommCount($commId); // 判断经纪人是否可签到 $signAble = true; $currentSignRange = Bll_Broker_CommunitySign::getCurrentSignRange(); if (!$currentSignRange) { $signAble = false; } else { // 经纪人是否已经签过到 if (Bll_Commsign::isSignedAlready($brokerId, $commId, $currentSignRange[0])) { $signAble = false; } } // 对比小区坐标和参数坐标两点的距离(5km) $distance = Util_Map::distance($lat, $lng, $community['sosolat'], $community['sosolng']); if ($distance > 5000) { $signAble = false; } $data = array(); $data['signAble'] = intval($signAble); $data['signCount'] = $communitySignCount; $data['countDown'] = $signAble ? 0 : Bll_Broker_CommunitySign::nextSignCountDown(); $data['signList'] = array(); // 获取各时间段签到前三名 if (Bll_Broker_CommunitySign::isTopSignerActivityOngoing()) { $data['signList'] = array(array('hour' => '10:00', 'brokers' => array()), array('hour' => '15:00', 'brokers' => array()), array('hour' => '20:00', 'brokers' => array())); foreach ($data['signList'] as &$theSignList) { $theHour = intval($theSignList['hour']); $hour = intval(date('H')); if ($hour >= 0 && $hour < 10) { break; } elseif ($hour >= 10 && $hour < 20) { if ($theHour > $hour) { continue; } } $theSignRange = array(); switch ($theHour) { case 10: $theSignRange = array(date('Y-m-d 10:00:00'), date('Y-m-d 14:59:59')); break; case 15: $theSignRange = array(date('Y-m-d 15:00:00'), date('Y-m-d 19:59:59')); break; case 20: $theSignRange = array(date('Y-m-d 20:00:00'), date('Y-m-d 23:59:59')); break; } $theSignList['brokers'] = Bll_Commsign::getFirstThr($commId, $theSignRange[0]); if (empty($theSignList['brokers'])) { $theSignList['brokers'] = Model_Broker_CommunitySign::data_access(date('Ym', strtotime($currentSignRange[0])))->filter('communityId', $commId)->filter_by_op_multi(array(array('signTime', '>=', $theSignRange[0]), array('signTime', '<=', $theSignRange[1])))->sort('signTime', 'asc')->limit(3)->get_all(); } $tmp = array(); if (!empty($theSignList['brokers'])) { foreach ($theSignList['brokers'] as $k => $v) { $brokerTmp = Bll_Commsign::getBrokerInfo($v['brokerId']); if (!$brokerTmp) { $broker = Model_Broker_AjkBrokerExtend::data_access()->filter('brokerId', $v['brokerId'])->find_only(); $broker_photo = Util_ImageUtils::get_broker_photo_url($broker['userPhoto'], '200x200'); $brokerTmp = array('brokerId' => $broker['brokerId'], 'username' => $broker['trueName'], 'userPhoto' => $broker_photo, 'cityId' => $broker['cityId']); Bll_Commsign::redisBrokerInfo($v['brokerId'], $brokerTmp); } if (empty($brokerTmp['userPhoto'])) { $brokerTmp['userPhoto'] = PageHelper::pure_static_url('/img/bknoimg.gif'); } $dataTmp = array('brokerId' => $brokerTmp['brokerId'], 'brokerTrueName' => $brokerTmp['username'], 'brokerPhoto' => $brokerTmp['userPhoto']); $tmp[] = $dataTmp; } $theSignList['brokers'] = $tmp; } } } return array('status' => Const_APIStatus::RETURN_CODE_OK, 'data' => $data); }
public function handle_request_internal() { /** * 小区签到逻辑 * * - 验证参数 * * - 获取经纪人 * * - 获取小区(AjkCommunity) * - 校验小区坐标和lat、lng的距离5公里(如小区无坐标,直接拒绝) * * - 添加签到记录 * - 添加签到记录 * - 更新签到按日统计信息(incr) * * - 更新经纪人坐标 * - 添加经纪人坐标日志 * - 更新经纪人坐标 * * - 返回数据 */ $result = array('status' => 'error', 'errcode' => '', 'message' => ''); // 获取参数 $brokerId = $this->_params['brokerId']; $commId = $this->_params['commId']; $lat = $this->_params['lat']; $lng = $this->_params['lng']; if (empty($lat) || empty($lng)) { $result['message'] = '获取定位数据失败,请重新定位'; return $result; } // 根据ID获取经纪人 $broker = Bll_Commsign::getBrokerInfo($brokerId); if (!$broker) { $broker = Model_Broker_AjkBrokerExtend::data_access()->filter('brokerId', $brokerId)->find_only(); if (!$broker) { throw new Exception('经纪人不存在', Const_APIStatus::E_BROKER_NOT_EXISTS); } $broker_photo = Util_ImageUtils::get_broker_photo_url($broker['userPhoto'], '200x200'); $brokerInfo = array('brokerId' => $broker['brokerId'], 'username' => $broker['trueName'], 'userPhoto' => $broker_photo, 'cityId' => $broker['cityId']); Bll_Commsign::redisBrokerInfo($brokerId, $brokerInfo); } // 根据ID获取小区 $community = Bll_Commsign::getCommInfo($commId); if (empty($community)) { $community = Bll_Community_APIComm::getInstance()->getInfoByIdMapping($commId, 2); $commInfo = array(); $commInfo['commId'] = $community['commId']; $commInfo['commName'] = $community['commName']; $commInfo['sosolng'] = $community['sosolng']; $commInfo['sosolat'] = $community['sosolat']; Bll_Commsign::setCommInfo($commId, $commInfo); } // 小区不存在或小区经纬租表不存在时,返回小区不存在 if (!$community || !$community['sosolat'] || !$community['sosolng']) { $result['errcode'] = Const_APIStatus::E_COMMUNITY_NOT_EXISTS; $result['message'] = '小区暂未开通签到功能'; return $result; } // 对比小区坐标和参数坐标两点的距离(5km) $distance = Util_Map::distance($lat, $lng, $community['sosolat'], $community['sosolng']); if ($distance > 5000) { $result['errcode'] = Const_APIStatus::E_BROKER_COMMSIGN_FAR_AWAY; $result['message'] = '签到失败!你跑得太远了,请返回重新定位'; return $result; } // 获取当前所处签到时间(10:00 15:00 20:00) $currentSignRange = Bll_Broker_CommunitySign::getCurrentSignRange(); if (!$currentSignRange) { $result['errcode'] = Const_APIStatus::E_BROKER_COMMSIGN_NOT_IN_RANGE; $result['message'] = '不在签到时间范围'; return $result; } // 判断是否已经签到 if (Bll_Commsign::isSigned($brokerId, $commId, $currentSignRange[0])) { $result['errcode'] = Const_APIStatus::E_BROKER_COMMSIGN_ALREADY_SIGNED; $result['message'] = '已经签到!'; return $result; } try { // 新建签到, $sign = array('brokerId' => $brokerId, 'communityId' => $commId, 'lat' => $lat, 'lng' => $lng, 'signTime' => date('Y-m-d H:i:s'), 'currentSignRange' => $currentSignRange[0]); Bll_Commsign::Signed($brokerId, $commId, $currentSignRange[0], $sign); //缓存‘签到信息’到同步数据库的redis中 Bll_RedisToDb::writeBrokerCommunitySign($sign); // 缓存经纪人位置 $locationInfo = array('brokerId' => $brokerId, 'lng' => $lng, 'lat' => $lat, 'updateTime' => date('Y-m-d H:i:s')); Bll_Commsign::setLocationInfo($brokerId, $locationInfo); //缓存‘位置信息’到同步数据库的redis中 Bll_RedisToDb::writeBrokerLocation($locationInfo); //记录每日签到情况 Bll_Commsign::dailySignedRecord($brokerId); // 获取签到排名 $rank = Bll_Commsign::getSignRank($commId, $currentSignRange[0]); //连续签到统计 $SignStatistics = Bll_Commsign::getSignStatistics($brokerId); $sendFlag = false; if ($SignStatistics) { if (date('Y-m-d', strtotime($SignStatistics['updateTime'])) == date('Y-m-d', strtotime('yesterday'))) { $SignStatistics['signCount'] = $SignStatistics['signCount'] + 1; $sendFlag = true; } elseif (date('Y-m-d', strtotime($SignStatistics['updateTime'])) == date('Y-m-d', time())) { } else { $SignStatistics['signCount'] = 1; $sendFlag = true; } if ($SignStatistics['signCount'] > $SignStatistics['maxSignCount']) { $SignStatistics['maxSignCount'] = $SignStatistics['signCount']; } $SignStatistics['updateTime'] = date('Y-m-d H:i:s'); Bll_Commsign::setSignStatistics($brokerId, $SignStatistics); //缓存‘签到统计信息’到同步数据库的redis中 $SignStatistics = array_merge($SignStatistics, array('sendFlag' => $sendFlag)); Bll_RedisToDb::writeCommSignStatistics($SignStatistics); } else { $SignStatistics = array('brokerId' => $brokerId, 'signCount' => 1, 'maxSignCount' => 1, 'createTime' => date('Y-m-d H:i:s')); Bll_Commsign::setSignStatistics($brokerId, $SignStatistics); $sendFlag = true; //缓存‘签到统计信息’到同步数据库的redis中 $SignStatistics = array_merge($SignStatistics, array('sendFlag' => $sendFlag)); Bll_RedisToDb::writeCommSignStatistics($SignStatistics); } } catch (Exception $e) { $result['message'] = '签到人太多,系统繁忙,请稍后再试~'; return $result; } return array('status' => Const_APIStatus::RETURN_CODE_OK, 'data' => array('signRank' => $rank, 'countDown' => Bll_Broker_CommunitySign::nextSignCountDown())); }