Commit 533c48a2 by 杨树贤

修改品牌和分类的获取方式

parent e5a2232e
......@@ -82,7 +82,7 @@ class SelfBrandModel extends Model
//获取品牌信息
public function getBrand($brandId)
{
$expireMinutes = 5;
$expireMinutes = 3;
return Cache::remember('brandList_' . $brandId, $expireMinutes, function () use ($brandId) {
//没有再去数据库里面取
......@@ -101,45 +101,6 @@ class SelfBrandModel extends Model
{
$expireMinutes = 5;
//先去缓存里面取
$Redis = new RedisModel;
$brandList = $Redis->hgetall('Self_Brand');
$brandList = array_map(function ($value) {
return json_decode($value, true);
}, $brandList);
//只要status=1的品牌
$brandList = array_values(array_filter($brandList, function ($value) {
return $value['status'] == 1;
}));
//如果要有地区筛选,则需要做数据处理
if (!empty($map['brand_area']) && !empty($brandList)) {
$brandListByArea = [];
$areaIds = explode(',', $map['brand_area']);
foreach ($brandList as $key => $brand) {
if (in_array($brand['brand_area'], $areaIds)) {
$brandListByArea[] = $brand;
}
}
return $brandListByArea;
}
//如果有id筛选,就要做数据处理
if (!empty($map['brand_id']) && !empty($brandList)) {
$brandListByBrandId = [];
$brandIds = explode(',', $map['brand_id']);
foreach ($brandList as $key => $brand) {
if (in_array($brand['brand_id'], $brandIds)) {
$brandListByBrandId[] = $brand;
}
}
return $brandListByBrandId;
}
//没有就去数据库里面取
if (!$brandList) {
return Cache::remember('brandList_' . json_encode($map), $expireMinutes, function () use ($map) {
$field = ['brand_id', 'brand_name', 'brand_logo', 'brand_area'];
$query = $this->select($field)->where('status', 1);
......@@ -148,11 +109,16 @@ class SelfBrandModel extends Model
$areaIds = explode(',', $map['brand_area']);
$query->whereIn('brand_area', $areaIds);
}
if (!empty($map['brand_id'])) {
$brandId = explode(',', $map['brand_id']);
$query->whereIn('brand_id', $brandId);
}
return $query->get()->toArray();
});
if (!empty($map['limit'])) {
$query->limit($map['limit']);
}
return $brandList;
return $query->get()->toArray();
});
}
}
......@@ -9,7 +9,7 @@ use Common\Model\RedisModel;
class SelfClassifyModel extends Model
{
protected $connection = false;
protected $connection = 'self';
protected $table = 'self_classify';
public $timestamps = false;
......@@ -53,23 +53,11 @@ class SelfClassifyModel extends Model
public function getClassList()
{
$classificationsExpireMinute = 5;
//先去缓存里面取
$Redis = new RedisModel;
$classList = $Redis->hgetall('Self_SelfClassInfo');
$classList = array_map(function ($value) {
return json_decode($value, true);
}, $classList);
//只要status=1的分类
$classList = array_values(array_filter($classList, function ($value) {
return $value['status'] == 1;
}));
$classList = $this->transform($classList);
if (!$classList) {
return Cache::remember('classList', $classificationsExpireMinute, function () {
$classList = Cache::remember('classList', $classificationsExpireMinute, function () {
$field = [
'class_id',
'parent_id',
'class_name',
'status',
'class_icon',
......@@ -81,43 +69,23 @@ class SelfClassifyModel extends Model
$query->select($field);
},
])->where('status', 1)->where('parent_id', 0)
->orderBy('sort', 'desc')->get()->toArray();
->orderBy('class_id', 'desc')->limit(9)->get()->toArray();
});
}
return $classList;
}
private function transform($data)
{
//将子分类放到父分类下面
$parentClass = [];
foreach ($data as $key => $item) {
//先获取所有父分类
if ($item['parent_id'] == 0) {
$item['sub_class'] = [];
$parentClass[] = $item;
unset($data[$key]);
}
}
foreach ($parentClass as $key => $value) {
foreach ($data as $k => $v) {
if ($v['parent_id'] == $value['class_id']) {
$parentClass[$key]['sub_class'][] = $v;
}
}
}
return $parentClass;
}
//获取分类参数
public function getClassAttr($class_id){
public function getClassAttr($class_id)
{
$Redis = new RedisModel();
$Cache = json_decode($Redis->hget('class_attr', $class_id), true);
if(empty($Cache)){
if (empty($Cache)) {
return false;
}
return $Cache;
}
}
common @ 58c9ab73
Subproject commit 157cfb8c0150092038e5a31e1acbc9c7b295bde2
Subproject commit 58c9ab735bada96c2f9175771693de1a4386e917
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment