MerchantCategoryService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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, $userId=0)
  54. {
  55. $locale = RedisService::get("caches:locale:lang_{$userId}");
  56. $locale = $locale? $locale : session('locale_lang');
  57. $locale = $locale? $locale :'zh-cn';
  58. $cacheKey = "caches:merchantCategory:{$num}_{$userId}_{$locale}";
  59. $datas = RedisService::get($cacheKey);
  60. if($datas){
  61. return $datas;
  62. }
  63. if($locale == 'zh-cn'){
  64. $field = ['id','name','icon','pages','sort','is_menu'];
  65. }else{
  66. $field = ['id','name',"name_{$locale}",'icon','pages','sort','is_menu'];
  67. }
  68. $datas = $this->model->where(['is_menu'=> 1,'status'=> 1,'mark'=>1])
  69. ->select($field)
  70. ->limit($num)
  71. ->orderBy('sort','asc')
  72. ->orderBy('id','desc')
  73. ->get()
  74. ->each(function($item, $k) use($locale){
  75. $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
  76. $name = isset($item['name'])? $item['name'] : '';
  77. $item['name'] = isset($item['name_'.$locale])? $item['name_'.$locale] : $name;
  78. });
  79. $datas = $datas? $datas->toArray() : [];
  80. if($datas){
  81. RedisService::set($cacheKey, $datas, 3 * 86400);
  82. }
  83. return $datas;
  84. }
  85. /**
  86. * 分类列表
  87. * @param $showType
  88. * @param int $num
  89. * @return array|mixed
  90. */
  91. public function getOptions($showType=0,$num=99, $userId=0)
  92. {
  93. $locale = RedisService::get("caches:locale:lang_{$userId}");
  94. $locale = $locale? $locale : session('locale_lang');
  95. $locale = $locale? $locale :'zh-cn';
  96. $cacheKey = "caches:merchantCategory:option_{$showType}_{$num}_{$userId}_{$locale}";
  97. $datas = RedisService::get($cacheKey);
  98. if($datas){
  99. return $datas;
  100. }
  101. if($locale == 'zh-cn'){
  102. $field = ['id','name','icon','sort'];
  103. }else{
  104. $field = ['id','name',"name_{$locale}",'icon','sort'];
  105. }
  106. $datas = $this->model->where(['is_menu'=> 1,'status'=> 1,'mark'=>1])
  107. ->where(function($query) use($showType){
  108. if($showType == 1){
  109. $query->whereNotIn('type',[3]);
  110. }
  111. })
  112. ->select($field)
  113. ->limit($num)
  114. ->orderBy('sort','desc')
  115. ->orderBy('id','desc')
  116. ->get();
  117. $datas = $datas? $datas->toArray() : [];
  118. if($datas){
  119. foreach($datas as &$item){
  120. if(isset($item['icon'])){
  121. $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
  122. $name = isset($item['name'])? $item['name'] : '';
  123. $item['name'] = isset($item['name_'.$locale])? $item['name_'.$locale] : $name;
  124. }
  125. unset($item);
  126. }
  127. RedisService::set($cacheKey, $datas, rand(30, 60));
  128. }
  129. return $datas;
  130. }
  131. }