| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace app\common\service;
- use app\common\model\ShopGoodsMenuModel;
- use utils\RedisCache;
- /**
- * 商品菜单服务 by wes
- * Class ShopGoodsMenuService
- * @package app\common\service
- */
- class ShopGoodsMenuService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new ShopGoodsMenuModel();
- }
- /**
- * 静态化入口
- * @return static|null
- */
- public static function make()
- {
- if(!self::$instance){
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取列表
- * @param $map 分组
- * @param $pageSize 分页大小
- * @param $field 返回字段
- * @param $cache 是否缓存数据,默认是
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getList($map, $pageSize=10, $field='', $cache=true)
- {
- $page = request()->post('page', 1);
- $cacheKey = "caches:goodsMenu:list_{$page}_{$pageSize}_".md5(json_encode($map, 256).$field);
- $list = RedisCache::get($cacheKey);
- if($list && $cache){
- return $list;
- }
- $where = ['status'=> 1];
- $field = $field? $field : '*';
- $order = isset($map['sort']) && $map['sort']? $map['sort'] : 'sort desc,id asc';
- $list = $this->model->where($where)
- ->field($field)
- ->order($order)
- ->paginate($pageSize);
- $list = $list? $list->toArray():[];
- if($list){
- RedisCache::set($cacheKey, $list, rand(5,10));
- }
- return $list;
- }
- }
|