b89ac48dd8
- PublicApiController 新增 staffList():通过 user_roles 关联筛选业务人员 - 返回 id/name/sn/phone/email,支持 keyword 模糊匹配,不分页全量返回 - 路由组新增 /common/public/staff/list - .gitignore 忽略 docs/*.sql(数据导出文件,不入库) Co-Authored-By: Claude <noreply@anthropic.com>
153 lines
5.1 KiB
PHP
153 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace app\common\controller;
|
|
|
|
use app\common\model\StockOrders;
|
|
use app\common\model\StockOrderItems;
|
|
use app\common\model\User;
|
|
use support\Request;
|
|
|
|
class PublicApiController extends BaseController
|
|
{
|
|
// 业务人员固定角色ID
|
|
protected const STAFF_ROLE_ID = 2;
|
|
|
|
protected $noNeedLogin = ['piList', 'vinList', 'staffList'];
|
|
protected $noNeedHeaderData = ['piList', 'vinList', 'staffList'];
|
|
|
|
/**
|
|
* PI 号列表(含关联的 VIN 和车型名称)
|
|
* GET /common/public/pi/list
|
|
*/
|
|
public function piList(Request $request)
|
|
{
|
|
try {
|
|
$page = (int)($request->input('page', 1));
|
|
$pageSize = (int)($request->input('page_size', 20));
|
|
$piNo = $request->input('pi_no', '');
|
|
|
|
if ($page < 1) $page = 1;
|
|
if ($pageSize < 1 || $pageSize > 200) $pageSize = 20;
|
|
|
|
$query = StockOrders::where('approval_status', 2)
|
|
->whereNotNull('pi_no')
|
|
->where('pi_no', '<>', '');
|
|
|
|
if ($piNo !== '') {
|
|
$query->where('pi_no', 'like', '%' . addcslashes($piNo, '%_') . '%');
|
|
}
|
|
|
|
$query->with(['items' => function ($q) {
|
|
$q->select('id', 'order_id', 'vin', 'model_snapshot');
|
|
}]);
|
|
|
|
$paginator = $query->orderBy('id', 'desc')
|
|
->paginate($pageSize, ['id', 'pi_no'], 'page', $page);
|
|
|
|
$data = $paginator->getCollection()->map(function ($order) {
|
|
return [
|
|
'pi_no' => $order->pi_no,
|
|
'vins' => $order->items->pluck('vin')->filter()->values()->toArray(),
|
|
'model_names' => $order->items->pluck('model_snapshot')->filter()->values()->toArray(),
|
|
];
|
|
})->toArray();
|
|
|
|
return $this->success([
|
|
'total' => $paginator->total(),
|
|
'per_page' => $paginator->perPage(),
|
|
'current_page' => $paginator->currentPage(),
|
|
'data' => $data,
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
return $this->error('查询失败,请稍后重试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* VIN 号列表(含车型名称)
|
|
* GET /common/public/vin/list
|
|
*/
|
|
public function vinList(Request $request)
|
|
{
|
|
try {
|
|
$page = (int)($request->input('page', 1));
|
|
$pageSize = (int)($request->input('page_size', 20));
|
|
$vin = $request->input('vin', '');
|
|
|
|
if ($page < 1) $page = 1;
|
|
if ($pageSize < 1 || $pageSize > 200) $pageSize = 20;
|
|
|
|
$query = StockOrderItems::whereHas('order', function ($q) {
|
|
$q->where('approval_status', 2);
|
|
})->whereNotNull('vin')
|
|
->where('vin', '<>', '');
|
|
|
|
if ($vin !== '') {
|
|
$query->where('vin', 'like', '%' . addcslashes($vin, '%_') . '%');
|
|
}
|
|
|
|
$paginator = $query->orderBy('id', 'desc')
|
|
->paginate($pageSize, ['id', 'vin', 'model_snapshot'], 'page', $page);
|
|
|
|
$data = $paginator->getCollection()->map(function ($item) {
|
|
return [
|
|
'vin' => $item->vin,
|
|
'model_name' => $item->model_snapshot,
|
|
];
|
|
})->toArray();
|
|
|
|
return $this->success([
|
|
'total' => $paginator->total(),
|
|
'per_page' => $paginator->perPage(),
|
|
'current_page' => $paginator->currentPage(),
|
|
'data' => $data,
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
return $this->error('查询失败,请稍后重试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 业务人员列表(role_id=2,在职,全量返回)
|
|
* GET /common/public/staff/list
|
|
*/
|
|
public function staffList(Request $request)
|
|
{
|
|
try {
|
|
$keyword = $request->input('keyword', '');
|
|
|
|
// 通过 user_roles 关联,筛选指定角色的用户
|
|
$query = User::whereHas('roles', function ($q) {
|
|
$q->where('roles.id', static::STAFF_ROLE_ID);
|
|
})->where('status', 1);
|
|
|
|
if ($keyword !== '') {
|
|
$kw = '%' . addcslashes($keyword, '%_') . '%';
|
|
$query->where(function ($q) use ($kw) {
|
|
$q->where('name', 'like', $kw)
|
|
->orWhere('sn', 'like', $kw)
|
|
->orWhere('phone', 'like', $kw)
|
|
->orWhere('email', 'like', $kw);
|
|
});
|
|
}
|
|
|
|
$list = $query->orderBy('id', 'asc')
|
|
->get(['id', 'sn', 'name', 'phone', 'email']);
|
|
|
|
$data = $list->map(function ($user) {
|
|
return [
|
|
'id' => $user->id,
|
|
'name' => $user->name,
|
|
'sn' => $user->sn,
|
|
'phone' => $user->phone,
|
|
'email' => $user->email,
|
|
];
|
|
})->values()->toArray();
|
|
|
|
return $this->success($data);
|
|
} catch (\Throwable $e) {
|
|
return $this->error('查询失败,请稍后重试');
|
|
}
|
|
}
|
|
}
|