first
This commit is contained in:
80
server-api/app/controller/admin/MaterialsController.php
Normal file
80
server-api/app/controller/admin/MaterialsController.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace app\controller\admin;
|
||||
|
||||
use app\support\MaterialTagService;
|
||||
use support\Request;
|
||||
|
||||
class MaterialsController
|
||||
{
|
||||
public function batches(Request $request)
|
||||
{
|
||||
return api_success([
|
||||
'list' => $this->service()->listBatches([
|
||||
'keyword' => $request->input('keyword', ''),
|
||||
'qr_url' => $request->input('qr_url', ''),
|
||||
'verify_code' => $request->input('verify_code', ''),
|
||||
'date_start' => $request->input('date_start', ''),
|
||||
'date_end' => $request->input('date_end', ''),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function detail(Request $request)
|
||||
{
|
||||
$id = (int)$request->input('id', 0);
|
||||
if ($id <= 0) {
|
||||
return api_error('物料批次 ID 不能为空', 422);
|
||||
}
|
||||
|
||||
try {
|
||||
return api_success($this->service()->detail($id, trim((string)$request->input('keyword', ''))));
|
||||
} catch (\RuntimeException $e) {
|
||||
return api_error($e->getMessage(), $e->getCode() ?: 404);
|
||||
}
|
||||
}
|
||||
|
||||
public function create(Request $request)
|
||||
{
|
||||
$count = (int)$request->input('count', 0);
|
||||
$remark = trim((string)$request->input('remark', ''));
|
||||
$adminId = (int)$request->header('x-admin-id', 0);
|
||||
$adminName = trim((string)$request->header('x-admin-name', ''));
|
||||
|
||||
try {
|
||||
return api_success($this->service()->createBatch($count, $remark, $adminId, $adminName), '物料批次已生成');
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return api_error($e->getMessage(), 422);
|
||||
} catch (\Throwable $e) {
|
||||
return api_error('物料批次生成失败', 500, ['detail' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
public function download(Request $request)
|
||||
{
|
||||
$id = (int)$request->input('id', 0);
|
||||
if ($id <= 0) {
|
||||
return api_error('物料批次 ID 不能为空', 422);
|
||||
}
|
||||
|
||||
try {
|
||||
$file = $this->service()->downloadBatch($id, $request);
|
||||
} catch (\RuntimeException $e) {
|
||||
return api_error($e->getMessage(), $e->getCode() ?: 500);
|
||||
} catch (\Throwable $e) {
|
||||
return api_error('物料批次下载失败', 500, ['detail' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
$filename = rawurlencode($file['filename']);
|
||||
return response($file['content'], 200, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Content-Disposition' => "attachment; filename=\"{$file['filename']}\"; filename*=UTF-8''{$filename}",
|
||||
'Cache-Control' => 'no-store, no-cache, must-revalidate',
|
||||
]);
|
||||
}
|
||||
|
||||
private function service(): MaterialTagService
|
||||
{
|
||||
return new MaterialTagService();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user