/** * [开启/关闭免打扰模式] * @param [string] $disturb [1-开启, 3-关闭] * @param [string] $start [开启时间,开启模式下有值] * @param [string] $end [截止时间,开启模式下有值] * @return [type] [description] */ public function disturb($uid, $disturb, $start = 0, $end = 0) { $this->devtagsObj = $this->initModel('\\Appserver\\Mdu\\Models\\DevtagsModel'); //获取当前用户的免打扰模式是开启还是关闭 $status = $this->devtagsObj->checkDisturb($uid); if (!empty($status)) { if ($status['dt_disturb'] == $disturb) { return self::SAME_MODE; } //读取redis $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $oldInfo = $redis->get('disturb:' . $uid); if (!empty($oldInfo) && $disturb == '3') { $start = $oldInfo['start']; $end = $oldInfo['end']; } if ($this->devtagsObj->setDisturb($uid, $disturb, $start, $end) > 0) { //写入redis $redis->set('disturb:' . $uid, array('disturb' => $disturb, 'start' => $start, 'end' => $end)); return self::SUCCESS; } else { return self::FAILED_SET_DISTURB; } } return self::UNTOKEN; }
/** * [用户签到] * @param [type] $uid [用户id] * @param [type] $level [用户当前等级] * @return [type] [description] */ public function userCheckin($uid, $level) { //第二天零点 $tomorrow = strtotime(date('Y-m-d', strtotime('+1 day'))); //第二天连续签到的最后时刻 $finalSign = strtotime(date('Y-m-d', strtotime('+2 day'))) - 1; $nowtime = $_SERVER['REQUEST_TIME']; $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); //用redis设置签到状态值,如果这个这key存在表示今天已经签到,不存在表示尚未签到 $checkinStatus = sprintf($this->di['sysconfig']['checkinStatus'], $uid); if ($redis->get($checkinStatus)) { return self::HAVE_CHECKINED; } //redis签到的key $signKey = sprintf($this->di['sysconfig']['signcount'], $uid); //multi是否处理是否连续签到,连续+1,不连续值为1 $redis->multi()->incr($signKey)->setTimeout($signKey, $finalSign - $nowtime)->exec(); //获得连续签到天数 $seriesCheckin = $redis->get($signKey); //签到奖励的云币数 $coins = Common::checkinCoin($this->di, $seriesCheckin); if ($this->checkinlogs->addCheckin($uid, $nowtime, $coins) == 0) { return self::FAILED_CHECKIN; } //获取用户签到总天数 $totalDays = $redis->get($uid . ':' . $this->di['sysconfig']['checkinTotal']); if ($totalDays == FALSE) { $totalDays = '0'; } //计算用户升级到下一级所需总签到天数 $needDays = UserHelper::levelCheck($level); $swoole = new SwooleUserClient($this->di['sysconfig']['swooleConfig']['ip'], $this->di['sysconfig']['swooleConfig']['port']); if ($totalDays <= 1050) { if ($totalDays > $needDays) { //更新用户等级 $res = $swoole->updateLevel($uid); if ($res['data'] == 0) { return self::FAILED_GET_DATA; } $level = $this->userInfo['level'] + 1; } } //发放奖励 $res = $swoole->checkInReceive($uid, $coins); if ($res['data'] == 1) { //设置签到状态:置1表示已签到 $redis->setex($checkinStatus, $tomorrow - $nowtime, '1'); //用redis保存签到总天数 $redis->set($uid . ':' . $this->di['sysconfig']['checkinTotal'], $totalDays + 1); return array('flag' => '1', 'checkindays' => (string) $seriesCheckin, 'coins' => (string) $coins, 'level' => (string) $level); } else { return self::FAILED_GET_COINS; } }
public function __construct($di, $plat) { $this->di = $di; $this->plat = $plat; //调用swoole $this->swoole = new SwooleUserClient($this->di['sysconfig']['swooleConfig']['ip'], $this->di['sysconfig']['swooleConfig']['port']); //调用第三方类 $this->oauthFactory = UserFactory::operateMethod($this->plat, $this->di); $redisObj = new RedisLib($this->di); $this->redis = $redisObj->getRedis(); }
/** * 1.3版本的宝贝排行榜 */ public function getOldRank($babyId) { $pic = $this->baby->getBabyPic($babyId); if (empty($pic)) { return self::GET_BABY_FAILED; } $redis = RedisLib::getRedis($this->di); $ranksInfo = $redis->get('ranksInfo:' . $babyId); if (!$ranksInfo) { //宝贝总数 $total = $this->babyRanks->countBaby(); $rankInfo = $this->babyRanks->getRankInfo($babyId); $rank = $this->babyRanks->getRank($rankInfo['br_mileages']); $ranks = ceil(($total['br_id'] - $rank['rank']) / $total['br_id'] * 100); //防止出现领先100的情况 if ($ranks >= 100) { $ranks = 99; } $mileage = ceil($rankInfo['br_mileages'] / 1000); $ranksInfo = array('babyPic' => $this->di['sysconfig']['babyPicServer'] . $pic['0']['baby_pic'], 'days' => $rankInfo['br_guards'], 'ranks' => (string) $ranks, 'mileage' => (string) $mileage); //将数据缓存 $redis->setex('ranksInfo:' . $babyId, 300, $ranksInfo); } return array('flag' => self::SUCCESS, 'pic' => $ranksInfo['babyPic'], 'days' => (string) $ranksInfo['days'], 'rank' => $ranksInfo['ranks'], 'mileage' => (string) $ranksInfo['mileage']); }
/** * [初始化方法,获得存在redis的推送队列] * @param [string] $redisKey [取到存在redis的推送目标的key] * @param [string] $redisPushBase [redis的库] */ public function __construct($di, $redisKey, $redisPushBase) { $this->di = $di; $this->AndroidPush = new AndroidPush(); $this->IosPush = new IosPush(); $this->deviceTagsModel = $this->initModel('\\Appserver\\Mdu\\Models\\DevtagsModel'); $this->family = $this->initModel('\\Appserver\\Mdu\\Models\\FamilyModel'); $this->baby = $this->initModel('\\Appserver\\Mdu\\Models\\BabyModel'); $redisObj = new RedisLib($di); $this->redis = $redisObj->getRedis(); $resultRow = $redisObj->queueIntoArray($redisKey, $redisPushBase); //队列值为空,则不进行推送 if (empty($resultRow)) { exit; } //将得到的数组信息,以100为单位分段处理,防止卡死 $this->chunkRes = array_chunk($resultRow, 100); }
/** * [点赞] * @param [type] $token [description] * @param [type] $mobi [description] * @return [type] [description] */ public function hit($token, $locusId) { $redis = RedisLib::getRedis($this->di); $userInfo = $redis->get('token:' . $token); $locusInfo = $this->locusmodel->getLocateInfo($locusId); if (empty($locusInfo)) { return self::NON_LOCUS; } //检查用户是否有权限进行操作 if (!$this->_checkRelation($userInfo['uid'], $locusInfo['baby_id'])) { return self::NO_FAMILY; } $praisesCheck = $this->praisesmodel->getLsidByUid($userInfo['uid'], $locusId); if (!empty($praisesCheck)) { return self::PRAISED; } //获取宝贝昵称 $babyInfo = $this->babymodel->getBabyInfoById($locusInfo['baby_id']); if (empty($babyInfo)) { return self::NON_LOCUS; } $familymodel = $this->initModel('\\Appserver\\Mdu\\Models\\FamilyModel'); //获取角色名 $rolename = $familymodel->getRelationByUidBabyId($userInfo['uid'], $locusInfo['baby_id']); if (!empty($rolename)) { $relName = $rolename['family_rolename']; } else { $relName = $userInfo['mobi']; } $this->di['db']->begin(); if ($this->praisesmodel->hit($userInfo['uid'], $relName, $_SERVER['REQUEST_TIME'], $locusId)) { if ($this->locusmodel->hit($locusId)) { if ($familymodel->hit($userInfo['uid'], $locusInfo['baby_id'], $_SERVER['REQUEST_TIME'])) { $this->di['db']->commit(); //type = 1001到代表赞, type = 1003代表评论 $result = array('rel_id' => (string) $userInfo['uid'], 'type' => '1001', 'addtime' => (string) $_SERVER['REQUEST_TIME'], 'content' => '', 'rel_name' => $relName, 'rel_pic' => $userInfo['pic'], 'baby_id' => (string) $locusInfo['baby_id'], 'locus_id' => (string) $locusId); // $uid是宝贝对应的主号,或者监护号或者是主号和监护号的集合;如果uid为空,则不执行推送的操作 $uid = Common::getPushUid($locusId, $userInfo['uid']); if (!empty($uid)) { for ($i = 0; $i < sizeof($uid); $i++) { if ($uid[$i] != '') { $result['uid'] = $uid[$i]; $result['alert'] = sprintf($this->di['sysconfig']['praPush'], $babyInfo['baby_nick'], $relName, $babyInfo['baby_nick'], date('m月d日', $locusInfo['locus_date'])); $redis->lPush($this->di['sysconfig']['pushForActive'], json_encode($result)); } } } return array('flag' => '1', 'praises' => (string) ($locusInfo['praises'] + 1)); } $this->di['db']->rollback(); return self::FAILED_PRAISE; } $this->di['db']->rollback(); return self::FAILED_PRAISE; } return self::FAILED_PRAISE; }
/** * token验证是否已经登录,如已登录,则返回用户的信息,否则返回错误信息 * @param str $token 获得post请求过来的token * return array|json */ protected function _getToken($token) { $redisObj = new Utils\RedisLib($this->di); $redis = $redisObj->getRedis(); $userInfo = $redis->get('token:' . $token); if ($userInfo) { //如果flag为1,说明正常 if ($userInfo['tokenFlag'] == '1') { //如果token有效,则延长token的有效时间 $redis->setex('token:' . $token, $this->di->get('sysconfig')['tokenTime'], $userInfo); return $userInfo; } else { $this->_showMsg('00009', $this->di->get('flagmsg')['00009']); } } else { $this->_showMsg('00000', $this->di->get('flagmsg')['00000']); } }
/** * [硬件更新] * @return [type] [description] */ public function getHardInfo($time, $uid) { $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $updateData = $redis->get('nearlyHardUp'); if (empty($updateData) || $time < $updateData['time']) { $url = dirname(dirname(dirname(__FILE__))) . '/' . $this->di['sysconfig']['hardInfoUrl']; $files = scandir($url); if (!empty($files)) { foreach ($files as $val) { if ($val !== '.' && $val !== '..') { $res[] = substr($val, 0, strlen($val) - 4); } } $hardUpTime = max($res); //检查该用户是否进行过新版本的推送 if (!$redis->get('userHardUp:' . $uid . ':' . $hardUpTime)) { $redis->del('userHardUp:' . $uid . ':' . $time); $redis->set('userHardUp:' . $uid . ':' . $hardUpTime, 1); $display = '1'; if (!isset($updateData['time']) || $hardUpTime > $updateData['time']) { $logs = file_get_contents($url . '/' . $hardUpTime . '.txt'); $redis->set('nearlyHardUp', array('time' => $hardUpTime, 'logs' => $logs)); } else { $logs = $updateData['logs']; } } else { $logs = ''; $display = '3'; } } else { $hardUpTime = $time; $logs = ''; $display = '3'; } } else { $hardUpTime = $time; $logs = ''; $display = '3'; } return array('flag' => '1', 'logs' => $logs, 'time' => (string) $hardUpTime, 'display' => $display); }
/** * 返回app0.3定位信息 * @param [string] $uid [用户id] * @param [string] $babyId [宝贝id] * @param [string] $starttime [开始时间戳] * @return [type] [description] */ public function locateInfoForV03($uid, $babyId) { $locusInfo = $this->locateModel->locateHaveSteps($babyId, $_SERVER['REQUEST_TIME']); $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $redis->select(14); if (empty($locusInfo) && $redis->get('baby:' . $babyId . ':device-status') >= 2) { $getPoint = new RpcService($this->di['sysconfig']['thriftConf']['ip'], $this->di['sysconfig']['thriftConf']['port']); //从云端获得假点 $pointInfo = json_decode($getPoint->getPoint($babyId), true); if (!empty($pointInfo) && array_key_exists('coordinates', $pointInfo)) { $locusInfo[0] = array('coordinates' => (string) $pointInfo['coordinates'], 'timestamp' => (string) $pointInfo['timestamp'], 'place' => (string) $pointInfo['place'], 'start' => (string) $pointInfo['start'], 'end' => (string) $_SERVER['REQUEST_TIME'], 'steps' => '', 'calory' => ''); } else { $locusInfo = array(); } } if (!empty($locusInfo)) { $num = sizeof($locusInfo); $locusInfo[$num - 1]['end'] = (string) $_SERVER['REQUEST_TIME']; } return $locusInfo; }
/** * 通过redis获取用户信息 */ public function setRedisTasksList($data) { $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); return $redis->lPush($this->di['sysconfig']['countProgress'], $data); }
/** * 删除亲人 * 判断用户对该宝贝是否有操作权限 */ public function delAction() { $this->_oauthrity($this->userInfo['uid'], $this->_sanReq['baby_id']); if (isset($this->_sanReq['fam_id'])) { $rel = $this->family->checkRelation($this->_sanReq['fam_id'], $this->_sanReq['baby_id']); if (!$rel) { $this->_showMsg(self::GET_NIL, $this->di['flagmsg'][self::GET_NIL]); } else { if ($rel['family_relation'] == 1) { $this->_showMsg(self::FORBID_DEL_HOST, $this->di['flagmsg'][self::FORBID_DEL_HOST]); } } //如果亲人已注册,清理数据库 if (($res = $this->family->cancelRel($this->_sanReq['baby_id'], $this->_sanReq['fam_id'], $_SERVER['REQUEST_TIME'])) == self::SUCCESS) { $this->_showMsg($res); } else { $this->_showMsg($res, $this->di['flagmsg'][$res]); } } elseif (isset($this->_sanReq['mobi'])) { //如果亲人未注册,清理缓存 $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $data = $redis->get(sprintf($this->di['sysconfig']['unregUserByBid'], $this->_sanReq['baby_id'])); if (!empty($data)) { unset($data[$this->_sanReq['mobi']]); if (!empty($data)) { $redis->set(sprintf($this->di['sysconfig']['unregUserByBid'], $this->_sanReq['baby_id']), $data); } else { $redis->del(sprintf($this->di['sysconfig']['unregUserByBid'], $this->_sanReq['baby_id'])); } } $userData = $redis->get(sprintf($this->di['sysconfig']['unregUser'], $this->_sanReq['mobi'])); if (!empty($userData)) { unset($userData[$this->_sanReq['baby_id']]); if (!empty($userData)) { $redis->set(sprintf($this->di['sysconfig']['unregUser'], $this->_sanReq['mobi']), $userData); } else { $redis->del(sprintf($this->di['sysconfig']['unregUser'], $this->_sanReq['mobi'])); } } $this->_showMsg(self::SUCCESS); } }
/** * 更新任务进度 */ public function updateprogressAction() { $usertasksmodel = new \Appserver\Mdu\Models\UsertasksModel(); //更新完成步行目标任务的进度 $stepTaskInfo = $usertasksmodel->getUserByGroup($this->di['sysconfig']['taskGroup']['stepGoal']); if (!empty($stepTaskInfo)) { $babystepsmodel = new \Appserver\Mdu\Models\BabyStepsModel(); foreach ($stepTaskInfo as $v) { $maxProgress = $babystepsmodel->getGoalCount($v['u_id'], strtotime(date('Y-m-d', $v['addtime']))); if (!empty($maxProgress)) { $usertasksmodel->editProgress($v['u_id'], $this->di['sysconfig']['taskGroup']['stepGoal'], $maxProgress['count']); } } } $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); //更新围栏签到任务的进度 $fenceTaskInfo = $usertasksmodel->getUserByGroup($this->di['sysconfig']['taskGroup']['fenceCheckin']); if (!empty($fenceTaskInfo)) { $fencesmodel = new \Appserver\Mdu\Models\FencesModel(); foreach ($fenceTaskInfo as $v) { $checkinInfo = $fencesmodel->getBabyCheckinByUid($v['u_id']); $progress = $checkinInfo['fence_checkin'] - intval($redis->get($this->di['sysconfig']['redisFenceCheckin'] . $v['u_id'] . ':' . $v['t_id'])); if (!empty($checkinInfo) && $progress > 0) { $usertasksmodel->editProgress($v['u_id'], $this->di['sysconfig']['taskGroup']['fenceCheckin'], $progress); } } echo '共更新' . sizeof($fenceTaskInfo) . '条任务进度'; } else { echo '没有需要更新的任务进度' . '\\n'; } }
/** * [记录未注册的亲人] * @return [type] [description] */ public function remeberUnregUser($mobi, $babyId, $uname, $relation) { $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $users = $redis->get(sprintf($this->di['sysconfig']['unregUser'], $mobi)); $babys = $redis->get(sprintf($this->di['sysconfig']['unregUserByBid'], $babyId)); //组织要保存的数据 $saveData = array('relation' => $relation, 'uname' => $uname); //根据手机号存储亲人关系 if (empty($users)) { $redis->set(sprintf($this->di['sysconfig']['unregUser'], $mobi), array($babyId => $saveData)); } else { $users = $users + array($babyId => $saveData); $redis->set(sprintf($this->di['sysconfig']['unregUser'], $mobi), $users); } //根据宝贝id存储亲人关系 if (empty($babys)) { $redis->set(sprintf($this->di['sysconfig']['unregUserByBid'], $babyId), array($mobi => $saveData)); } else { $babys = $babys + array($mobi => $saveData); $redis->set(sprintf($this->di['sysconfig']['unregUserByBid'], $babyId), $babys); } }
/** * [返回用户登录数据] * @param [string] $mobi [用户手机号] * @param [string] $deviceToken [用户手机的设备标签] * @param [string] $type [1-ios 3-android] * @return [type] [description] */ protected function _showLoginData($mobi, $deviceToken, $type = '') { //执行免登录操作,将新注册的用户信息存到redis $swoole = new \Appserver\Utils\SwooleUserClient($this->di['sysconfig']['swooleConfig']['ip'], $this->di['sysconfig']['swooleConfig']['port']); $row = $swoole->getUserInfoByMobi($mobi); if (empty($row['data'])) { return self::NON_EXIST_MOBILE; } if (empty($deviceToken)) { $disturb = '1'; } else { //获取用户的免打扰状态 $RedisObj = new \Appserver\Utils\RedisLib($this->di); $redis = $RedisObj::getRedis(); $disturbInfo = $redis->get('disturb:' . $row['data']['u_id']); if (!$disturbInfo) { $disturb = '3'; //默认为3:关闭免打扰 $start = ''; $end = ''; } else { $disturb = $disturbInfo['disturb']; $start = $disturbInfo['start']; $end = $disturbInfo['end']; } } $userInfo['qqUid'] = $row['data']['u_qq_uid']; $userInfo['wbUid'] = $row['data']['u_wb_uid']; $userInfo['uid'] = $row['data']['u_id']; $userInfo['mobi'] = $row['data']['u_mobi']; $userInfo['pic'] = $row['data']['u_pic'] ? UserHelper::checkPic($this->di, $row['data']['u_pic']) : ''; $userInfo['uname'] = $row['data']['u_name']; $userInfo['level'] = $row['data']['u_level']; $userInfo['regtime'] = $row['data']['u_regtime']; //设置tokenflag,表示是正常的token $userInfo['tokenFlag'] = '1'; $userInfo['wb_status'] = $row['data']['u_wb_uid'] ? '1' : ''; $userInfo['qq_status'] = $row['data']['u_qq_uid'] ? '1' : ''; $RedisObj = new RedisLib($this->di); $redis = $RedisObj->getRedis(); //获取用户连续签到次数 $checkinCount = $redis->get(sprintf($this->di['sysconfig']['signcount'], $userInfo['uid'])); if ($checkinCount == false) { $checkinCount = '0'; } //获取用户当前云币数量 $coins = $swoole->coinsInfo($userInfo['uid']); //当发现用户未创建钱包时,未其创建一个 if (empty($coins['data'])) { if ($swoole->createWallet($userInfo['uid'])['data'] == '1') { $coins['data']['uw_coins'] = '0'; } else { return self::FAILED_CREATE_WALLET; } } //保存一个login:uid的redis并产生用户token,用来限制一个用户只能在一台手机上登录 $token = UserHelper::setToken($this->di, $userInfo, $type, $deviceToken); return array('flag' => '1', 'token' => $token, 'u_id' => $userInfo['uid'], 'mobi' => $userInfo['mobi'], 'u_pic' => $userInfo['pic'], 'u_name' => $userInfo['uname'], 'coins' => $coins['data']['uw_coins'], 'level' => $userInfo['level'], 'checkindays' => (string) $checkinCount, 'wb_status' => $userInfo['wb_status'], 'qq_status' => $userInfo['qq_status'], 'user_qr' => UserHelper::makeUserQr($userInfo['uid'], $userInfo['mobi'], $userInfo['regtime']), 'disturb' => $disturb); }
/** * 监护号设置 * @param str $code : $code = set 表所设置监护号;$code = del 表示取消监护号 * 判断用户对该宝贝是否有操作权限 */ public function guaAction($code = '') { $relation = $this->family->checkRelation($this->userInfo['uid'], $this->_sanReq['baby_id']); if ($relation['family_relation'] != '1') { $this->_showMsg(self::NO_OAUTH, $this->di['flagmsg'][self::NO_OAUTH]); } //判断宝贝与要添加的亲人关系,如果必须是副号才允许设置为监护人 $famRel = $this->family->checkRelation($this->_sanReq['fam_id'], $this->_sanReq['baby_id']); if ($code == 'set') { if (($res = $this->family->issetHost($this->_sanReq['baby_id'])) != self::SUCCESS) { $this->_showMsg($res, $this->di['flagmsg'][$res]); } if ($famRel['family_relation'] == '3') { if ($this->family->guardian(5, $this->_sanReq['baby_id'], $this->_sanReq['fam_id']) == '1') { $babyInfo = $this->baby->getBabyName($this->_sanReq['baby_id']); if (!$babyInfo) { $this->_showMsg(self::NOT_BABY_OWN, $this->di['flagmsg'][self::NOT_BABY_OWN]); } //组装推送数据 //推送alert $content = sprintf($this->di['sysconfig']['setGuaMsg'], $famRel['family_rolename'], $babyInfo['baby_nick']); //推送内容 $data = array('baby_id' => (string) $this->_sanReq['baby_id'], 'nick' => $babyInfo['baby_nick'], 'baby_pic' => $this->di['sysconfig']['babyPicServer'] . $babyInfo['baby_pic'], 'nearly' => $babyInfo['baby_nearly'], 'nearlyTime' => (string) $babyInfo['baby_nearlytime'], 'devs' => (string) $babyInfo['baby_devs'], 'sex' => $babyInfo['baby_sex'], 'relation' => (string) '5', 'birthday' => (string) $babyInfo['baby_birthday'], 'battery' => (string) $babyInfo['baby_nearbattery']); $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $redis->lPush($this->di['sysconfig']['addfamily'], json_encode(array('uid' => $this->_sanReq['fam_id'], 'content' => $content, 'data' => $data, 'type' => '19'))); $this->_returnResult(array('flag' => self::SUCCESS)); } else { $this->_showMsg(self::FAILED_SET_GUARDIAN, $this->di['flagmsg'][self::FAILED_SET_GUARDIAN]); } } else { $this->_showMsg(self::IS_HOST, $this->di['flagmsg'][self::IS_HOST]); } } elseif ($code == 'del') { if ($famRel['family_relation'] == '5') { if ($this->family->guardian(3, $this->_sanReq['baby_id'], $this->_sanReq['fam_id']) == '1') { $this->_returnResult(array('flag' => self::SUCCESS)); } else { $this->_showMsg(self::FAILED_UNSET_GUARDIAN, $this->di['flagmsg'][self::FAILED_UNSET_GUARDIAN]); } } else { $this->_showMsg(self::IS_NOT_HOST, $this->di['flagmsg'][self::IS_NOT_HOST]); } } else { $this->_showMsg(self::INVALID_OPERATE, $this->di['flagmsg']['self::INVALID_OPERATE']); } }
/** * [添加评论] * @param [string`] $token [用户登录token] * @param [string`] $locusId [轨迹id] * @param [string`] $content [评论内容] * @param [string`] $replyUid [被回复人id] */ public function add($token, $locusId, $content, $replyUid) { //调用swoole $swoole = new SwooleUserClient($this->di['sysconfig']['swooleConfig']['ip'], $this->di['sysconfig']['swooleConfig']['port']); //获取轨迹对应的宝贝id $locusInfo = $this->locusModel->getLocateInfo($locusId); if (empty($locusInfo)) { return self::NON_LOCUS; } $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $userInfo = $redis->get('token:' . $token); //获取评论人关系和角色名 $rel = $this->familyModel->getRelationByUidBabyId($userInfo['uid'], $locusInfo['baby_id']); if (empty($rel)) { return self::NO_OAUTH; } $uname = $rel['family_rolename'] == '' ? $userInfo['mobi'] : $rel['family_rolename']; //获取宝贝信息 $babyInfo = $this->babyModel->getBabyInfoById($locusInfo['baby_id']); if (empty($babyInfo)) { return self::NON_LOCUS; } //被回复人姓名,当不是评论时,该字段为空 $lcName = ''; //如果被回复人id有值,则说明这是一条回复 if (!empty($replyUid)) { //获取被回复人的称呼,如果没有则取昵称,如果没有昵称则获取手机号 $replyRelInfo = $this->familyModel->getRelationByUidBabyId($replyUid, $locusInfo['baby_id']); if ($replyRelInfo['family_rolename'] == '') { $replyUserInfo = $swoole->userInfoByIds($replyUid); if (empty($replyUserInfo['data'])) { return self::NON_REPLY_USER; } $lcName = $replyUserInfo['data']['u_name'] ? $replyUserInfo['data']['u_name'] : $replyUserInfo['u_mobi']; } else { $lcName = $replyRelInfo['family_rolename']; } } $this->di['db']->begin(); if ($commId = $this->commentModel->addComment($userInfo['uid'], $uname, $locusId, $content, $_SERVER['REQUEST_TIME'], $replyUid, $lcName)) { //评论发表后,更新轨迹表里的评论总数 if (!$this->locusModel->setCommentNum($locusId)) { $this->di['db']->rollback(); return self::FAILED_COMMENT; } //评论发表后,更新宝贝关系表里的评论总数 if (!$this->familyModel->incCommentNum($userInfo['uid'], $locusInfo['baby_id'], $_SERVER['REQUEST_TIME'])) { $this->di['db']->rollback(); return self::FAILED_COMMENT; } $this->di['db']->commit(); //计算评论成功后评论的数量,返回 $count = $locusInfo['comments'] + 1; //type = 1001到代表赞, type = 1003代表评论 $result = array('rel_id' => (string) $userInfo['uid'], 'type' => '1003', 'addtime' => (string) $_SERVER['REQUEST_TIME'], 'content' => $content, 'rel_name' => $uname, 'rel_pic' => $userInfo['pic'], 'baby_id' => (string) $locusInfo['baby_id'], 'locus_id' => (string) $locusId); //推送给$uid: $uid是宝贝对应的主号,或者监护号或者是主号和监护号的集合,如果uid为空数组,则不用执行推送的操作 $uid = $this->getPushUid($locusId, $userInfo['uid']); if (!empty($uid)) { $num = sizeof($uid); $redis = RedisLib::getRedis(); for ($i = 0; $i < $num; $i++) { if ($uid[$i] != '') { $result['uid'] = $uid[$i]; $result['alert'] = sprintf($this->di['sysconfig']['commentPush'], $babyInfo['baby_nick'], $uname, $babyInfo['baby_nick'], date('m月d日', $locusInfo['locus_date'])); $redis->lPush($this->di['sysconfig']['pushForActive'], json_encode($result)); } } } if (!empty($replyUid)) { //推送:如果是回复,而且回复给副号,则也要推送给对应的副号 $rel = $this->familyModel->checkRelation($replyUid, $locusInfo['baby_id']); if (!empty($rel)) { if ($rel['family_relation'] == 3) { $result['uid'] = $replyUid; $result['alert'] = $this->di['sysconfig']['replyPush']; $redis->lPush($this->di['sysconfig']['pushForActive'], $result); } } } } else { return self::FAILED_COMMENT; } return array('flag' => '1', 'comments' => (string) $count, 'lc_id' => $commId, 'addtime' => (string) $_SERVER['REQUEST_TIME']); }
/** * [获取轨迹消息列表] * @param [string] $uid [用户id] * @param [string] $babyId [宝贝id] * @return [type] [description] */ public function getMessList($uid, $babyId) { $redis = RedisLib::getRedis(); //获取对应的轨迹消息提示列表的key $key = sprintf($this->di['sysconfig']['tracksMsg'], $uid, $babyId); $num = $redis->lSize($key); $locusList = array(); for ($i = 0; $i < $num; $i++) { $locusList[$i] = $redis->rPop($key); } $redis->del($key); return array('flag' => '1', 'messagelist' => $locusList); }
/** * [获取天气预报信息] * @param [type] $babyId [description] * @param [type] $city [description] * @return [type] [description] */ public function getFore($babyId, $city) { $babyInfo = $this->baby->getBabyName($babyId); try { $rpcConnect = new \Appserver\Utils\RpcService($this->di['sysconfig']['thriftConf']['ip'], $this->di['sysconfig']['thriftConf']['port']); $location = $rpcConnect->GetBabyLocation($babyId); } catch (\Exception $e) { continue; } //如果从babyid找不到地理位置,则根据请求的地理位置计算天气 if (!empty($location)) { $location = json_decode($location, true); $city = $location['city']; } if ($city != '') { //获取地区编码 $areaid = UserHelper::getAreaCode($this->di, $city); if (!$areaid) { $city = substr($city, 0, -3); $areaid = UserHelper::getAreaCode($this->di, $city); } } if ($city == '' || !$areaid) { return array('flag' => '1', 'area' => $city, 'aqi' => '', 'qlt' => '', 'h_temp' => '', 'l_temp' => '', 'weat_pic' => '', 'weather' => '', 'baby_pic' => ''); } $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $weatherData = $redis->get($this->di['weatherConfig']['weatherData'] . $areaid); if (empty($weatherData)) { //缓存过期,重新获取数据 $data = UserHelper::rollCurlRequest(array('aqi' => sprintf($this->di['weatherConfig']['pm25']['url'], urlencode($city)), 'fore' => sprintf($this->di['weatherConfig']['weatherConfig']['url'], urlencode($city)))); if (empty($data) || !empty($data[0]['error']) || !empty($data[1]['error'])) { $flag = 0; $log = '查询天气:' . $city . '>>结果:aqi:' . $data[0]['error'] . '>>天气:' . $data[1]['error']; } else { $weather = json_decode($data['fore']['info'], true); $aqi = json_decode($data['aqi']['info'], true); if ($weather['error_code'] != '0' || $aqi['error_code'] != '0') { $flag = 0; } $log = '查询天气:' . $city . '>>结果:aqi:' . $aqi['reason'] . '>>天气:' . $weather['reason']; //构造天气数据 if ($weather['error_code'] == 0) { $temp = explode('~', $weather['result']['today']['temperature']); $ltemp = (string) $temp[0]; $htemp = (string) $temp[1]; //气象实况 $weath = $weather['result']['today']['fb']; $forecast = $weather['result']['today']['weather']; } else { $ltemp = ''; $htemp = ''; //气象实况,99-未知天气 $weath = '99'; $forecast = ''; $flag = 0; $log = '查询天气:' . $city . '>>结果:' . $weather['reason']; } //构造aqi数据 if ($aqi['error_code'] == 0) { $pmAqi = (string) $aqi['result']['AQI']; $qlt = (string) $aqi['result']['Quality']; } else { $pmAqi = ''; $qlt = ''; $flag = 0; $log = '查询aqi:' . $city . '>>结果:aqi:' . $aqi['reason']; } $weatherData['area'] = $city; $weatherData['aqi'] = $pmAqi; $weatherData['qlt'] = $qlt; $weatherData['h_temp'] = $htemp; $weatherData['l_temp'] = $ltemp; $weatherData['weather'] = $forecast; $weatherData['weatherCode'] = $weath; //存入缓存:如果数据不完整,则缓存半小时,若数据完整,则缓存8小时 if (empty($weatherData['weather'])) { $redis->setex($this->di['weatherConfig']['weatherData'] . $areaid, $this->di['weatherConfig']['weatherShortTime'], $weatherData); } else { $redis->setex($this->di['weatherConfig']['weatherData'] . $areaid, $this->di['weatherConfig']['weatherTime'], $weatherData); } } } if (!empty($weatherData['weatherCode']) && !empty($weatherData['aqi']) && (!empty($weatherData['l_temp']) || !empty($weatherData['h_temp']))) { $res = UserHelper::getBabyWeatherPic($this->di, $babyInfo['baby_sex'], $weatherData['weatherCode'], $weatherData['aqi'], $weatherData['l_temp'], $weatherData['h_temp']); $weatherData['baby_pic'] = $res['baby_pic']; $weatherData['weat_pic'] = $res['weat_pic']; } else { $weatherData['baby_pic'] = ''; $weatherData['weat_pic'] = ''; } unset($weatherData['weatherCode']); $weatherData['flag'] = '1'; return $weatherData; }
/** * 通过redis获取用户信息 */ private function checkFromRedis($token) { $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); return $redis->get('token:' . $token); }
/** * 使用验证码,验证码失效返回提示信息,验证码可用则激活验证 * @param [string] $mobi [手机号] * @param [string] $type [验证码类型:1-注册 3-添加亲人 7-找回密码 9-修改密码 11-第三方绑定] * @param [string] $captcha [验证码] * @param [string] $useTime [验证码使用时间] * @return [type] [description] */ public function checkCaptcha($mobi, $type, $captcha, $useTime) { //限制验证次数 $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $redisKey = sprintf($this->di['sysconfig']['checkCaptcha']['redisKey'], $mobi); //获取用户验证次数 $checkCount = $redis->get($redisKey); if (!$checkCount || $checkCount < $this->di['sysconfig']['checkCaptcha']['count']) { $redis->multi()->incr($redisKey)->setTimeout($redisKey, $this->di['sysconfig']['checkCaptcha']['ttl'])->exec(); } elseif ($checkCount >= $this->di['sysconfig']['checkCaptcha']['count']) { return self::GET_TOO_CAPTCHA; } $userInfo = $this->userSwoole->getUserInfoByMobi($mobi); if (($type == '1' || $type == '11') && !empty($userInfo['data'])) { return self::EXIST_MOBILE; } if (($type == '3' || $type == '7' || $type == '9') && empty($userInfo['data'])) { return self::NON_EXIST_MOBILE; } //获取验证码是否正确,以及是否被使用 $captchaInfo = $this->captcha->getCapthaTime($mobi, $type, $captcha); if (empty($captchaInfo)) { return self::ERROR_CAPTCHA; } elseif ($captchaInfo['mc_validtime'] != 0) { return self::INVALID_CAPTCHA; } if ($useTime - $captchaInfo['mc_addtime'] > 0 && $useTime - $captchaInfo['mc_addtime'] <= $this->di['sysconfig']['capTime']) { if ($this->captcha->updateCaptcha($mobi, $captcha, $type, $useTime) == FALSE) { $this->showMsg('33333', $this->config->item('flagMsg')['33333']); } return self::SUCCESS; } else { return self::INVALID_CAPTCHA; } }
/** * [检查刚注册的用户的状态] * 发现该用户在注册前已经被添加为亲人时,完成与对应宝贝的关系绑定 * @return [type] [description] */ public function checkNewUser($uid, $mobi) { $redisObj = new RedisLib($this->di); $redis = $redisObj->getRedis(); $babyList = $redis->get(sprintf($this->di['sysconfig']['unregUser'], $mobi)); if (!empty($babyList)) { $query = ''; foreach ($babyList as $babyId => $val) { $bids[] = $babyId; $query .= '(' . $babyId . ',' . $uid . ',"' . $val['uname'] . '",' . $val['relation'] . ',' . $_SERVER['REQUEST_TIME'] . ',' . 1 . '),'; } $family = $this->initModel('\\Appserver\\Mdu\\Models\\FamilyModel'); $family->batchInsert(rtrim($query, ',')); //该用户注册完毕,删除该用户之前未注册的缓存信息 $redis->del(sprintf($this->di['sysconfig']['unregUser'], $mobi)); //从宝贝列表的缓存中去掉对应的未注册用户信息 foreach ($bids as $babyId) { $data = $redis->get(sprintf($this->di['sysconfig']['unregUserByBid'], $babyId)); unset($data[$mobi]); if (empty($data)) { $redis->del(sprintf($this->di['sysconfig']['unregUserByBid'], $babyId)); } else { $redis->set(sprintf($this->di['sysconfig']['unregUserByBid'], $babyId), $data); } } } return true; }