ShopGoodsMenuService.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\ShopGoodsMenuModel;
  4. use utils\RedisCache;
  5. /**
  6. * 商品菜单服务 by wes
  7. * Class ShopGoodsMenuService
  8. * @package app\common\service
  9. */
  10. class ShopGoodsMenuService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new ShopGoodsMenuModel();
  17. }
  18. /**
  19. * 静态化入口
  20. * @return static|null
  21. */
  22. public static function make()
  23. {
  24. if(!self::$instance){
  25. self::$instance = new static();
  26. }
  27. return self::$instance;
  28. }
  29. /**
  30. * 获取列表
  31. * @param $map 分组
  32. * @param $pageSize 分页大小
  33. * @param $field 返回字段
  34. * @param $cache 是否缓存数据,默认是
  35. * @return array|mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getList($map, $pageSize=10, $field='', $cache=true)
  41. {
  42. $page = request()->post('page', 1);
  43. $cacheKey = "caches:goodsMenu:list_{$page}_{$pageSize}_".md5(json_encode($map, 256).$field);
  44. $list = RedisCache::get($cacheKey);
  45. if($list && $cache){
  46. return $list;
  47. }
  48. $where = ['status'=> 1];
  49. $field = $field? $field : '*';
  50. $order = isset($map['sort']) && $map['sort']? $map['sort'] : 'sort desc,id asc';
  51. $list = $this->model->where($where)
  52. ->field($field)
  53. ->order($order)
  54. ->paginate($pageSize);
  55. $list = $list? $list->toArray():[];
  56. if($list){
  57. RedisCache::set($cacheKey, $list, rand(5,10));
  58. }
  59. return $list;
  60. }
  61. }