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.

233 lines
9.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* 其他
*/
namespace App\Http\Controllers\Mobile;
use App\Helpers\ResponseCode;
use App\Models\AppointmentConfig;
use App\Models\AppointmentType;
use App\Models\Banner;
use App\Models\Company;
use App\Models\Config;
use App\Repositories\YuanheRepository;
use Illuminate\Support\Facades\Validator;
class OtherController extends CommonController
{
/**
* @OA\Get(
* path="/api/mobile/other/config",
* tags={"小程序-其他"},
* summary="获取配置信息",
* @OA\Parameter(name="appointment_type_id", in="query", @OA\Schema(type="integer"), required=true, description="场地类型"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function config()
{
$all = request()->all();
$config = Config::get();
$appointment = AppointmentConfig::where(function ($query) use ($all) {
if (isset($all['appointment_type_id'])) {
$query->where('appointment_type_id', $all['appointment_type_id']);
}
})->where('show_front', 1)->get();
// 场地类型
$appointment_type = AppointmentType::get();
return $this->success(compact('config', 'appointment', 'appointment_type'));
}
/**
* @OA\Get(
* path="/api/mobile/other/banner",
* tags={"小程序-其他"},
* summary="获取banner",
* @OA\Parameter(name="position", in="query", @OA\Schema(type="integer"), required=true, description="位置1首页"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function banner()
{
$position = request('position', 1);
$config = Banner::with('image')->where(function ($query) use ($position) {
if (isset($position)) {
$query->where('position', $position);
}
})->orderBy('sort')->get();
return $this->success($config);
}
/**
* @OA\Get(
* path="/api/mobile/other/company",
* tags={"小程序-其他"},
* summary="公司搜索",
* @OA\Parameter(name="company_name", in="query", @OA\Schema(type="integer"), required=true, description="公司名字"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function company()
{
$all = \request()->all();
$messages = [
'company_name.required' => '公司名称必填',
];
$validator = Validator::make($all, [
'company_name' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$YuanheRepository = new YuanheRepository();
// $result = $YuanheRepository->search(['keyword' => $all['company_name']]);
$result = "[{\"createBy\":null,\"createDt\":null,\"updateBy\":null,\"updateDt\":null,\"creditCode\":\"91320594MA7F0G9W6A\",\"keyNo\":\"db5ppy5kbsprbbhjbjlarvmvphjhp3mrkv\",\"name\":\"苏州元瞰科技有限公司\",\"operName\":\"刘杰杰\",\"startDate\":\"2021-12-21\",\"status\":\"注销\"},{\"createBy\":null,\"createDt\":null,\"updateBy\":null,\"updateDt\":null,\"creditCode\":\"91310118MA1JMYNK9K\",\"keyNo\":\"ak95iy5s3rhmsasj5kepiijjimh95jl3b5\",\"name\":\"上海元橡瞰行科技有限公司\",\"operName\":\"任杰\",\"startDate\":\"2019-03-19\",\"status\":\"存续\"},{\"createBy\":null,\"createDt\":null,\"updateBy\":null,\"updateDt\":null,\"creditCode\":\"91110108MAERYU4T74\",\"keyNo\":\"fs3ijyphhk59kbhjblbsj35priklbla5vh\",\"name\":\"北京元字瞰科技有限公司\",\"operName\":\"宋明龙\",\"startDate\":\"2025-08-22\",\"status\":\"存续\"},{\"createBy\":null,\"createDt\":null,\"updateBy\":null,\"updateDt\":null,\"creditCode\":\"\",\"keyNo\":\"13ivbylse5l5mjkjar9pbhkm9vpsjiksjl\",\"name\":\"上海瞰元信息科技有限公司\",\"operName\":\"刘勇\",\"startDate\":\"2005-06-30\",\"status\":\"吊销\"},{\"createBy\":null,\"createDt\":null,\"updateBy\":null,\"updateDt\":null,\"creditCode\":\"91330100796660273A\",\"keyNo\":\"f3aaeyiss99ev5hjbipkv9eaerkih99bbs\",\"name\":\"杭州瞰元信息科技有限公司\",\"operName\":\"曾铁农\",\"startDate\":\"2006-12-22\",\"status\":\"吊销\"}]\"";
$result = json_decode($result, true);
if (!$result) {
return $this->fail([ResponseCode::ERROR_PARAMETER, '获取失败']);
}
return $this->success($result);
}
/**
* @OA\Get(
* path="/api/mobile/other/company-list",
* tags={"小程序-其他"},
* summary="公司列表",
* @OA\Parameter(name="company_name", in="query", @OA\Schema(type="string"), required=false, description="公司名字"),
* @OA\Parameter(name="company_longitude", in="query", @OA\Schema(type="string"), required=false, description="经度"),
* @OA\Parameter(name="company_latitude", in="query", @OA\Schema(type="string"), required=false, description="纬度"),
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function companyList()
{
$all = \request()->all();
$messages = [
'company_longitude.required' => '经度必填',
'company_latitude.required' => '纬度必填',
];
$validator = Validator::make($all, [
'company_longitude' => 'required',
'company_latitude' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$longitude = floatval($all['company_longitude']);
$latitude = floatval($all['company_latitude']);
$pageSize = intval($all['page_size'] ?? 10);
$page = intval($all['page'] ?? 1);
$sortName = $all['sort_name'] ?? 'distance';
$sortType = $all['sort_type'] ?? 'asc';
// 先构建基础查询不使用SQL计算距离
$query = Company::select('*')
->where(function ($query) use ($all) {
if (isset($all['company_name'])) {
$query->where('company_name', 'like', '%' . $all['company_name'] . '%');
}
})
->whereNotNull('company_longitude')
->whereNotNull('company_latitude')
->where('company_longitude', '!=', '')
->where('company_latitude', '!=', '')
->whereRaw('company_longitude REGEXP \'^-?[0-9]+\.?[0-9]*$\'')
->whereRaw('company_latitude REGEXP \'^-?[0-9]+\.?[0-9]*$\'')
->whereRaw('CAST(company_longitude AS DECIMAL(10,8)) BETWEEN -180 AND 180')
->whereRaw('CAST(company_latitude AS DECIMAL(10,8)) BETWEEN -90 AND 90')
->whereHas('users', function ($query) {
$query->where('is_schoolmate', 1);
})
->with([
'users' => function ($query) {
$query->with([
'courseSigns' => function ($query) {
$query->with('course')->orderBy('fee_status', 'desc');
}
]);
}
]);
// 添加SQL调试信息
\Log::info('SQL Query', [
'sql' => $query->toSql(),
'bindings' => $query->getBindings()
]);
// 添加参数调试信息
\Log::info('Distance Parameters', [
'latitude' => $latitude,
'longitude' => $longitude,
'latitude_type' => gettype($latitude),
'longitude_type' => gettype($longitude)
]);
// 根据排序字段进行排序
if ($sortName !== 'distance') {
$query->orderBy($sortName, $sortType);
}
$result = $query->paginate($pageSize, ['*'], 'page', $page);
// 在PHP中计算距离
foreach ($result->items() as $company) {
$companyLat = floatval($company->company_latitude);
$companyLon = floatval($company->company_longitude);
// 使用Haversine公式计算距离
$distance = 6371 * acos(
cos(deg2rad($latitude)) * cos(deg2rad($companyLat)) *
cos(deg2rad($companyLon) - deg2rad($longitude)) +
sin(deg2rad($latitude)) * sin(deg2rad($companyLat))
);
$company->distance = $distance;
}
// 如果按距离排序在PHP中排序
if ($sortName === 'distance') {
$items = $result->items();
usort($items, function ($a, $b) {
return $a->distance <=> $b->distance;
});
// 重新设置分页数据
$result->setCollection(collect($items));
}
// 添加调试信息
\Log::info('Distance calculation debug', [
'input_lat' => $latitude,
'input_lon' => $longitude,
'target_lat' => '31.326810',
'target_lon' => '120.421618',
'expected_distance' => 13.22,
'actual_distance' => $result->first() ? $result->first()->distance : 'N/A',
'calculation_method' => 'PHP Haversine'
]);
return $this->success($result);
}
}