* @version : HUOSDK 8.0 */ namespace huo\controller\member; use huo\controller\common\Base; use huo\logic\member\MemberLogic; use huo\model\member\MemberExtModel; use huo\model\member\MemoauthModel; use huolib\constant\CacheConst; use huolib\constant\MemConst; use think\Cache; class MemCache extends Base { public static function ins() { return new static(); } /** *获取玩家Key * * @param string $mem_id * * @return string */ public function getMemIdKey($mem_id) { return 'mem_key_'.$mem_id; } /** * 获取渠道ID * * @param $mem_id * * @return mixed */ public function getAgentIdByMemId($mem_id) { $_mem_data = MemCache::ins()->getInfoById($mem_id); return $_mem_data['agent_id']; } /** * 获取上级玩家ID * * @param $mem_id * * @return mixed */ public function getParentIdByMemId($mem_id) { $_mem_data = MemCache::ins()->getInfoById($mem_id); return $_mem_data['parent_mem_id']; } /** * 获取账户信息 * * @param string $mem_id * * @return array|bool|mixed */ public function getInfoById($mem_id) { $_key = $this->getMemIdKey($mem_id); Cache::rm($_key); $_mem_data_json = Cache::get($_key); $_mem_data = json_decode($_mem_data_json, true); if (!is_array($_mem_data)) { $_mem_data = $_mem_data_json; } if (!is_array($_mem_data) || empty($_mem_data)) { $_mem_data = (new MemberLogic())->getInfoById($mem_id); if (empty($_mem_data)) { return false; } $this->saveMemCache($mem_id, $_mem_data); } return $_mem_data; } /** * 保存玩家cache 数据 * * @param string $mem_id * @param array $mem_data * @param int $ttl */ public function saveMemCache($mem_id, $mem_data, $ttl = 3600) { $_key = $this->getMemIdKey($mem_id); Cache::set($_key, json_encode($mem_data), $ttl); } /** * 更新帐号信息 * * @param string $mem_id * @param array $mem_data * * @return bool */ public function updateMem($mem_id, $mem_data) { $_key = $this->getMemIdKey($mem_id); Cache::rm($_key); return (new MemberLogic())->updateMem($mem_id, $mem_data); } /** *获取玩家Key * * @param string $type * @param string $open_id * * @return string */ public function getOpenIdKey($type, $open_id) { return CacheConst::CACHE_MEM_OAUTH_OPENID_MEM_PREFIX.$type.'_'.$open_id; } /** * 通过OpenId 获取玩家ID * * * @param string $type * @param string $open_id * * @return string */ public function getMemIdByOpenId($type, $open_id) { $_key = $this->getOpenIdKey($type, $open_id); $_mem_id = Cache::get($_key); if (empty($_mem_id)) { $_mem_id = (new MemoauthModel())->getMemIdByOpenId($type, $open_id); if (empty($_mem_id)) { return false; } $this->saveOpenIdCache($type, $open_id, $_mem_id); } return $_mem_id; } /** * 保存玩家cache 数据 * * @param string $type * @param string $open_id * @param string $mem_id * @param int $ttl * * @return bool */ public function saveOpenIdCache($type, $open_id, $mem_id, $ttl = 3600) { $_key = $this->getOpenIdKey($type, $open_id); return Cache::set($_key, $mem_id, $ttl); } /** * 获取玩家Key * * @param $mem_id * * @return string */ public function getMemExtKey($mem_id) { return 'me_key_'.$mem_id; } /** * 通过OpenId 获取玩家ID * * @param $mem_id * * @return array|bool */ public function getMeInfoById($mem_id) { $_key = $this->getMemExtKey($mem_id); $_me_data_json = Cache::get($_key); $_me_data = json_decode($_me_data_json, true); if (!is_array($_me_data)) { $_me_data = $_me_data_json; } if (!is_array($_me_data) || empty($_me_data)) { $_me_data = (new MemberExtModel())->getExt($mem_id); if (empty($_me_data)) { return false; } $this->saveMeCache($mem_id, $_me_data); } return $_me_data; } /** * 获取玩家积分余额 * * @param int $mem_id 玩家ID * * @return int|mixed */ public function getMyItg($mem_id) { $_me_data = $this->getMeInfoById($mem_id); if (empty($_me_data)) { return 0; } return $_me_data['my_integral']; } /** * 保存玩家cache 数据 * * @param string $mem_id * @param array $me_data * @param int $ttl * * @return bool */ public function saveMeCache($mem_id, $me_data, $ttl = 3600) { $_key = $this->getMemExtKey($mem_id); return Cache::set($_key, json_encode($me_data), $ttl); } /** * 更新玩家扩展信息 * * @param string $mem_id * @param array $me_data * * @param bool $rm_cache 是否强刷缓存 * * @return bool */ public function updateMeCache($mem_id, $me_data, $rm_cache = false) { if (true == $rm_cache) { $_key = $this->getMemExtKey($mem_id); Cache::rm($_key); $_me_data = $me_data; } else { $_old_data = $this->getMeInfoById($mem_id); $_me_data = array_merge($_old_data, $me_data); $this->saveMeCache($mem_id, $_me_data); } $_rs = (new MemberExtModel())->updateExt($_me_data, $mem_id); return $_rs; } /** * 判断是否封禁 * * @param $mem_id * * @return bool */ public function isBan($mem_id) { $_mem_info = $this->getInfoById($mem_id); if ($_mem_info['status'] == MemConst::STATUS_FORBID) { return true; } return false; } }