MerchantCategoryService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\MerchantCategoryModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 商户分类服务管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class MerchantCategoryService
  20. * @package App\Services\Api
  21. */
  22. class MerchantCategoryService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * MerchantCategoryService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new MerchantCategoryModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 按排序获取缓存列表
  49. * @param $position
  50. * @param int $num
  51. * @return array|mixed
  52. */
  53. public function getMenuList($num=8)
  54. {
  55. $cacheKey = "caches:merchantCategory:{$num}";
  56. $datas = RedisService::get($cacheKey);
  57. if($datas){
  58. return $datas;
  59. }
  60. $datas = $this->model->where(['is_menu'=> 1,'status'=> 1,'mark'=>1])
  61. ->select(['id','icon','name','pages','sort'])
  62. ->limit($num)
  63. ->orderBy('sort','asc')
  64. ->orderBy('id','desc')
  65. ->get()
  66. ->each(function($item, $k){
  67. $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
  68. });
  69. $datas = $datas? $datas->toArray() : [];
  70. if($datas){
  71. RedisService::set($cacheKey, $datas, 3 * 86400);
  72. }
  73. return $datas;
  74. }
  75. /**
  76. * 分类列表
  77. * @param $showType
  78. * @param int $num
  79. * @return array|mixed
  80. */
  81. public function getOptions($showType=0,$num=99)
  82. {
  83. $cacheKey = "caches:merchantCategory:option_{$showType}_{$num}";
  84. $datas = RedisService::get($cacheKey);
  85. if($datas){
  86. return $datas;
  87. }
  88. $datas = $this->model->where(['is_menu'=> 1,'status'=> 1,'mark'=>1])
  89. ->where(function($query) use($showType){
  90. if($showType == 1){
  91. $query->whereNotIn('type',[3]);
  92. }
  93. })
  94. ->select(['id','name','icon'])
  95. ->limit($num)
  96. ->orderBy('sort','desc')
  97. ->orderBy('id','desc')
  98. ->get();
  99. $datas = $datas? $datas->toArray() : [];
  100. if($datas){
  101. foreach($datas as &$item){
  102. if(isset($item['icon'])){
  103. $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
  104. }
  105. unset($item);
  106. }
  107. RedisService::set($cacheKey, $datas, rand(30, 60));
  108. }
  109. return $datas;
  110. }
  111. }