123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- /**
- * MemTokenModel.php UTF-8
- * 玩家Token表
- *
- * @date : 2018/6/26 16:22
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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;
- }
- }
|