123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- /**
- * CategoryCache.php UTF-8
- * 类型
- *
- * @date : 2018/5/3 19:42
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\controller\game;
- use huo\controller\common\Base;
- use huo\model\game\CategoryModel;
- use huolib\constant\CacheConst;
- use huolib\constant\GameConst;
- use huolib\tool\StrUtils;
- use think\Cache;
- class CategoryCache extends Base {
- public static function ins() {
- return new static();
- }
- /**
- * 获取标签类型KEY
- *
- * @param int $type 1 标签 2 类型
- *
- * @return string
- */
- private function getCategoryKey($type) {
- if (GameConst::CATEGORY_TYPE_TAG == $type) {
- return CacheConst::KEY_CACHE_GAME_TAG; //游戏标签key
- } else {
- return CacheConst::KEY_CACHE_GAME_CATEGORY; //游戏类型
- }
- }
- /**
- * 获取所有类型
- *
- * @param int $type 1 标签 2 类型
- *
- * @return array|bool|mixed
- */
- public function getIdNames($type) {
- $_key = $this->getCategoryKey($type);
- $_cate_data_json = Cache::get($_key);
- $_cate_data = json_decode($_cate_data_json, true);
- if (!is_array($_cate_data)) {
- $_cate_data = $_cate_data_json;
- }
- if (!is_array($_cate_data) || empty($_cate_data)) {
- $_cate_data = (new CategoryModel())->getIdNames($type);
- if (empty($_cate_data)) {
- return false;
- }
- $this->saveCategoryCache($type, $_cate_data);
- }
- return $_cate_data;
- }
- /**
- * 保存玩家cache 数据
- *
- * @param int $type 1 标签 2 类型
- * @param array $cate_data
- * @param int $ttl
- */
- public function saveCategoryCache($type, $cate_data, $ttl = 3600) {
- $_key = $this->getCategoryKey($type);
- Cache::set($_key, json_encode($cate_data), $ttl);
- }
- /**
- * 更新游戏类型
- *
- * @param int $type 1 标签 2 类型 4 首页标记
- * @param array $cate_data
- *
- * @return bool
- */
- public function updateCategory($type, $cate_data) {
- $_key = $this->getCategoryKey($type);
- Cache::rm($_key);
- return (new CategoryModel())->updateCategory($cate_data, $cate_data['id']);
- }
- /**
- * 添加游戏类型
- *
- * @param $type
- * @param $cate_data
- *
- * @return mixed
- */
- public function addCategory($type, $cate_data) {
- $_key = $this->getCategoryKey($type);
- Cache::rm($_key);
- return (new CategoryModel())->addCategory($cate_data);
- }
- /**
- * 根据cate_id 获取对应名称
- *
- * @param int $type
- * @param string $ids
- *
- * @return array
- */
- public function getNameByIds($type = 2, $ids) {
- $_ids = $ids;
- if (is_string($_ids)) {
- $_ids_arr = StrUtils::strToArr($ids);
- }
- if (empty($_ids_arr)) {
- return [];
- }
- $_cates = $this->getIdNames($type);
- $_name_arr = [];
- foreach ($_ids_arr as $_k => $_v) {
- $_cate_id = intval($_v);
- if (empty($_cates[$_cate_id])) {
- continue;
- }
- $_name_arr[$_k] = $_cates[$_cate_id];
- }
- return array_values($_name_arr);
- }
- /**
- * 删除类型
- *
- * @param $id
- *
- * @return bool
- */
- public function deleteCategory($id) {
- $_data = (new CategoryModel())->getInfoById($id);
- if (empty($_data)) {
- return false;
- }
- $_rs = (new CategoryModel())->deleteCategory($id);
- $_key = $this->getCategoryKey($_data['type']);
- Cache::rm($_key);
- return $_rs;
- }
- /**
- * 判断数组中值是否为类型|标签|首页标签,返回相应的数组
- *
- * @param int $type 1 标签 2 类型 4 首页标记
- * @param array $array
- *
- * @return array
- */
- public function returnTypeArray($type, $array = []) {
- if (empty($array)) {
- return $array;
- }
- $_cate_arr = $this->getIdNames($type);
- foreach ($array as $_k => $_v) {
- if (empty($_cate_arr[$_v])) { //不存在表示非相应类型
- unset($array[$_k]);
- }
- }
- return $array;
- }
- }
|