GameversionModel.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * GameversionModel.php UTF-8
  4. * 游戏版本表
  5. *
  6. * @date : 2017/11/23 15:20
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\game;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use think\Cache;
  17. class GameversionModel extends CommonModel {
  18. protected $name = 'game_version';
  19. protected $insert = ['version_key', 'version', 'content'];
  20. // 开启自动写入时间戳字段
  21. protected $autoWriteTimestamp = true;
  22. protected $cache_tag = 'game_version_key';
  23. protected $cache_key_prefix = CacheConst::CACHE_GAME_VERSION_PREFIX;
  24. /**
  25. * 基础查询
  26. *
  27. * @param $query
  28. */
  29. protected function base($query) {
  30. $query->where('delete_time', 0)->where('is_default', 2);
  31. }
  32. protected function game() {
  33. return $this->belongsTo('huo\model\game\GameModel', 'app_id');
  34. }
  35. public function gamex() {
  36. return $this->belongsTo('huo\model\game\GameModel', 'app_id')->setEagerlyType(0);
  37. }
  38. // 小程序不需要 2018-09-20 chenbingling
  39. // public function getPackageUrlAttr($value) {
  40. // if (!empty($value)) {
  41. // return huo_get_down_url($value);
  42. // }
  43. //
  44. // return $value;
  45. // }
  46. public function getSizeAttr($value) {
  47. return huo_format_file_size($value);
  48. }
  49. public function setVersionKeyAttr($value, $data) {
  50. return md5($data['app_id'].time().uniqid());
  51. }
  52. public function setVersionAttr($value) {
  53. return empty($value) ? '1.0' : $value;
  54. }
  55. public function setContentAttr($value) {
  56. return empty($value) ? '' : $value;
  57. }
  58. public function addVersion($data) {
  59. if (empty($data) || empty($data['app_id'])) {
  60. return false;
  61. }
  62. $_map['app_id'] = $data['app_id'];
  63. $data['is_default'] = 1;
  64. $_cnt = self::where($_map)->count();
  65. if (empty($_cnt)) {
  66. $data['is_default'] = 2;
  67. }
  68. if ($_obj = self::create($data, true)) {
  69. return $_obj->id;
  70. } else {
  71. return false;
  72. }
  73. }
  74. /***
  75. * 获取版本id对应的游戏名称列
  76. *
  77. * @param int $is_default
  78. * @param int $is_delete
  79. *
  80. * @return array
  81. */
  82. public function getClientIdName($is_default = 2, $is_delete = 2) {
  83. $_map['gameversion_model.is_default'] = $is_default;
  84. $_map['gameversion_model.is_delete'] = $is_delete;
  85. $_field = "CONCAT(gamex.name,' v',gameversion_model.version,' (',gameversion_model.id,')')";
  86. $_games = $this->useGlobalScope(false)
  87. ->with('gamex')
  88. ->where($_map)
  89. ->column($_field, 'gameversion_model.id');
  90. return $_games;
  91. }
  92. /**
  93. * 获取版本key
  94. *
  95. * @param $map
  96. *
  97. * @return mixed
  98. */
  99. public function getVersionKey($map) {
  100. $key = 'version_key'.json_encode($map);
  101. return $this->useGlobalScope(false)->cache($key, CommonConst::CONST_DAY_SECONDS, $this->cache_tag)->where($map)
  102. ->value('version_key');
  103. }
  104. /**
  105. * 根据游戏ID获取默认信息
  106. *
  107. * @param int $app_id 应用ID
  108. *
  109. * @return array|bool|false
  110. */
  111. public function getDefaultInfoByAppId($app_id) {
  112. $_id = $this->getDefaultIdByAppId($app_id);
  113. if (empty($_id)) {
  114. return false;
  115. }
  116. $_data = $this->getInfoById($_id);
  117. return $_data;
  118. }
  119. /**
  120. * 获取默认单条记录缓存key
  121. *
  122. * @param int $app_id 应用ID
  123. *
  124. * @return string
  125. */
  126. protected function getDefaultCacheKey($app_id) {
  127. return $this->cache_key_prefix.'default'.$app_id;
  128. }
  129. /**
  130. * 获取默认版本ID
  131. *
  132. * @param int $app_id 应用ID
  133. *
  134. * @return int
  135. */
  136. public function getDefaultIdByAppId($app_id) {
  137. if (empty($app_id)) {
  138. return 0;
  139. }
  140. $_default_cache_key = $this->getDefaultCacheKey($app_id);
  141. $_id = Cache::get($_default_cache_key);
  142. if (!empty($_id)) {
  143. return $_id;
  144. }
  145. /* 取默认 */
  146. $_map = [
  147. 'app_id' => $app_id,
  148. 'is_default' => CommonConst::CONST_DEFAULT,
  149. 'is_delete' => CommonConst::CONST_NOT_DELETE
  150. ];
  151. $_id = $this->where($_map)->value('id');
  152. if (empty($_id)) {
  153. return 0;
  154. }
  155. Cache::set($_default_cache_key, $_id);
  156. return $_id;
  157. }
  158. /**
  159. * 根据游戏ID获取包地址
  160. *
  161. * @param int $app_id 应用ID
  162. *
  163. * @return string
  164. */
  165. public function getPackageUrlByAppId($app_id) {
  166. $_data = $this->getDefaultInfoByAppId($app_id);
  167. $_package_url = get_val($_data, 'package_url', '');
  168. return $_package_url;
  169. }
  170. }