123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- /**
- * GameMiniModel.php UTF-8
- * 小程序应用
- *
- * @date : 2018/8/9 16:54
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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;
- }
- }
|