文件
mashangyoujiu/Application/RiderApi/Controller/VehicleController.class.php

350 行
12 KiB
PHP

<?php
namespace RiderApi\Controller;
/**
* 骑手车辆管理控制器
*/
class VehicleController extends BaseController {
/**
* 获取骑手车辆列表
*/
public function getList() {
try {
$vehicleModel = M('rider_vehicle');
$list = $vehicleModel->where(array(
'rider_id' => $this->riderId
))->order('status DESC, add_time DESC')->select();
// 格式化数据
foreach ($list as &$item) {
$item['type'] = $item['vehicle_type']; // 前端使用type字段
$item['vehicle_photo_url'] = !empty($item['vehicle_photo']) ?
'http://' . $_SERVER['HTTP_HOST'] . '/uploads/' . $item['vehicle_photo'] : '';
$item['driving_license_url'] = !empty($item['driving_license']) ?
'http://' . $_SERVER['HTTP_HOST'] . '/uploads/' . $item['driving_license'] : '';
$item['insurance_photo_url'] = !empty($item['insurance_photo']) ?
'http://' . $_SERVER['HTTP_HOST'] . '/uploads/' . $item['insurance_photo'] : '';
}
$this->ajaxReturn(array(
'code' => 1,
'msg' => '获取成功',
'data' => $list ?: array()
));
} catch (\Exception $e) {
$this->ajaxReturn(array(
'code' => 0,
'msg' => '获取失败:' . $e->getMessage()
));
}
}
/**
* 添加车辆
*/
public function add() {
try {
// 获取JSON参数
$input = json_decode(file_get_contents('php://input'), true);
$vehicleType = isset($input['type']) ? trim($input['type']) : '';
$plateNumber = isset($input['plate_number']) ? trim($input['plate_number']) : '';
$brand = isset($input['brand']) ? trim($input['brand']) : '';
$model = isset($input['model']) ? trim($input['model']) : '';
$color = isset($input['color']) ? trim($input['color']) : '';
// 验证必填参数
if (empty($vehicleType)) {
$this->ajaxReturn(array('code' => 0, 'msg' => '请选择车辆类型'));
}
// 验证车辆类型
$allowedTypes = array('bicycle', 'motorcycle', 'car');
if (!in_array($vehicleType, $allowedTypes)) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆类型不正确'));
}
$vehicleModel = M('rider_vehicle');
// 检查是否已有使用中的车辆,如果没有则设为使用中
$currentVehicle = $vehicleModel->where(array(
'rider_id' => $this->riderId,
'status' => 1
))->find();
$status = $currentVehicle ? 0 : 1; // 如果没有使用中的车辆,新添加的设为使用中
$data = array(
'rider_id' => $this->riderId,
'vehicle_type' => $vehicleType,
'plate_number' => $plateNumber,
'brand' => $brand,
'model' => $model,
'color' => $color,
'status' => $status,
'auth_status' => 0, // 待审核
'add_time' => date('Y-m-d H:i:s'),
'update_time' => date('Y-m-d H:i:s')
);
$result = $vehicleModel->add($data);
if ($result) {
$this->ajaxReturn(array(
'code' => 1,
'msg' => '添加成功',
'data' => array('id' => $result)
));
} else {
$this->ajaxReturn(array('code' => 0, 'msg' => '添加失败'));
}
} catch (\Exception $e) {
$this->ajaxReturn(array(
'code' => 0,
'msg' => '添加失败:' . $e->getMessage()
));
}
}
/**
* 更新车辆信息
*/
public function update() {
try {
// 获取JSON参数
$input = json_decode(file_get_contents('php://input'), true);
$id = isset($input['id']) ? intval($input['id']) : 0;
$vehicleType = isset($input['type']) ? trim($input['type']) : '';
$plateNumber = isset($input['plate_number']) ? trim($input['plate_number']) : '';
$brand = isset($input['brand']) ? trim($input['brand']) : '';
$model = isset($input['model']) ? trim($input['model']) : '';
$color = isset($input['color']) ? trim($input['color']) : '';
// 验证必填参数
if (empty($id)) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆ID不能为空'));
}
if (empty($vehicleType)) {
$this->ajaxReturn(array('code' => 0, 'msg' => '请选择车辆类型'));
}
// 验证车辆类型
$allowedTypes = array('bicycle', 'motorcycle', 'car');
if (!in_array($vehicleType, $allowedTypes)) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆类型不正确'));
}
$vehicleModel = M('rider_vehicle');
// 验证车辆是否属于当前骑手
$vehicle = $vehicleModel->where(array(
'id' => $id,
'rider_id' => $this->riderId
))->find();
if (!$vehicle) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆不存在'));
}
$data = array(
'vehicle_type' => $vehicleType,
'plate_number' => $plateNumber,
'brand' => $brand,
'model' => $model,
'color' => $color,
'update_time' => date('Y-m-d H:i:s')
);
$result = $vehicleModel->where(array(
'id' => $id,
'rider_id' => $this->riderId
))->save($data);
if ($result !== false) {
$this->ajaxReturn(array(
'code' => 1,
'msg' => '更新成功'
));
} else {
$this->ajaxReturn(array('code' => 0, 'msg' => '更新失败'));
}
} catch (\Exception $e) {
$this->ajaxReturn(array(
'code' => 0,
'msg' => '更新失败:' . $e->getMessage()
));
}
}
/**
* 删除车辆
*/
public function delete() {
try {
// 获取JSON参数
$input = json_decode(file_get_contents('php://input'), true);
$id = isset($input['id']) ? intval($input['id']) : 0;
// 验证必填参数
if (empty($id)) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆ID不能为空'));
}
$vehicleModel = M('rider_vehicle');
// 验证车辆是否属于当前骑手
$vehicle = $vehicleModel->where(array(
'id' => $id,
'rider_id' => $this->riderId
))->find();
if (!$vehicle) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆不存在'));
}
// 检查是否为使用中的车辆
if ($vehicle['status'] == 1) {
$this->ajaxReturn(array('code' => 0, 'msg' => '当前使用的车辆不能删除'));
}
$result = $vehicleModel->where(array(
'id' => $id,
'rider_id' => $this->riderId
))->delete();
if ($result) {
$this->ajaxReturn(array(
'code' => 1,
'msg' => '删除成功'
));
} else {
$this->ajaxReturn(array('code' => 0, 'msg' => '删除失败'));
}
} catch (\Exception $e) {
$this->ajaxReturn(array(
'code' => 0,
'msg' => '删除失败:' . $e->getMessage()
));
}
}
/**
* 设置当前使用车辆
*/
public function setCurrent() {
try {
// 获取JSON参数
$input = json_decode(file_get_contents('php://input'), true);
$id = isset($input['id']) ? intval($input['id']) : 0;
// 验证必填参数
if (empty($id)) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆ID不能为空'));
}
$vehicleModel = M('rider_vehicle');
// 验证车辆是否属于当前骑手
$vehicle = $vehicleModel->where(array(
'id' => $id,
'rider_id' => $this->riderId
))->find();
if (!$vehicle) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆不存在'));
}
// 开启事务
$vehicleModel->startTrans();
try {
// 先将该骑手的所有车辆设为未使用
$vehicleModel->where(array(
'rider_id' => $this->riderId
))->save(array(
'status' => 0,
'update_time' => date('Y-m-d H:i:s')
));
// 设置指定车辆为使用中
$vehicleModel->where(array(
'id' => $id,
'rider_id' => $this->riderId
))->save(array(
'status' => 1,
'update_time' => date('Y-m-d H:i:s')
));
$vehicleModel->commit();
$this->ajaxReturn(array(
'code' => 1,
'msg' => '设置成功'
));
} catch (\Exception $e) {
$vehicleModel->rollback();
throw $e;
}
} catch (\Exception $e) {
$this->ajaxReturn(array(
'code' => 0,
'msg' => '设置失败:' . $e->getMessage()
));
}
}
/**
* 获取车辆详情
*/
public function getDetail() {
try {
$id = $this->getRequestParam('id', 0, 'intval');
if (empty($id)) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆ID不能为空'));
}
$vehicleModel = M('rider_vehicle');
$vehicle = $vehicleModel->where(array(
'id' => $id,
'rider_id' => $this->riderId
))->find();
if (!$vehicle) {
$this->ajaxReturn(array('code' => 0, 'msg' => '车辆不存在'));
}
// 格式化数据
$vehicle['type'] = $vehicle['vehicle_type']; // 前端使用type字段
$vehicle['vehicle_photo_url'] = !empty($vehicle['vehicle_photo']) ?
'http://' . $_SERVER['HTTP_HOST'] . '/uploads/' . $vehicle['vehicle_photo'] : '';
$vehicle['driving_license_url'] = !empty($vehicle['driving_license']) ?
'http://' . $_SERVER['HTTP_HOST'] . '/uploads/' . $vehicle['driving_license'] : '';
$vehicle['insurance_photo_url'] = !empty($vehicle['insurance_photo']) ?
'http://' . $_SERVER['HTTP_HOST'] . '/uploads/' . $vehicle['insurance_photo'] : '';
$this->ajaxReturn(array(
'code' => 1,
'msg' => '获取成功',
'data' => $vehicle
));
} catch (\Exception $e) {
$this->ajaxReturn(array(
'code' => 0,
'msg' => '获取失败:' . $e->getMessage()
));
}
}
}