CategoryCache.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * CategoryCache.php UTF-8
  4. * 类型
  5. *
  6. * @date : 2018/5/3 19:42
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\game;
  13. use huo\controller\common\Base;
  14. use huo\model\game\CategoryModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\GameConst;
  17. use huolib\tool\StrUtils;
  18. use think\Cache;
  19. class CategoryCache extends Base {
  20. public static function ins() {
  21. return new static();
  22. }
  23. /**
  24. * 获取标签类型KEY
  25. *
  26. * @param int $type 1 标签 2 类型
  27. *
  28. * @return string
  29. */
  30. private function getCategoryKey($type) {
  31. if (GameConst::CATEGORY_TYPE_TAG == $type) {
  32. return CacheConst::KEY_CACHE_GAME_TAG; //游戏标签key
  33. } else {
  34. return CacheConst::KEY_CACHE_GAME_CATEGORY; //游戏类型
  35. }
  36. }
  37. /**
  38. * 获取所有类型
  39. *
  40. * @param int $type 1 标签 2 类型
  41. *
  42. * @return array|bool|mixed
  43. */
  44. public function getIdNames($type) {
  45. $_key = $this->getCategoryKey($type);
  46. $_cate_data_json = Cache::get($_key);
  47. $_cate_data = json_decode($_cate_data_json, true);
  48. if (!is_array($_cate_data)) {
  49. $_cate_data = $_cate_data_json;
  50. }
  51. if (!is_array($_cate_data) || empty($_cate_data)) {
  52. $_cate_data = (new CategoryModel())->getIdNames($type);
  53. if (empty($_cate_data)) {
  54. return false;
  55. }
  56. $this->saveCategoryCache($type, $_cate_data);
  57. }
  58. return $_cate_data;
  59. }
  60. /**
  61. * 保存玩家cache 数据
  62. *
  63. * @param int $type 1 标签 2 类型
  64. * @param array $cate_data
  65. * @param int $ttl
  66. */
  67. public function saveCategoryCache($type, $cate_data, $ttl = 3600) {
  68. $_key = $this->getCategoryKey($type);
  69. Cache::set($_key, json_encode($cate_data), $ttl);
  70. }
  71. /**
  72. * 更新游戏类型
  73. *
  74. * @param int $type 1 标签 2 类型 4 首页标记
  75. * @param array $cate_data
  76. *
  77. * @return bool
  78. */
  79. public function updateCategory($type, $cate_data) {
  80. $_key = $this->getCategoryKey($type);
  81. Cache::rm($_key);
  82. return (new CategoryModel())->updateCategory($cate_data, $cate_data['id']);
  83. }
  84. /**
  85. * 添加游戏类型
  86. *
  87. * @param $type
  88. * @param $cate_data
  89. *
  90. * @return mixed
  91. */
  92. public function addCategory($type, $cate_data) {
  93. $_key = $this->getCategoryKey($type);
  94. Cache::rm($_key);
  95. return (new CategoryModel())->addCategory($cate_data);
  96. }
  97. /**
  98. * 根据cate_id 获取对应名称
  99. *
  100. * @param int $type
  101. * @param string $ids
  102. *
  103. * @return array
  104. */
  105. public function getNameByIds($type = 2, $ids) {
  106. $_ids = $ids;
  107. if (is_string($_ids)) {
  108. $_ids_arr = StrUtils::strToArr($ids);
  109. }
  110. if (empty($_ids_arr)) {
  111. return [];
  112. }
  113. $_cates = $this->getIdNames($type);
  114. $_name_arr = [];
  115. foreach ($_ids_arr as $_k => $_v) {
  116. $_cate_id = intval($_v);
  117. if (empty($_cates[$_cate_id])) {
  118. continue;
  119. }
  120. $_name_arr[$_k] = $_cates[$_cate_id];
  121. }
  122. return array_values($_name_arr);
  123. }
  124. /**
  125. * 删除类型
  126. *
  127. * @param $id
  128. *
  129. * @return bool
  130. */
  131. public function deleteCategory($id) {
  132. $_data = (new CategoryModel())->getInfoById($id);
  133. if (empty($_data)) {
  134. return false;
  135. }
  136. $_rs = (new CategoryModel())->deleteCategory($id);
  137. $_key = $this->getCategoryKey($_data['type']);
  138. Cache::rm($_key);
  139. return $_rs;
  140. }
  141. /**
  142. * 判断数组中值是否为类型|标签|首页标签,返回相应的数组
  143. *
  144. * @param int $type 1 标签 2 类型 4 首页标记
  145. * @param array $array
  146. *
  147. * @return array
  148. */
  149. public function returnTypeArray($type, $array = []) {
  150. if (empty($array)) {
  151. return $array;
  152. }
  153. $_cate_arr = $this->getIdNames($type);
  154. foreach ($array as $_k => $_v) {
  155. if (empty($_cate_arr[$_v])) { //不存在表示非相应类型
  156. unset($array[$_k]);
  157. }
  158. }
  159. return $array;
  160. }
  161. }