* @version : Beibao 1.0 */ namespace huo\model\member; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huolib\constant\DeviceTypeConst; use huolib\constant\MemConst; use huolib\tool\StrUtils; use think\Cache; class MemTokenModel extends CommonModel { protected $name = 'mem_token'; protected $cache_tag = CacheConst::TAG_MEM_TOKEN_DATA; protected $cache_prefix = CacheConst::CACHE_MEM_TOKEN_PREFIX; /** * 头像获取器 * * @param $value * * @return string */ public function getAvatarAttr($value) { if (!empty($value)) { return cmf_get_image_url($value); } return $value; } /** * 关联member表 */ public function member() { return $this->belongsTo('huo\model\member\MemberModel', 'id', 'mem_id')->setEagerlyType(0); } /** * 添加玩家Token * * @param int $mem_id * @param string $device_type * * @return bool|string */ public function addToken($mem_id, $device_type = DeviceTypeConst::DEVICE_TYPE_WAP) { $_data['mem_id'] = $mem_id; $_data['create_time'] = time(); $_data['update_time'] = $_data['create_time']; $_data['expire_time'] = $_data['update_time'] + MemConst::DEFAULT_EXPIRE_TIME; $_data['device_type'] = $device_type; $_data['token'] = StrUtils::genToken($_data['mem_id'].$device_type); if ($_obj = self::create($_data, true)) { $_data['id'] = $_obj->id; $_md_cache_key = $this->cache_prefix.$_data['mem_id'].$_data['device_type']; Cache::set($_md_cache_key, $_data, CommonConst::CONST_DAY_SECONDS); $_token_cache_key = $this->cache_prefix.$_data['token'].$_data['device_type']; Cache::set($_token_cache_key, $_data, CommonConst::CONST_DAY_SECONDS); return $_data['token']; } else { return false; } } /** * 更新玩家Token * * @param array $data * * @param int $id * * @return bool|string */ public function editToken($data = [], $id) { $_map['id'] = $id; $_data = $data; $_data['update_time'] = time(); $_data['expire_time'] = $_data['update_time'] + MemConst::DEFAULT_EXPIRE_TIME; $_data['token'] = StrUtils::genToken($_data['mem_id'].$_data['device_type'].$_data['token']); $_rs = self::update($_data, $_map, true); if (false === $_rs) { return false; } else { $_md_cache_key = $this->cache_prefix.$_data['mem_id'].$_data['device_type']; Cache::set($_md_cache_key, $_data, CommonConst::CONST_DAY_SECONDS); $_token_cache_key = $this->cache_prefix.$_data['token'].$_data['device_type']; Cache::set($_token_cache_key, $_data, CommonConst::CONST_DAY_SECONDS); $_old_token_cache_key = $this->cache_prefix.$data['token'].$_data['device_type']; Cache::set($_old_token_cache_key, $data, CommonConst::CONST_MINUTE_SECONDS); return $_data['token']; } } /** * 通过玩家设备获取Token信息 * * @param int $mem_id * @param string $device_type * * @return bool|array */ public function getTokenInfoByMemDevice($mem_id = 0, $device_type = DeviceTypeConst::DEVICE_TYPE_WAP) { $_map['mem_id'] = $mem_id; $_map['device_type'] = $device_type; $_md_cache_key = $this->cache_prefix.$mem_id.$device_type; $_data = Cache::get($_md_cache_key); if (!empty($_data)) { return $_data; } $_info = $this->where($_map)->find(); if (false === $_info) { return false; } if (is_object($_info)) { $_info = $_info->toArray(); } if (empty($_info)) { return []; } Cache::set($_md_cache_key, $_info, CommonConst::CONST_DAY_SECONDS); $_token_cache_key = $this->cache_prefix.$_info['token'].$device_type; Cache::set($_token_cache_key, $_info, CommonConst::CONST_DAY_SECONDS); return $_info; } /** * 更新玩家Token * * @param string $token * @param string $device_type * * @return bool|array */ public function getTokenInfoByToken($token, $device_type) { $_map['token'] = $token; $_map['device_type'] = $device_type; $_token_cache_key = $this->cache_prefix.$token.$device_type; $_data = Cache::get($_token_cache_key); if (!empty($_data)) { return $_data; } $_info = $this->where($_map)->find(); if (false === $_info) { return false; } if (is_object($_info)) { $_info = $_info->toArray(); } if (empty($_info)) { return []; } Cache::set($_token_cache_key, $_info, CommonConst::CONST_DAY_SECONDS); $_md_cache_key = $this->cache_prefix.$_info['mem_id'].$_info['device_type']; Cache::set($_md_cache_key, $_info, CommonConst::CONST_DAY_SECONDS); return $_info; } /** * 删除玩家id关联的token * * @param $mem_id * * @return bool */ public function deleteToken($mem_id) { $_map = ['mem_id' => $mem_id]; $_list = $this->where($_map)->field('id,mem_id,token,device_type')->select(); if (false == $_list) { return false; } if (empty($_list)) { return true; } if (is_object($_list)) { $_list = $_list->toArray(); } foreach ($_list as $data) { $_map = ['id' => $data['id']]; $_rs = $this->where($_map)->delete(); if (false == $_rs) { return false; } $_token_cache_key = $this->cache_prefix.$data['token'].$data['device_type']; Cache::rm($_token_cache_key); $_md_cache_key = $this->cache_prefix.$data['mem_id'].$data['device_type']; Cache::rm($_md_cache_key); } return true; } }