GameMiniModel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * GameMiniModel.php UTF-8
  4. * 小程序应用
  5. *
  6. * @date : 2018/8/9 16:54
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\model\game;
  13. use huo\model\game\GameextModel;
  14. use huo\model\rate\GameRateModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use huomp\model\common\CommonModel;
  18. use think\Cache;
  19. class GameMiniModel extends CommonModel {
  20. protected $name = 'game_mini';
  21. // 开启自动写入时间戳字段
  22. protected $autoWriteTimestamp = true;
  23. protected $cache_tag = CacheConst::TAG_CACHE_GAME_MP;
  24. protected $cache_key_prefix = CacheConst::CACHE_GAME_MP_PREFIX;
  25. /**
  26. * 关联game表
  27. *
  28. * @return mixed
  29. */
  30. public function game() {
  31. return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id,name');
  32. }
  33. public function joingame() {
  34. return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->setEagerlyType(0);
  35. }
  36. /**
  37. * 关联game_ext表
  38. *
  39. * @return mixed
  40. */
  41. public function gameext() {
  42. return $this->belongsTo(GameextModel::className(), 'app_id', 'app_id')->setEagerlyType(0);
  43. }
  44. /**
  45. * 关联game_rate表
  46. *
  47. * @return mixed
  48. */
  49. public function gamerate() {
  50. return $this->belongsTo(GameRateModel::className(), 'app_id', 'app_id')->setEagerlyType(0);
  51. }
  52. /**
  53. * 关联game_version表
  54. *
  55. * @return \think\model\relation\HasMany
  56. */
  57. public function gv() {
  58. return $this->hasMany('huo\model\game\GameversionModel', 'app_id')->field('app_id,package_url');
  59. }
  60. /**
  61. * 添加数据
  62. *
  63. * @param $data
  64. *
  65. * @return bool
  66. */
  67. public function addData($data) {
  68. if (empty($data) || empty($data['app_id'])) {
  69. return false;
  70. }
  71. $_data = $data;
  72. $_obj = self::create($_data, true);
  73. if ($_obj) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. /**
  79. * 更新数据
  80. *
  81. * @param array $data 数据
  82. * @param int $id 应用ID
  83. *
  84. * @return bool
  85. */
  86. public function updateData($data, $id) {
  87. $_map['app_id'] = $id;
  88. $_data = $data;
  89. $_rs = $this->where($_map)->update($_data);
  90. if (false === $_rs) {
  91. return false;
  92. }
  93. $_cache_key = $this->cache_key_prefix.$_data['app_id'];
  94. Cache::tag($this->cache_tag, $_cache_key)->rm($_cache_key);
  95. return true;
  96. }
  97. /**
  98. * 获取小程序信息
  99. *
  100. * @param int $app_id 应用ID
  101. *
  102. * @return array|bool|false
  103. */
  104. public function getDataByAppId($app_id) {
  105. if (empty($app_id)) {
  106. return false;
  107. }
  108. $_cache_key = $this->cache_key_prefix.$app_id;
  109. $_data = Cache::tag($this->cache_tag, $_cache_key)->get($_cache_key);
  110. if (!empty($_data)) {
  111. return $_data;
  112. }
  113. $_data = $this->where('app_id', $app_id)->find();
  114. if (is_object($_data)) {
  115. $_data = $_data->toArray();
  116. }
  117. Cache::tag($this->cache_tag, $_cache_key)->set($_cache_key, $_data, CommonConst::CONST_DAY_SECONDS);
  118. return $_data;
  119. }
  120. /**
  121. * 获取小程序信息
  122. *
  123. * @param string $mini_app_id 小程序ID
  124. *
  125. * @return array|bool|false
  126. */
  127. public function getDataByMpAppId($mini_app_id) {
  128. if (empty($mini_app_id)) {
  129. return false;
  130. }
  131. $_app_id = $this->where('mini_app_id', $mini_app_id)->value('app_id');
  132. if (empty($_app_id)) {
  133. return false;
  134. }
  135. return $this->getDataByAppId($_app_id);
  136. }
  137. /**
  138. * 获取小程序ID
  139. *
  140. * @param int $app_id 应用ID
  141. *
  142. * @return string|bool|false
  143. */
  144. public function getMpIdByAppId($app_id) {
  145. if (empty($app_id)) {
  146. return false;
  147. }
  148. $_mini_app_id = false;
  149. $_data = $this->getDataByAppId($app_id);
  150. if (!empty($_data)) {
  151. $_mini_app_id = $_data['mini_app_id'];
  152. }
  153. return $_mini_app_id;
  154. }
  155. /**
  156. * 获取游戏ID
  157. *
  158. * @param int $mini_app_id 小程序ID
  159. *
  160. * @return string|bool|false
  161. */
  162. public function getAppIdByMpId($mini_app_id) {
  163. if (empty($mini_app_id)) {
  164. return false;
  165. }
  166. $app_id = false;
  167. $_data = $this->getDataByMpAppId($mini_app_id);
  168. if (!empty($_data)) {
  169. $app_id = $_data['app_id'];
  170. }
  171. return $app_id;
  172. }
  173. }