* @version : HUOSDK 8.0 */ namespace huo\model\member; use huo\controller\game\GameCache; use huo\model\common\CommonModel; use huo\model\game\GameModel; use huo\model\user\UserModel; use huolib\constant\AccountConst; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huolib\constant\GameConst; use huolib\constant\OrderConst; use huolib\tool\Time; use think\Cache; class MemGameModel extends CommonModel { const IS_DEFAULT_FALSE = 1; const IS_DEFAULT_TRUE = 2; const STATUS_NORMAL = 2; const STATUS_LOCKED = 1; protected $name = 'mem_game'; //设置只读字段 protected $readonly = ['app_id']; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $cache_prefix = CacheConst::CACHE_MEM_GAME_CNT_PREFIX; public function game() { return $this->hasOne('huo\model\game\GameModel', 'id', 'app_id')->setEagerlyType(0); } public function game2() { return $this->hasOne('huo\model\game\GameModel', 'id', 'app_id')->field('id,name,icon,publicity oneword'); } public function gamemini() { return $this->hasOne('huomp\model\game\GameMiniModel', 'app_id', 'app_id')->field( 'app_id,mini_app_id,need_popup,entrance_image' ); } public function gameext() { return $this->hasOne('huo\model\game\GameextModel', 'app_id', 'app_id')->field('app_id,down_cnt'); } public function mem() { return $this->hasOne('huo\model\member\MemberModel', 'id', 'mem_id')->field('id,username'); } public function leftmem() { return $this->belongsTo('huo\model\member\MemberModel', 'mem_id', 'id') ->setEagerlyType(0); } public function pay() { return $this->hasMany('huo\model\order\OrderModel', 'mg_mem_id') ->where(['status' => OrderConst::PAY_STATUS_SUC]) ->field('mg_mem_id,sum(real_amount) sum')->group('mg_mem_id'); } public function paystat() { return $this->hasOne('huo\model\order\OrderModel', 'mg_mem_id') ->where(['status' => OrderConst::PAY_STATUS_SUC]) ->field('mg_mem_id,sum(amount) sum_amount')->group('mg_mem_id'); } /** * 关联game_version表 * * @return \think\model\relation\HasMany */ public function gv() { return $this->hasMany('huo\model\game\GameversionModel', 'app_id')->field('app_id,package_url'); } public function guidedagent() { return $this->belongsTo(UserModel::className(), 'guided_agent_id', 'id') ->field('id,user_login,user_nicename,role_id'); } /** * mg_game登陆 * * @param $mem_data * @param $mem_id * @param $app_id * * @return array|bool|false */ public function login($mem_data, $mem_id, $app_id) { if (empty($app_id)) { return true; } $_map['mem_id'] = $mem_id; $_map['app_id'] = $app_id; $_map['is_default'] = 2; /* 获取关联的游戏共用mg_mem_id */ $_link_app_id = (new GameModel())->getLinkAppId($app_id); $_app_ids = [$app_id]; if (!empty($_link_app_id)) { $_app_ids[] = $_link_app_id; $_game_data = GameCache::ins()->getInfoByAppId($app_id); if ($_game_data['classify'] == GameConst::GAME_H5) { // 管理后台,H5游戏关联主包小游戏 $_same_series_app_ids = (array)(new GameModel())->getIdsByWhere(['apple_id' => $_link_app_id]); $_app_ids = array_merge($_app_ids, $_same_series_app_ids); } } $_map['app_id'] = ['in', $_app_ids]; $_mg_data = $this->where($_map)->order('id ASC')->find(); if (is_object($_mg_data)) { $_mg_data = $_mg_data->toArray(); } if (empty($_mg_data)) { $_guided_agent_id = $mem_data['guided_agent_id'] ?? 0; if (empty($_guided_agent_id)) { $_guided_agent_id = $mem_data['agent_id'] ?? 0; } $_data['mem_id'] = $mem_id; $_data['app_id'] = $app_id; $_data['guided_agent_id'] = $_guided_agent_id; $_data['nickname'] = get_val($mem_data, 'nickname', ''); $_data['create_time'] = time();// 取用当前时间 $_data['update_time'] = $_data['create_time']; $_data['is_default'] = 2; if ($_obj = self::create($_data, true)) { $_data['id'] = $_obj->id; } else { return false; } unset($_map['is_default']); $_key_map = $_map; $_cache_key = $this->cache_prefix.md5(json_encode($_key_map)); Cache::set($_cache_key, 1); $_key_map['update_time'] = $_data['update_time']; $_time_cache_key = $this->cache_prefix.md5(json_encode($_key_map)); list($_start_time, $_end_time) = Time::today(); $_diff_time = $_end_time - time(); Cache::set($_time_cache_key, 1, $_diff_time); $_mg_data = $_data; } else { $_mg_data['mem_id'] = $mem_id; $_mg_data['app_id'] = $app_id; $_mg_data['nickname'] = get_val($mem_data, 'nickname', ''); $_mg_data['update_time'] = time(); $_mg_data['is_default'] = 2; unset($_map['is_default']); $_key_map = $_map; $_key_map['update_time'] = $_mg_data['update_time']; $_time_cache_key = $this->cache_prefix.md5(json_encode($_key_map)); list($_start_time, $_end_time) = Time::today(); $_diff_time = $_end_time - time(); Cache::set($_time_cache_key, 1, $_diff_time); $_rs = self::update($_mg_data); if (false === $_rs) { return false; } } return $_mg_data; } /** * 新增数据 * * @param $data * * @return bool|mixed */ public function addData($data) { $_data = $data; if ($_obj = self::create($_data, true)) { return $_obj->id; } else { return false; } } public function updateData($data, $id) { $_map['id'] = $id; $_data = $data; $_rs = self::update($_data, $_map); if (false === $_rs) { return false; } else { return true; } } /** * 获取玩家玩过的游戏id列表 * * @param $mem_id * * @return array */ public function getAppIds($mem_id) { return $this->where(['mem_id' => $mem_id])->column('app_id'); } /** * 判断游戏是否在有打开过 * * @param int $mem_id * @param int $app_id * @param int $time //大于0 则表示判断该时间以后 * * @return bool true 有登陆 false 没登陆 */ public function hasOpen($mem_id, $app_id, $time = 0) { $_map['mem_id'] = $mem_id; $_map['app_id'] = $app_id; $_cache_time = CommonConst::CONST_DAY_SECONDS; if (!empty($time)) { $_map['update_time'] = ['gt', $time]; list($_start_time, $_end_time) = Time::today(); $_cache_time = $_end_time - time(); } $_cache_key = $this->cache_prefix.md5(json_encode($_map)); $_cnt = Cache::get($_cache_key); if (is_numeric($_cnt)) { if (empty($_cnt)) { return false; } return true; } $_cnt = $this->where($_map)->count(); if (empty($_cnt)) { Cache::set($_cache_key, 0, $_cache_time); return false; } Cache::set($_cache_key, 1, $_cache_time); return true; } /** * 删除打开状态缓存 * * @param $mem_id * @param $app_id * @param int $time */ public function rmHasOpen($mem_id, $app_id, $time = 0) { $_map['mem_id'] = $mem_id; $_map['app_id'] = $app_id; if (!empty($time)) { $_map['update_time'] = ['gt', $time]; } $_cache_key = $this->cache_prefix.md5(json_encode($_map)); Cache::rm($_cache_key); } /** * 获取mg_mem_id * * @param $app_id * @param $mem_id * * @return int|mixed */ public function getMgMemId($app_id, $mem_id) { $_map = ['app_id' => $app_id, 'mem_id' => $mem_id]; $_mg_mem_id = $this->where($_map)->value('id'); if (empty($_mg_mem_id)) { return 0; } return $_mg_mem_id; } public function getInfoByAppMemId($app_id, $mem_id) { $_map = ['app_id' => $app_id, 'mem_id' => $mem_id]; $_data = $this->where($_map)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } return $_data; } public function getMemGameIds($app_ids, $mem_id) { $_map = ['app_id' => ['in', $app_ids], 'mem_id' => $mem_id]; $_mg_mem_id = $this->where($_map)->order('id ASC')->column('id'); if (empty($_mg_mem_id)) { return []; } return $_mg_mem_id; } /** * 通过昵称获取玩家id * * @param $nickname * * @return array */ public function getIdsByNickname($nickname) { $_map = ['nickname' => ['like', $nickname.'%']]; $_ids = $this->where($_map)->column('id'); return $_ids; } public function getNicknameById($id) { $_data = $this->getInfoById($id); return get_val($_data, 'nickname', ''); } /** * 获取默认小号ID * * @param int $mem_id 玩家ID * @param int $app_id 应用ID * * @return int */ public function getDefaultMgIdByMemAppId($mem_id, $app_id) { if (empty($mem_id) || empty($app_id)) { return 0; } /* 取默认 */ $_map = [ 'mem_id' => $mem_id, 'app_id' => $app_id, 'is_default' => CommonConst::CONST_DEFAULT, 'status' => AccountConst::ACCOUNT_STATUS_UNLOCKED ]; /* 获取关联的游戏共用mg_mem_id */ $_link_app_id = (new GameModel())->getLinkAppId($app_id); if (!empty($_link_app_id)) { $_map['app_id'] = ['in', [$app_id, $_link_app_id]]; } $_id = $this->where($_map)->order('update_time desc')->value('id'); if (!empty($_id)) { return $_id; } /* 无默认 取最近登录游戏 */ unset($_map['is_default']); $_id = $this->where($_map)->order('update_time desc')->value('id'); if (empty($_id)) { /* 无正常值 */ return 0; } return $_id; } }