Index.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\api\controller\inspection;
  3. use app\api\controller\inspection\Base;
  4. use app\common\Service\InspectService;
  5. use app\common\Enum\StatusEnum;
  6. /**
  7. * 示例接口
  8. */
  9. class Index extends Base
  10. {
  11. /**
  12. * 获取验货选项列表
  13. */
  14. public function getOptions()
  15. {
  16. $params = [
  17. 'status' => StatusEnum::ENABLED,
  18. ];
  19. $options = InspectService::getInspectionItems($params);
  20. $this->success('获取成功', $options);
  21. }
  22. /**
  23. * 提交验货任务
  24. *
  25. * @ApiTitle (提交验货任务)
  26. * @ApiSummary (提交验货任务,包含验货结果和照片信息)
  27. * @ApiMethod (POST)
  28. * @ApiRoute (/api/inspection/index/submitTask)
  29. * @ApiParams (name="order_id", type="integer", required=true, description="订单ID")
  30. * @ApiParams (name="inspector_id", type="integer", required=true, description="验货员ID")
  31. * @ApiParams (name="remark", type="string", required=false, description="备注信息")
  32. * @ApiParams (name="images", type="string", required=false, description="任务图片,多张用逗号分隔")
  33. * @ApiParams (name="start_time", type="integer", required=false, description="任务开始时间戳")
  34. * @ApiParams (name="end_time", type="integer", required=false, description="任务结束时间戳")
  35. * @ApiParams (name="task_id", type="integer", required=false, description="任务ID,更新时传入")
  36. * @ApiParams (name="results", type="array", required=true, description="验货结果数组")
  37. * @ApiParams (name="results.item_id", type="integer", required=true, description="检查项ID")
  38. * @ApiParams (name="results.name", type="string", required=false, description="检查项名称")
  39. * @ApiParams (name="results.is_qualified", type="integer", required=true, description="是否合格 0:否 1:是")
  40. * @ApiParams (name="results.remark", type="string", required=false, description="不合格原因")
  41. * @ApiParams (name="results.photos", type="array", required=false, description="照片数组")
  42. * @ApiParams (name="results.photos.photo_url", type="string", required=true, description="照片URL")
  43. * @ApiReturnParams (name="code", type="integer", required=true, sample="1")
  44. * @ApiReturnParams (name="msg", type="string", required=true, sample="验货任务提交成功")
  45. * @ApiReturnParams (name="data", type="object", description="返回数据")
  46. * @ApiReturn ({
  47. 'code':'1',
  48. 'msg':'验货任务提交成功',
  49. 'data': {
  50. 'task_id': 123
  51. }
  52. })
  53. */
  54. public function submitTask()
  55. {
  56. $params = $this->request->post();
  57. // 验证必要参数
  58. if (empty($params['order_id'])) {
  59. $this->error('订单ID不能为空');
  60. }
  61. $params['inspector_id'] = $this->auth->id;
  62. if (empty($params['results']) || !is_array($params['results'])) {
  63. $this->error('验货结果不能为空');
  64. }
  65. // 组装任务数据
  66. $taskData = [
  67. 'order_id' => $params['order_id'],
  68. 'inspector_id' => $params['inspector_id'],
  69. 'remark' => $params['remark'] ?? '',
  70. 'images' => $params['images'] ?? '',
  71. 'start_time' => $params['start_time'] ?? time(),
  72. 'end_time' => $params['end_time'] ?? time(),
  73. 'task_status' => 1, // 进行中
  74. 'status' => 1 // 开启
  75. ];
  76. // 如果是更新任务,传入task_id
  77. if (!empty($params['task_id'])) {
  78. $taskData['task_id'] = $params['task_id'];
  79. }
  80. // 组装结果数据
  81. $resultData = [];
  82. foreach ($params['results'] as $result) {
  83. if (empty($result['item_id'])) {
  84. $this->error('检查项ID不能为空');
  85. }
  86. $resultItem = [
  87. 'item_id' => $result['item_id'],
  88. 'name' => $result['name'] ?? '',
  89. 'is_qualified' => $result['is_qualified'] ?? 1,
  90. 'remark' => $result['remark'] ?? '',
  91. 'photos' => $result['photos'] ?? []
  92. ];
  93. $resultData[] = $resultItem;
  94. }
  95. // 调用服务提交验货任务
  96. $result = InspectService::submitInspectionTask($taskData, $resultData);
  97. if ($result['code'] == 1) {
  98. $this->success($result['msg'], $result['data']);
  99. } else {
  100. $this->error($result['msg']);
  101. }
  102. }
  103. //
  104. }