| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?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)
- {
- $cacheKey = "caches:merchantCategory:{$num}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = $this->model->where(['is_menu'=> 1,'status'=> 1,'mark'=>1])
- ->select(['id','icon','name','pages','sort'])
- ->limit($num)
- ->orderBy('sort','asc')
- ->orderBy('id','desc')
- ->get()
- ->each(function($item, $k){
- $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
- });
- $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)
- {
- $cacheKey = "caches:merchantCategory:option_{$showType}_{$num}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = $this->model->where(['is_menu'=> 1,'status'=> 1,'mark'=>1])
- ->where(function($query) use($showType){
- if($showType == 1){
- $query->whereNotIn('type',[3]);
- }
- })
- ->select(['id','name','icon'])
- ->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']) : '';
- }
- unset($item);
- }
- RedisService::set($cacheKey, $datas, rand(30, 60));
- }
- return $datas;
- }
- }
|