海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 lines
3.1 KiB

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Channel;
use App\Models\Notice;
use App\Models\AgentProduct;
use App\Models\Advertising;
use App\Models\Special;
use App\Models\UserChannel;
use Illuminate\Support\Facades\Storage;
/**
* 小程序首页
* Class IndexController
* @package App\Http\Controllers\Api
*/
class IndexController extends Controller
{
// 首页数据大杂烩
public function index()
{
$img_prefix = Storage::disk('public')->url('');
# 轮播图
$slide = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1])
->orderBy('sort')->orderBy('id', 'DESC')->limit(10)
->get(['title', 'picture', 'type', 'url']);
foreach ($slide as &$v) {
$v->picture = $img_prefix . $v->picture;
}
# 公告
$notice = Notice::where('agent_id', $this->agent_id)->limit(10)
->orderBy('sort')->orderBy('id', 'desc')->get(['id', 'title']);
# 我的频道
if ($this->user_id) {
$channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels');
//如果不存在则存入初始数据
if (!$channel_ids) {
$channel_ids = Channel::where([
['agent_id', '=', $this->agent_id],
['pid', '<>', 0],
])
->orderBy('id')
->limit(8)
->pluck('id')
->toArray();
//存入user_channel
UserChannel::where('user_id', $this->user_id)
->insert([
'user_id' => $this->user_id,
'channels' => json_encode($channel_ids)
]);
}
$my_channels = Channel::where('agent_id', $this->agent_id)
->whereIn('id', $channel_ids)
->get(['id', 'name', 'icon']);
} else {
$my_channels = Channel::where('agent_id', $this->agent_id)
->where('pid', '<>', 0)
->orderBy('id')
->limit(8)
->get(['id', 'name', 'icon']);
}
if (!$my_channels->isEmpty()) {
foreach ($my_channels as &$v) {
$v->icon = $img_prefix . $v->icon;
}
}
# 专题列表
$special = Special::query()
->where('agent_id', $this->agent_id)
->orderBy('sort')->orderBy('id')
->limit(6)->get(['id', 'picture_ad']);
if (!$special->isEmpty()) {
foreach ($special as $k=>&$v) {
$v->picture_ad = $img_prefix . $v->picture_ad;
}
}
# 人气爆款
$hots = AgentProduct::with('product:id,title,pictures') //必须查询ID才能正常查询
->where('agent_id', $this->agent_id)
->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with
->orderBy('sale', 'desc')->orderBy('id', 'desc')
->limit(6)->get();
if (!$hots->isEmpty()) {
foreach ($hots as &$v) {
if (!empty($v->product->pictures) && is_array($v->product->pictures)) {
$v->product->pictures = array_map(function($item) use ($img_prefix) {
return strpos($item, $img_prefix) === false ? $img_prefix . $item : $item;
}, $v->product->pictures);
}
}
}
return $this->success([
'slide' => $slide,
'notice' => $notice,
'my_channels' => $my_channels,
'special' => $special,
'hots' => $hots,
]);
}
}