// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\MerchantCategoryModel; use App\Services\BaseService; use App\Services\RedisService; /** * 商户分类服务管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class MerchantCategoryService * @package App\Services\Api */ class MerchantCategoryService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * MerchantCategoryService constructor. */ public function __construct() { $this->model = new MerchantCategoryModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 按排序获取缓存列表 * @param $position * @param int $num * @return array|mixed */ public function getMenuList($num=8, $userId=0) { $locale = RedisService::get("caches:locale:lang_{$userId}"); $locale = $locale? $locale : session('locale_lang'); $locale = $locale? $locale :'zh-cn'; $cacheKey = "caches:merchantCategory:{$num}_{$userId}_{$locale}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } if($locale == 'zh-cn'){ $field = ['id','name','icon','pages','sort','is_menu']; }else{ $field = ['id','name',"name_{$locale}",'icon','pages','sort','is_menu']; } $datas = $this->model->where(['is_menu'=> 1,'status'=> 1,'mark'=>1]) ->select($field) ->limit($num) ->orderBy('sort','asc') ->orderBy('id','desc') ->get() ->each(function($item, $k) use($locale){ $item['icon'] = $item['icon']? get_image_url($item['icon']) : ''; $name = isset($item['name'])? $item['name'] : ''; $item['text'] = isset($item['name_'.$locale])? $item['name_'.$locale] : $name; }); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, 3 * 86400); } return $datas; } /** * 分类列表 * @param $showType * @param int $num * @return array|mixed */ public function getOptions($showType=0,$num=99, $userId=0) { $locale = RedisService::get("caches:locale:lang_{$userId}"); $locale = $locale? $locale : session('locale_lang'); $locale = $locale? $locale :'zh-cn'; $cacheKey = "caches:merchantCategory:option_{$showType}_{$num}_{$userId}_{$locale}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } if($locale == 'zh-cn'){ $field = ['id','name','icon','sort']; }else{ $field = ['id','name',"name_{$locale}",'icon','sort']; } $datas = $this->model->where(['is_menu'=> 1,'status'=> 1,'mark'=>1]) ->where(function($query) use($showType){ if($showType == 1){ $query->whereNotIn('type',[3]); } }) ->select($field) ->limit($num) ->orderBy('sort','desc') ->orderBy('id','desc') ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ foreach($datas as &$item){ if(isset($item['icon'])){ $item['icon'] = $item['icon']? get_image_url($item['icon']) : ''; $name = isset($item['name'])? $item['name'] : ''; $item['text'] = isset($item['name_'.$locale])? $item['name_'.$locale] : $name; } unset($item); } RedisService::set($cacheKey, $datas, rand(30, 60)); } return $datas; } }