* @version : HuoMp 1.0 */ namespace huomp\model\game; use huo\model\game\GameextModel; use huo\model\rate\GameRateModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huomp\model\common\CommonModel; use think\Cache; class GameMiniModel extends CommonModel { protected $name = 'game_mini'; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $cache_tag = CacheConst::TAG_CACHE_GAME_MP; protected $cache_key_prefix = CacheConst::CACHE_GAME_MP_PREFIX; /** * 关联game表 * * @return mixed */ public function game() { return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id,name'); } public function joingame() { return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->setEagerlyType(0); } /** * 关联game_ext表 * * @return mixed */ public function gameext() { return $this->belongsTo(GameextModel::className(), 'app_id', 'app_id')->setEagerlyType(0); } /** * 关联game_rate表 * * @return mixed */ public function gamerate() { return $this->belongsTo(GameRateModel::className(), 'app_id', 'app_id')->setEagerlyType(0); } /** * 关联game_version表 * * @return \think\model\relation\HasMany */ public function gv() { return $this->hasMany('huo\model\game\GameversionModel', 'app_id')->field('app_id,package_url'); } /** * 添加数据 * * @param $data * * @return bool */ public function addData($data) { if (empty($data) || empty($data['app_id'])) { return false; } $_data = $data; $_obj = self::create($_data, true); if ($_obj) { return true; } return false; } /** * 更新数据 * * @param array $data 数据 * @param int $id 应用ID * * @return bool */ public function updateData($data, $id) { $_map['app_id'] = $id; $_data = $data; $_rs = $this->where($_map)->update($_data); if (false === $_rs) { return false; } $_cache_key = $this->cache_key_prefix.$_data['app_id']; Cache::tag($this->cache_tag, $_cache_key)->rm($_cache_key); return true; } /** * 获取小程序信息 * * @param int $app_id 应用ID * * @return array|bool|false */ public function getDataByAppId($app_id) { if (empty($app_id)) { return false; } $_cache_key = $this->cache_key_prefix.$app_id; $_data = Cache::tag($this->cache_tag, $_cache_key)->get($_cache_key); if (!empty($_data)) { return $_data; } $_data = $this->where('app_id', $app_id)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } Cache::tag($this->cache_tag, $_cache_key)->set($_cache_key, $_data, CommonConst::CONST_DAY_SECONDS); return $_data; } /** * 获取小程序信息 * * @param string $mini_app_id 小程序ID * * @return array|bool|false */ public function getDataByMpAppId($mini_app_id) { if (empty($mini_app_id)) { return false; } $_app_id = $this->where('mini_app_id', $mini_app_id)->value('app_id'); if (empty($_app_id)) { return false; } return $this->getDataByAppId($_app_id); } /** * 获取小程序ID * * @param int $app_id 应用ID * * @return string|bool|false */ public function getMpIdByAppId($app_id) { if (empty($app_id)) { return false; } $_mini_app_id = false; $_data = $this->getDataByAppId($app_id); if (!empty($_data)) { $_mini_app_id = $_data['mini_app_id']; } return $_mini_app_id; } /** * 获取游戏ID * * @param int $mini_app_id 小程序ID * * @return string|bool|false */ public function getAppIdByMpId($mini_app_id) { if (empty($mini_app_id)) { return false; } $app_id = false; $_data = $this->getDataByMpAppId($mini_app_id); if (!empty($_data)) { $app_id = $_data['app_id']; } return $app_id; } }