广告切换

This commit is contained in:
2025-07-10 18:08:08 +08:00
parent 2a2c4d434b
commit 74ac6d3036
3 changed files with 42 additions and 15 deletions
+6 -13
View File
@@ -44,19 +44,12 @@ class AdpController extends BaseController
if ($scene === 'default') {
$item['title'] = isset($item['translation']) ? $item['translation']['title'] : '';
unset($item['translation']);
$ad = AdModel::with('translation')->where(['adp_id' => $item['id'], 'status' => 1])->limit(8)->get();
foreach ($ad as $k => $v) {
$v['type_txt'] = $v['type'] == 1?'新车' : ($v['type'] == 2?'二手车' : '');
$v['alt'] = isset($v['translation']) ? $v['translation']['alt'] : '';
$v['title'] = isset($v['translation']) ? $v['translation']['title'] : '';
if(!empty($v['img'])){
$v['full_img'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($v['img']);
}else{
$v['full_img'] = '';
}
unset($v['translation']);
$item['ad'][$k] = $v;
}
$ad = AdModel::with('translation')->where(['adp_id' => $item['id'], 'type' => 1, 'status' => 1])->limit(8)->get()->toArray();
$ad = $this->getService()->ad_arrange($ad);
$item['new_ad'] = $ad;
$ad = AdModel::with('translation')->where(['adp_id' => $item['id'], 'type' => 2, 'status' => 1])->limit(8)->get()->toArray();
$ad = $this->getService()->ad_arrange($ad);
$item['used_ad'] = $ad;
}
return $item;
+16
View File
@@ -77,4 +77,20 @@ class AdpModel extends Model
{
return $this->hasMany(AdModel::class, 'adp_id','id');
}
/**
* 广告
*/
public function new_ad()
{
return $this->hasMany(AdModel::class, 'adp_id','id')->where(['type' => 1]);
}
/**
* 广告
*/
public function used_ad()
{
return $this->hasMany(AdModel::class, 'adp_id','id')->where(['type' => 2]);
}
}
+20 -2
View File
@@ -31,8 +31,10 @@ class AdpService extends BaseService
'fields' => ['id', 'new_url', 'used_url', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
'relations' => [
'translation' => ['adp_id', 'title'],
'ad' => ['id', 'type', 'img', 'price', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
'ad.translation' => ['ad_id', 'alt', 'title'],
// 'new_ad' => ['id', 'type', 'img', 'price', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
// 'new_ad.translation' => ['ad_id', 'alt', 'title'],
// 'used_ad' => ['id', 'type', 'img', 'price', 'status', 'sort', 'creater', 'created_at', 'updated_at'],
// 'used_ad.translation' => ['ad_id', 'alt', 'title'],
],
'sorts' => [
['field' => 'sort', 'direction' => 'desc'],
@@ -46,4 +48,20 @@ class AdpService extends BaseService
},
];
}
public function ad_arrange(array $ad): array{
foreach ($ad as $k => &$v) {
$v['type_txt'] = $v['type'] == 1?'新车' : ($v['type'] == 2?'二手车' : '');
$v['alt'] = isset($v['translation']) ? $v['translation']['alt'] : '';
$v['title'] = isset($v['translation']) ? $v['translation']['title'] : '';
if(!empty($v['img'])){
$v['full_img'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($v['img']);
}else{
$v['full_img'] = '';
}
unset($v['translation']);
$item['ad'][$k] = $v;
}
return $ad;
}
}