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())); }
public static function testRedis() { $redis = self::getRedisConnect(); //测试程序///////////////////////////////////////////////// $currentSignRange = Bll_Broker_CommunitySign::getCurrentSignRange(); $redis->flushDB(); $SignStatistics = Model_Broker_CommunitySignStatistics::data_access()->filter_by_op('id', '>=', 0)->limit(500)->get_all(); //var_dump($SignStatistics);die; $redis->select(1); $arr = $redis->keys(self::$SignStatistics . '*'); foreach ($arr as $k) { //var_dump(self::getKey($k)); } $redis->select(0); exit; $id = 100; for ($i = 1; $i <= 10; ++$i) { $brokerId = intval('1111' . $i); $commId = intval('200' . $i); $id = $id + 1; $sign = array('id' => $id, 'brokerId' => $brokerId, 'communityId' => $commId, 'lat' => floatval('31.' . $i), 'lng' => floatval('121.' . $i), 'signTime' => date('Y-m-d H:i:s'), 'sourceType' => Model_Broker_Location::SOURCE_TYPE_COMMUNITY_SIGN, 'updateTime' => date('Y-m-d H:i:s'), 'currentSignRange' => $currentSignRange[0]); Bll_Commsign::Signed($brokerId, $commId, $currentSignRange[0], $sign); //Bll_Commsign::setLocationInfo($brokerId,$sign); } //var_dump($redis->keys('*'));exit; //测试结束////////////////////////////////////////////////////////////////// }