| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- // +----------------------------------------------------------------------
- // | EasyAdmin
- // +----------------------------------------------------------------------
- // | PHP交流群: 763822524
- // +----------------------------------------------------------------------
- // | 开源协议 https://mit-license.org
- // +----------------------------------------------------------------------
- // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
- // +----------------------------------------------------------------------
- namespace app\common\service;
- use app\common\model\ShopGoodsSpec;
- use utils\RedisCache;
- /**
- * 商品规格服务 by wes
- * Class ShopGoodsSpecService
- * @package app\common\service
- */
- class ShopGoodsSpecService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new ShopGoodsSpec();
- }
- /**
- * 静态化入口
- * @return static|null
- */
- public static function make()
- {
- if(!self::$instance){
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取规格列表数据
- * @param $goodsId 商品ID
- * @param string $field 返回字段
- * @param string $cache 是否缓存
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getListByGoods($goodsId, $field='', $cache=false)
- {
- if($goodsId<=0){
- return false;
- }
- $cacheKey = "caches:goodsSpec:list_{$goodsId}".($field? '_'.md5($field):'');
- $data = RedisCache::get($cacheKey);
- if($data && $cache){
- return $data;
- }
- $where = ['goods_id'=> $goodsId];
- $field = $field? $field : 'spec_ids,spec_text,original_price,price,picture,stock';
- $data = $this->model->where($where)->field($field)->select();
- $data = $data? $data->toArray():[];
- if($data && $cache){
- RedisCache::set($cacheKey, $data, rand(3, 5));
- }
- return $data;
- }
- }
|