海南旅游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.
 
 
 

83 lines
2.3 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\Slide;
use App\Models\Special;
use App\Models\UserChannel;
/**
* 小程序首页
* Class IndexController
* @package App\Http\Controllers\Api
*/
class IndexController extends Controller
{
// 首页数据大杂烩
public function index()
{
# 轮播图
$slide = Slide::where(['agent_id' => $this->agent_id, 'status' => 1])
->orderBy('sort')->orderBy('id', 'DESC')->limit(10)
->get(['title', 'picture', 'type', 'url']);
# 公告
$notice = Notice::where('agent_id', $this->agent_id)->limit(10)->get(['id', 'title', 'updated_at']);
# 我的频道
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']);
}
# 专题列表
$special = Special::query()
->where('agent_id', $this->agent_id)
->orderBy('sort')
->orderBy('id')
->get(['id', 'picture_ad']);
# 人气爆款
$hots = AgentProduct::query()
->where('agent_id', $this->agent_id)
->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with
->with('product:id,title,pictures') //必须查询ID才能正常查询
->limit(6)->get();
return $this->success([
'slide' => $slide,
'notice' => $notice,
'my_channels' => $my_channels,
'special' => $special,
'hots' => $hots,
]);
}
}