* @version : HUOSDK 8.0 */ namespace huo\model\member; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huolib\constant\MemConst; use huolib\constant\MpConfConst; use huolib\constant\OauthConst; use huomp\model\weixin\MpConfModel; use think\Cache; class MemoauthModel extends CommonModel { protected $name = 'mem_oauth'; //设置只读字段 //protected $readonly = ['from', 'openid', 'create_time']; protected $readonly = ['from', 'create_time']; //重置玩家需要修改openid chenbingling 20181026 // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $cache_key_prefix = CacheConst::CACHE_MEM_OAUTH_MEM_PREFIX; /** * @param array $data * * @return bool|mixed */ public function addOauth($data = []) { $_data['from'] = get_val($data, 'from', ''); $_data['conf_id'] = get_val($data, 'conf_id', '0'); $_data['openid'] = get_val($data, 'openid', ''); $_data['access_token'] = get_val($data, 'access_token', ''); $_data['unionid'] = get_val($data, 'unionid', ''); $_data['nickname'] = get_val($data, 'nickname', ''); $_data['gender'] = get_val($data, 'gender', MemConst::GENDER_N); $_data['avatar'] = get_val($data, 'avatar', ''); $_data['mem_id'] = get_val($data, 'mem_id', 0); $_data['last_login_time'] = time(); $_data['last_login_ip'] = get_client_ip(0, true); $_data['status'] = get_val($data, 'status', MemConst::STATUS_NORMAL); $_data['expires_in'] = get_val($data, 'expires_in', 0); $_data['more'] = get_val($data, 'more', ''); if ($_obj = self::create($_data, true)) { return $_obj->id; } else { return false; } } /** * 更新Oauth信息 * * @param $from * @param $open_id * @param $oauth_data * * @return bool */ public function updateOauth($from, $open_id, $oauth_data) { if (empty($from) || empty($open_id)) { return false; } $_oauth_data = $this->getInfoByOpenId($from, $open_id); $_map['from'] = $from; $_map['openid'] = $open_id; $_rs = self::update($oauth_data, $_map, true); if (false === $_rs) { return false; } $_oauth_data = array_merge($_oauth_data, $oauth_data); $_cache_conf_key = $this->cache_key_prefix.$_oauth_data['conf_id'].$_oauth_data['mem_id']; Cache::set($_cache_conf_key, $_oauth_data, CommonConst::CONST_DAY_SECONDS); $_cache_key = $this->cache_key_prefix.$open_id.$from; Cache::set($_cache_key, $_oauth_data, CommonConst::CONST_DAY_SECONDS); return true; } /** * 通过配置ID与玩家ID获取数据 * * @param int $conf_id 配置ID * @param int $mem_id 玩家ID * * @return array|bool| */ public function getDataByMpMemId($conf_id, $mem_id) { if (empty($conf_id) || empty($mem_id)) { return false; } $_cache_key = $this->cache_key_prefix.$conf_id.$mem_id; $_data = Cache::get($_cache_key); if (!empty($_data)) { return $_data; } $_map['conf_id'] = $conf_id; $_map['mem_id'] = $mem_id; $_data = $this->where($_map)->find(); if (false == $_data) { return false; } if (is_object($_data)) { $_data = $_data->toArray(); } if (!empty($_data)) { Cache::set($_cache_key, $_data, CommonConst::CONST_DAY_SECONDS); } return $_data; } /** * 通过配置ID与玩家ID获取Open_id * * @param int $conf_id 配置ID * @param int $mem_id 玩家ID * * @return bool|mixed */ public function getOpenIdByMpMemId($conf_id, $mem_id) { $_data = $this->getDataByMpMemId($conf_id, $mem_id); if (empty($_data)) { return false; } return $_data['openid']; } /** * 通过open_id 获取信息 * * @param string $type * @param string $open_id * * @return array|false */ public function getInfoByOpenId($type, $open_id) { $_cache_key = $this->cache_key_prefix.$open_id.$type; $_data = Cache::get($_cache_key); if (!empty($_data)) { return $_data; } $_map['from'] = $type; $_map['openid'] = $open_id; $_data = $this->where($_map)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } if (empty($_data)) { return false; } if (!empty($_data)) { Cache::set($_cache_key, $_data, CommonConst::CONST_DAY_SECONDS); } return $_data; } /** * @param $type * @param $open_id * * @return bool|int|mixed */ public function getMemIdByOpenId($type, $open_id) { $_data = $this->getInfoByOpenId($type, $open_id); if (empty($_data)) { return 0; } return $_data['mem_id']; } /** * @param $type * @param $union_id * * @return bool|int|mixed */ public function getMemIdByUnionId($type, $union_id) { $_wx_type_array = [OauthConst::OAUTH_MP, OauthConst::OAUTH_WEIXIN, OauthConst::OAUTH_WXQRCODE]; if (in_array($type, $_wx_type_array)) { $_map['from'] = ['in', $_wx_type_array]; } else { $_map['from'] = $type; } $_map['unionid'] = $union_id; $_map['mem_id'] = ['gt', 0]; $_mem_id = $this->where($_map)->value('mem_id'); if (false === $_mem_id) { return false; } if (empty($_mem_id)) { return 0; } return $_mem_id; } /** * @param $type * @param $mem_id * * @return bool|int|mixed */ public function getOpenidByMemId($type, $mem_id, $conf_id = 0) { if (!empty($type)) { $_map['from'] = $type; } if (!empty($conf_id)) { $_map['conf_id'] = $conf_id; } $_map['mem_id'] = $mem_id; $_open_id = $this->where($_map)->value('openid'); if (false === $_open_id) { return false; } if (empty($_open_id)) { return 0; } return $_open_id; } /** * 获取第三方数据 * * @param $conf_id * @param $mem_id * * @return array|bool|false */ public function getInfoByConfId($conf_id, $mem_id) { $_map['conf_id'] = $conf_id; $_map['mem_id'] = $mem_id; $_data = $this->where($_map)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } if (empty($_data)) { return false; } return $_data; } /** * 根据应用于玩家ID获取信息 * * @param $app_id * @param $mem_id * * @return array|bool */ public function getInfoByAppMemId($app_id, $mem_id) { $_type = MpConfConst::MP_CONF_TYPE_6; $_conf_id = (new MpConfModel())->getIdByAppId($app_id, $_type); if (empty($_conf_id)) { return []; } return $this->getDataByMpMemId($_conf_id, $mem_id); } /** * 删除缓存 * * @param $conf_id * @param $open_id * @param $mem_id * @param $from */ public function clearCache($conf_id, $open_id, $mem_id, $from) { $_cache_conf_key = $this->cache_key_prefix.$conf_id.$mem_id; Cache::rm($_cache_conf_key); $_cache_key = $this->cache_key_prefix.$open_id.$from; Cache::rm($_cache_key); $_key = CacheConst::CACHE_MEM_OAUTH_OPENID_MEM_PREFIX.$from.'_'.$open_id; Cache::rm($_key); } /** * 根据微信ID与玩家ID获取信息 * * @param $mp_id * @param $mem_id * * @return array|bool */ public function getInfoByMpMemId($mp_id, $mem_id) { $_conf_id = (new MpConfModel())->getIdByMpId($mp_id); if (empty($_conf_id)) { return []; } return $this->getDataByMpMemId($_conf_id, $mem_id); } /** * @param $type * @param $mem_id * * @return bool|int|mixed */ public function getConfidByMemId($type, $mem_id) { if (!empty($type)) { $_map['from'] = $type; } $_map['mem_id'] = $mem_id; $_conf_id = $this->where($_map)->value('conf_id'); if (false === $_conf_id) { return false; } if (empty($_conf_id)) { return 0; } return $_conf_id; } /** * @param $type * @param $mem_id * * @return bool|int|mixed */ public function getTypeByMemId($mem_id) { $_map['mem_id'] = $mem_id; $_from = $this->where($_map)->value('from'); if (false === $_from) { return false; } if (empty($_from)) { return 0; } return $_from; } /** * 左联玩家表 */ public function ljmem() { return $this->belongsTo('huo\model\member\MemberModel', 'mem_id', 'id', [], 'left')->setEagerlyType(0); } }