logistics/app/api/controller/LogisticsController.php

227 lines
7.8 KiB
PHP
Raw Normal View History

2023-08-07 16:12:28 +08:00
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\controller;
2023-08-09 11:18:21 +08:00
use app\api\logic\LogisticsLogic;
2023-08-07 16:12:28 +08:00
use think\facade\Db;
/**
* 文章管理
* Class ArticleController
* @package app\api\controller
*/
class LogisticsController extends BaseApiController
{
public array $notNeedLogin = ['courierLogisticsList','logisticsDetail','logisticsCreate','courierTakeGoods','courierCompleteDelivery','userConfirmReceipt','userCancelOrder'];
/*
* 获取配送员物流信息列表
* @method get
* @param int $courier_id 配送员id
* @param int $status 物流状态
* @param int $page_size 每页数量
* @param int $page_num 页码
* @return \think\response\Json
*/
public function courierLogisticsList(): \think\response\Json
{
//获取参数
2023-08-09 11:18:21 +08:00
$params = $this->request->get(['courier_id','status','page_size','page_num','keywords']);
//验证参数
if(empty($params['courier_id'])) return $this->fail('参数错误');
if(empty($params['keywords'])) $params['keywords'] = '';
//返回数据
return $this->data(LogisticsLogic::list($params));
2023-08-07 16:12:28 +08:00
}
/*
* 获取物流信息详情
* @method get
* @param int $logistics_id 物流id
* @return \think\response\Json
*/
public function logisticsDetail(): \think\response\Json
{
//获取参数
2023-08-09 11:18:21 +08:00
$logistics_id = $this->request->get('logistics_id');
//验证参数
if(empty($logistics_id)) return $this->fail('参数错误');
2023-08-07 16:12:28 +08:00
//返回数据
2023-08-09 11:18:21 +08:00
return $this->data(LogisticsLogic::detail($logistics_id));
2023-08-07 16:12:28 +08:00
}
/*
* 生成物流信息
* @method post
* @param int $order_id 订单id
* @param string $order_sn 订单编号
* @return \think\response\Json
*/
public function logisticsCreate(): \think\response\Json
{
//获取参数
2023-08-09 11:18:21 +08:00
$params = $this->request->post(['order_id','order_sn']);
//验证参数
if(empty($params['order_id']) || empty($params['order_sn'])) return $this->fail('参数错误');
//生成数据
$result = LogisticsLogic::create($params);
//返回数据
return $result['code'] ==1 ? $this->success('生成成功') : $this->fail($result['msg']);
2023-08-07 16:12:28 +08:00
}
/*
* 配送员提取商品
* @method post
* @param int $logistics_id 物流id
* @return \think\response\Json
*/
public function courierTakeGoods(): \think\response\Json
{
//获取参数
2023-08-09 14:41:05 +08:00
$params = $this->request->post(['logistics_id','user_id']);
//验证参数
if(empty($params['logistics_id']) || empty($params['user_id'])) return $this->fail('参数错误');
//提取商品
$result = LogisticsLogic::takeGoods($params);
//返回数据
return $result['code'] ==1 ? $this->success('提取成功') : $this->fail($result['msg']);
2023-08-07 16:12:28 +08:00
}
/*
* 配送员完成配送
* @method post
* @param int $logistics_id 物流id
* @return \think\response\Json
*/
public function courierCompleteDelivery(): \think\response\Json
{
//获取参数
2023-08-09 14:41:05 +08:00
$params = $this->request->post(['logistics_id','user_id']);
//验证参数
if(empty($params['logistics_id']) || empty($params['user_id'])) return $this->fail('参数错误');
//完成配送
$result = LogisticsLogic::doneDelivery($params);
//返回数据
return $result['code'] ==1 ? $this->success('配送完成') : $this->fail($result['msg']);
2023-08-07 16:12:28 +08:00
}
/*
* 用户确认收货
* @method post
* @param int $logistics_id 物流id
* @return \think\response\Json
*/
public function userConfirmReceipt(): \think\response\Json
{
//获取参数
$logistics_id = input('logistics_id', 0, 'intval');
2023-08-07 17:43:33 +08:00
$user_id = input('user_id', 0, 'intval');
2023-08-07 16:12:28 +08:00
//获取物流信息
2023-08-07 17:43:33 +08:00
$logistics = Db::name('logistics')->where('id', $logistics_id)->where('user_id',$user_id)->find();
2023-08-07 16:12:28 +08:00
if (!$logistics) return $this->fail('物流信息不存在');
if ($logistics['status'] == 4) return $this->fail('不可更改物流状态');
//设置记录信息
$record = [
'lst_id' => $logistics['id'],
'type' => 1,
'user_name' => $logistics['user_name'],
'user_phone' => $logistics['user_phone'],
'content' => '已确认收货',
'create_time' => time(),
];
//更改物流信息状态
$res = $this->logisticsUpdate($logistics_id, 3,$record);
if($res){
return $this->success('操作成功');
}else{
return $this->fail('操作失败');
}
}
/*
* 用户取消订单
* @method post
* @param int $logistics_id 物流id
* @return \think\response\Json
*/
public function userCancelOrder(): \think\response\Json
{
//获取参数
$logistics_id = input('logistics_id', 0, 'intval');
2023-08-07 17:43:33 +08:00
$user_id = input('user_id', 0, 'intval');
2023-08-07 16:12:28 +08:00
//获取物流信息
2023-08-07 17:43:33 +08:00
$logistics = Db::name('logistics')->where('id', $logistics_id)->where('user_id',$user_id)->find();
2023-08-07 16:12:28 +08:00
if (!$logistics) return $this->fail('物流信息不存在');
if ($logistics['status'] == 2 || $logistics['status'] == 3 || $logistics['status'] == 4) return $this->fail('订单已完成不能取消订单');
//设置记录信息
$record = [
'lst_id' => $logistics['id'],
'type' => 1,
'user_name' => $logistics['user_name'],
'user_phone' => $logistics['user_phone'],
'content' => '已取消订单',
'create_time' => time(),
];
//更改物流信息状态
$res = $this->logisticsUpdate($logistics_id, 4,$record);
if($res){
return $this->success('操作成功', [], 1, 200);
}else{
return $this->fail('操作失败', [], 0, 200);
}
}
/*
* 更新物流信息
* @method post
* @param int $logistics_id 物流id
* @return bool
*/
private function logisticsUpdate($id,$status,$data):bool
{
if(empty($id) || empty($status) || empty($data)) return false;
Db::startTrans();
try {
2023-08-09 11:18:21 +08:00
$update = [
'status' => $status,
'update_time' => time(),
];
switch($status){
case 1:
$update['qh_time'] = time();
break;
case 2:
$update['pc_time'] = time();
break;
case 3:
$update['sh_time'] = time();
break;
case 4:
$update['qx_time'] = time();
break;
default:
break;
}
Db::name('logistics')->where('id', $id)->update($update);
2023-08-07 16:12:28 +08:00
Db::name('logistics_record')->insert($data);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
return false;
}
}
}