request->action()); if ($action === 'docs') { return; } $plain = FactoryApiKey::fromRequest($this->request); $factory = FactoryApiKey::findFactory($plain); if (!$factory) { $this->error('API Key 无效', null, 401); } FactoryContext::bindFactory((int)$factory->id); } /** * 连通性 */ public function ping() { $this->success('ok', [ 'factory_id' => FactoryContext::id(), 'time' => date('c'), ]); } /** * GET 按编码查询;POST 新增或按编码更新产品 */ public function product() { if ($this->request->isGet()) { $code = trim((string)$this->request->request('code', '')); $query = (new Product())->order('id desc'); if ($code !== '') { $row = $query->where('code', $code)->with('processes')->find(); if (!$row) { $this->error('产品不存在', null, 404); } $this->success('', $row); } $list = $query->paginate($this->request->request('limit/d', 20)); $this->success('', ['total' => $list->total(), 'rows' => $list->items()]); } $payload = $this->jsonBody(); try { $row = BizIngest::saveProduct($payload, 0); } catch (\Exception $e) { $this->error($e->getMessage()); } $row = Product::with('processes')->find($row->id); $this->success('已保存', $row); } /** * GET 按工单号查询;POST 新增或按工单号更新 */ public function workorder() { if ($this->request->isGet()) { $no = trim((string)$this->request->request('no', '')); $query = (new Workorder())->order('id desc'); if ($no !== '') { $row = $query->where('no', $no)->with('processes')->find(); if (!$row) { $this->error('工单不存在', null, 404); } $this->success('', $row); } $list = $query->paginate($this->request->request('limit/d', 20)); $this->success('', ['total' => $list->total(), 'rows' => $list->items()]); } $payload = $this->jsonBody(); try { $row = BizIngest::saveWorkorder($payload, 0); } catch (\Exception $e) { $this->error($e->getMessage()); } $row = Workorder::with('processes')->find($row->id); $this->success('已保存', $row); } /** * 字段说明(无需 Key) */ public function docs() { $this->success('', [ 'auth' => [ 'header' => 'X-Api-Key: psk_xxxxxxxx', 'alt' => 'Authorization: Bearer psk_xxxxxxxx', ], 'base' => '/api/ps/open', 'product' => [ 'method' => 'POST /api/ps/open/product', 'fields' => [ 'code' => '产品编号,必填,本厂内唯一,存在则更新', 'name' => '产品名称', 'spec' => '规格', 'unit' => '单位,默认件', 'remark' => '备注', 'processes' => [ ['code' => '工艺编号', 'seq' => 1, 'name' => '工序名', 'workstation' => '工位', 'standard_time' => '工时', 'qc_points' => '质检要点'], ], ], ], 'workorder' => [ 'method' => 'POST /api/ps/open/workorder', 'fields' => [ 'no' => '工单号,必填,本厂内唯一,存在则更新', 'product_code' => '产品编号,可选;填写则关联已有产品或自动建档', 'product_name' => '产品名称。不选产品时必填;选了产品可带出', 'spec' => '规格。可选;选了产品可带出', 'batch_no' => '批次', 'customer' => '客户', 'plan_date' => '计划日期 YYYY-MM-DD', 'remark' => '备注', 'processes' => [ ['code' => '工艺编号', 'seq' => 1, 'name' => '工序名'], ], ], ], ]); } /** * @return array */ protected function jsonBody() { $payload = $this->request->post(); if (!$payload) { $raw = (string)file_get_contents('php://input'); $decoded = json_decode($raw, true); if (is_array($decoded)) { $payload = $decoded; } } return is_array($payload) ? $payload : []; } }