316 lines
12 KiB
PHP
316 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Advert;
|
|
use App\Models\Category;
|
|
use App\Models\Seller;
|
|
use App\Models\SellerLesson;
|
|
use App\Models\SellerLessonLog;
|
|
use App\User;
|
|
use Auth;
|
|
use DB;
|
|
use GuzzleHttp\Client;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\View;
|
|
use RuLong\Area\Models\OpenArea as Area;
|
|
use Session;
|
|
|
|
class IndexController extends Controller
|
|
{
|
|
|
|
public function __construct(Request $request)
|
|
{
|
|
parent::__construct($request);
|
|
$this->middleware('auth')->except(['location', 'getlocation', 'getOrgans']);
|
|
View::share('nav', 1);
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$user = Auth::user();
|
|
$location = Parent::getDefaultLocation();
|
|
$lat = $location['lat'];
|
|
$lng = $location['lng'];
|
|
$area = $location['area'];
|
|
|
|
$adverts = Advert::limit(\Params::get('advert_limit'))->orderBy('sort', 'asc')->get(); //banner
|
|
$c_lists = Category::with('storage')->where('parent_id', 1)
|
|
// ->orderBy('sort', 'asc')
|
|
// ->orderBy('created_at', 'desc')
|
|
->inRandomOrder()
|
|
->limit(10)->get(); //分类
|
|
|
|
$organs = $this->getDefaultOrgans($request); //机构列表
|
|
$organ = $user->organ;
|
|
//推荐课程
|
|
$recommend = $this->getRecommend();
|
|
|
|
//都在看
|
|
$watching_ids = SellerLessonLog::where('status', 1)->groupBy('lesson_id')->inRandomOrder()->take(3)->pluck('lesson_id');
|
|
$watchings = SellerLesson::with(['organ' => function ($query) use ($lat, $lng) {
|
|
$query->select('id', 'name', 'storage_id', 'user_id', 'category_id', DB::raw('round(ACOS(SIN((' . $lat . ' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((' . $lat . ' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((' . $lng . ' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380,2) as distance'));
|
|
}])->whereIn('seller_id', $watching_ids)->get();
|
|
|
|
$data = [
|
|
'couponlogs' => $user->couponlog()->where('status', 0)->count(),
|
|
'lessonlogs' => SellerLessonLog::where('user_id', $user->id)->where('status', 1)->count(),
|
|
];
|
|
return view('index.index', compact('adverts', 'organs', 'c_lists', 'user', 'data', 'recommend', 'watchings'));
|
|
}
|
|
|
|
//获取推荐课程
|
|
public function getRecommend()
|
|
{
|
|
$user = Auth::user();
|
|
$location = Parent::getDefaultLocation();
|
|
$lat = $location['lat'];
|
|
$lng = $location['lng'];
|
|
$area = $location['area'];
|
|
|
|
$organ = $user->organ;
|
|
$find = SellerLesson::when($area, function ($query) use ($area) {
|
|
$query->whereHas('organ', function ($query) use ($area) {
|
|
if ($area->depth == 2) {
|
|
$query->where('city_sn', $area->sn);
|
|
} elseif ($area->depth == 3) {
|
|
$query->where('area_sn', $area->sn);
|
|
} else {
|
|
$query->where('province_sn', $area->sn);
|
|
}
|
|
});
|
|
})->count();
|
|
|
|
if ($find) {
|
|
$area = Area::where('sn', '230100')->first();
|
|
}
|
|
|
|
//推荐课程
|
|
$recommend = SellerLesson::with(['organ' => function ($query) use ($lat, $lng) {
|
|
$query->select('id', 'name', 'storage_id', 'user_id', 'category_id', DB::raw('round(ACOS(SIN((' . $lat . ' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((' . $lat . ' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((' . $lng . ' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380,2) as distance'));
|
|
}])->when(!empty($organ->toArray()), function ($q) use ($organ) {
|
|
$q->where('seller_id', $organ->id);
|
|
})->when(empty($organ->toArray()) && $area, function ($query) use ($area) {
|
|
$query->whereHas('organ', function ($query) use ($area) {
|
|
if ($area->depth == 2) {
|
|
$query->where('city_sn', $area->sn);
|
|
} elseif ($area->depth == 3) {
|
|
$query->where('area_sn', $area->sn);
|
|
} else {
|
|
$query->where('province_sn', $area->sn);
|
|
}
|
|
});
|
|
})->inRandomOrder()->first();
|
|
return $recommend;
|
|
}
|
|
|
|
//通用获取组织
|
|
public function getDefaultOrgans($request)
|
|
{
|
|
$page = $request->page ?? 0; //获取当前页码
|
|
$location = Parent::getDefaultLocation();
|
|
$lat = $location['lat'];
|
|
$lng = $location['lng'];
|
|
$area = $location['area'];
|
|
|
|
$user = Auth::user();
|
|
if (!$area) {
|
|
return collect([]);
|
|
}
|
|
|
|
$organCateIds = [];
|
|
if ($user && $user->organ) {
|
|
$organCateIds = $user->organ->top_cate_id;
|
|
}
|
|
|
|
$pageNum = 5;
|
|
|
|
if ($user && $user->organ && $user->organ->id > 0 && !$page) {
|
|
$myorgan = Seller::with(['storage'])->where('id', Auth::user()->seller_id)->select('id', 'name', 'storage_id', 'user_id', 'category_id', DB::raw('round(ACOS(SIN((' . $lat . ' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((' . $lat . ' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((' . $lng . ' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380,2) as distance'))->first();
|
|
}
|
|
|
|
$organs = Seller::with('storage')
|
|
->where('type', 'organ')
|
|
->when($area, function ($query) use ($area) {
|
|
if ($area->depth == 2) {
|
|
$query->where('city_sn', $area->sn);
|
|
} elseif ($area->depth == 3) {
|
|
$query->where('area_sn', $area->sn);
|
|
} else {
|
|
$query->where('province_sn', $area->sn);
|
|
}
|
|
})
|
|
->when($organCateIds, function ($query) use ($organCateIds) {
|
|
$query->whereNotIn('category_id', $organCateIds);
|
|
})
|
|
->select('id', 'name', 'storage_id', 'user_id', 'category_id', DB::raw('round(ACOS(SIN((' . $lat . ' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((' . $lat . ' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((' . $lng . ' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380,2) as distance'))
|
|
->orderBy('distance', 'asc')
|
|
->orderBy('id', 'asc')
|
|
->get();
|
|
// ->paginate($pageNum);
|
|
|
|
if ($user && $user->organ && $user->organ->id > 0 && !$page) {
|
|
$organs->prepend($myorgan);
|
|
}
|
|
|
|
return $organs;
|
|
}
|
|
|
|
//分页加载更多组织
|
|
public function getOrgans(Request $request)
|
|
{
|
|
$organs = $this->getDefaultOrgans($request);
|
|
if ($organs->count() > 0) {
|
|
$html = response(view('index.organs', compact('organs')))->getContent();
|
|
return $this->success($html);
|
|
} else {
|
|
return $this->error('已经到最后一页');
|
|
}
|
|
}
|
|
|
|
//搜索课程
|
|
public function search(Request $request)
|
|
{
|
|
$title = $request->title;
|
|
$location = Parent::getDefaultLocation();
|
|
$lat = $location['lat'];
|
|
$lng = $location['lng'];
|
|
$area = $location['area'];
|
|
|
|
// $lists = SellerLesson::where('status', 1)->where('title', 'like', "%" . $request->title . "%")->get();
|
|
|
|
$user = Auth::user();
|
|
|
|
$organCateIds = $lessonids = [];
|
|
if ($user->organ) {
|
|
$organCateIds = $user->organ->top_cate_id;
|
|
$lessonids = SellerLesson::whereIn('category_id', $organCateIds)->where('seller_id', '<>', $user->organ->id)->pluck('id')->toArray();
|
|
}
|
|
|
|
$lists = SellerLesson::with(['organ.storage', 'organ' => function ($query) use ($lat, $lng) {
|
|
$query->select('id', 'name', 'storage_id', 'user_id', 'category_id', DB::raw('round(ACOS(SIN((' . $lat . ' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((' . $lat . ' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((' . $lng . ' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380,2) as distance'));
|
|
}])->when($area, function ($query) use ($area) {
|
|
$query->whereHas('organ', function ($query) use ($area) {
|
|
if ($area->depth == 2) {
|
|
$query->where('city_sn', $area->sn);
|
|
} elseif ($area->depth == 3) {
|
|
$query->where('area_sn', $area->sn);
|
|
} else {
|
|
$query->where('province_sn', $area->sn);
|
|
}
|
|
});
|
|
})->when(!empty($lessonids), function ($q) use ($lessonids) {
|
|
$q->whereNotIn('id', $lessonids);
|
|
})->where('status', 1)->where('title', 'like', "%" . $request->title . "%")->get();
|
|
|
|
return view('index.search', compact('lists'));
|
|
}
|
|
|
|
public function pass()
|
|
{
|
|
$user = Auth::user();
|
|
if ($user->info->update(['pass_at' => date('Y-m-d 23:59:59')])) {
|
|
return $this->success('今日不再提示');
|
|
} else {
|
|
return $this->error('设置失败');
|
|
}
|
|
}
|
|
|
|
public function subscribe()
|
|
{
|
|
return view('index.subscribe');
|
|
}
|
|
|
|
public function location(Request $request)
|
|
{
|
|
$lat = $request->lat;
|
|
$lng = $request->lng;
|
|
|
|
if (!$lat || !$lng) {
|
|
return $this->error('设置失败');
|
|
}
|
|
|
|
$location = Parent::getDefaultLocation();
|
|
|
|
if (!$location['real_area']) {
|
|
$url = "http://api.map.baidu.com/geocoder";
|
|
$client = new Client();
|
|
$response = $client->request('GET', $url, [
|
|
'timeout' => 30,
|
|
'query' => [
|
|
'location' => $lat . "," . $lng,
|
|
'output' => 'json',
|
|
],
|
|
]);
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
$data = json_decode($response->getBody()->getContents(), true);
|
|
$city = $data['result']['addressComponent']['city'];
|
|
} else {
|
|
return $this->location($url);
|
|
}
|
|
$area = Area::where('name', $city)->first();
|
|
|
|
//如果地址没有开通,默认哈尔滨
|
|
if ($area->status != 1) {
|
|
$area = Area::where('sn', '230100')->first();
|
|
$lat = $area->lat;
|
|
$lng = $area->lng;
|
|
}
|
|
|
|
Session::put('lat', $lat);
|
|
Session::put('lng', $lng);
|
|
Session::put('area', $area);
|
|
Session::put('real_area', $area);
|
|
return $this->success($city);
|
|
} else {
|
|
Session::put('lat', $lat);
|
|
Session::put('lng', $lng);
|
|
return $this->error(json_encode($location));
|
|
}
|
|
|
|
}
|
|
|
|
//设置地区
|
|
public function getLocation(Request $request)
|
|
{
|
|
|
|
$area = $request->area;
|
|
$title = $request->title;
|
|
|
|
if ($area) {
|
|
$area = Area::find($area);
|
|
Session::put('area', $area);
|
|
Session::push('history_area', $area);
|
|
$url = route('index.index');
|
|
if ($request->callback) {
|
|
$url = $request->callback;
|
|
}
|
|
return $this->success('设置成功', $url);
|
|
} else {
|
|
$location = Parent::getDefaultLocation();
|
|
$lat = $location['lat'];
|
|
$lng = $location['lng'];
|
|
$area = $location['area'];
|
|
$real_area = $location['real_area'];
|
|
$historys = $location['history_area'];
|
|
|
|
if (empty($lat) || empty($lng) || empty($area)) {
|
|
return redirect()->route('index.index');
|
|
}
|
|
|
|
$lists = Area::where('city', $area->city)->where('depth', 3)->where('status', 1)->get();
|
|
if ($title) {
|
|
$all_list = Area::where('status', 1)->where('name', 'like', "%" . $title . "%")->where('depth', 2)->orderBy('name_first_char', 'asc')->get();
|
|
} else {
|
|
$all_list = Area::where('status', 1)->where('depth', 2)->orderBy('name_first_char', 'asc')->get();
|
|
}
|
|
|
|
$hots = Area::where('status', 1)->where('hot', 1)->get();
|
|
return view('index.location', compact('area', 'lists', 'all_list', 'real_area', 'hots', 'historys'));
|
|
}
|
|
|
|
}
|
|
|
|
}
|