123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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['name'] = 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['name'] = isset($item['name_'.$locale])? $item['name_'.$locale] : $name;
- }
- unset($item);
- }
- RedisService::set($cacheKey, $datas, rand(30, 60));
- }
- return $datas;
- }
- }
|