增加物流信息对外接口

This commit is contained in:
weiz 2023-11-08 14:12:31 +08:00
parent b7bf95ee4b
commit 87cf5fc481

View File

@ -0,0 +1,103 @@
<?php
namespace app\common\controller;
use app\common\model\logistics\Logistics;
use app\common\model\logistics\LogisticsRecord;
use app\common\model\logistics\Product;
class LogisticController extends BaseLikeAdminController
{
public function lists(): \think\response\Json
{
$params = $this->request->post(['order_sn','shop_name','user_name','courier_name','status','page_no','page_size']);
if(empty($params['page_no']) || empty($params['page_size'])){
return $this->fail('缺少必要参数');
}
$where = [];
foreach($params as $k => $v) {
if($k=='page_no' || $k=='page_size'){
continue;
}
if($k != 'status'){
$where[] = [$k,'like','%'.$v.'%'];
}else{
$where[] = [$k,'=',$v];
}
}
$data = Logistics::field(['id','order_id','order_sn','shop_name','shop_phone','receiver_name','receiver_phone','receiver_address','courier_name','courier_company','status','qh_time','ps_time','qx_time','create_time'])
->where($where)
->order(['id' => 'desc'])
->page($params['page_no'],$params['page_size'])
->select()
->each(function ($item){
$item['status_name'] = $item->status_name;
$product_count = 0;
//获取产品信息
$item['products'] = Product::field('product_num,cart_info')->where('order_id', $item['order_id'])->select()->each(function($pro_item) use(&$product_count){
$pro_item['cart_info'] = json_decode($pro_item['cart_info'], true);
$pro_item['goods_name'] = $pro_item['cart_info']['product']['store_name'];
$pro_item['goods_unit'] = $pro_item['cart_info']['product']['unit_name'];
$product_count += $pro_item['product_num'];
unset($pro_item['cart_info']);
return $pro_item;
});
$item['product_count'] = $product_count;
return $item;
})
->toArray();
$count = Logistics::field(['id'])->where($where)->count();
$result = [
'lists'=>$data,
'count' => $count,
'page_no' => $params['page_no'],
'page_size' => $params['page_size']
];
return $this->success('请求成功',$result);
}
public function detail(): \think\response\Json
{
$params = $this->request->post(['id']);
if(empty($params['id'])){
return $this->fail('参数错误');
}
$logistics = Logistics::findOrEmpty($params['id']);
$logistics['status_name'] = $logistics->status_name;
//获取商品信息
$product_count = 0;
$product = Product::field('product_num,cart_info')->where('order_id', $logistics['order_id'])->select()->each(function($pro_item) use(&$product_count){
$pro_item['cart_info'] = json_decode($pro_item['cart_info'], true);
$pro_item['goods_name'] = $pro_item['cart_info']['product']['store_name'];
$pro_item['goods_unit'] = $pro_item['cart_info']['product']['unit_name'];
$pro_item['goods_pic'] = $pro_item['cart_info']['product']['image'];
$pro_item['goods_price'] = $pro_item['cart_info']['product']['price'];
$pro_item['goods_sku'] = $pro_item['cart_info']['productAttr']['sku'];
$pro_item['goods_total_price'] = $pro_item['cart_info']['product']['price'] * $pro_item['product_num'];
$product_count += $pro_item['product_num'];
unset($pro_item['cart_info']);
return $pro_item;
})->toArray();
$logistics['product'] = $product;
//获取物流记录
$records = LogisticsRecord::field('type,user_name,content,create_time')
->where('lst_id', $logistics['id'])->order('create_time asc')->select()->each(function($red_item){
switch ($red_item['type']) {
case 1:
$red_item['content'] = '用户'.$red_item['user_name'].$red_item['content'];
break;
case 2:
$red_item['content'] = '配送员'.$red_item['user_name'].$red_item['content'];
break;
case 3:
$red_item['content'] = '平台'.$red_item['user_name'].$red_item['content'];
break;
default:
$red_item['content'] = '未知';
}
unset($red_item['type'], $red_item['user_name']);
})->toArray();
$logistics['records'] = $records;
//返回数据
return $this->success('请求成功',$logistics->toArray());
}
}