ShopGoodsMenuService.php 2.3 KB

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