| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace app\api\controller\ps;
- use app\common\controller\Api;
- use app\common\library\ps\BizIngest;
- use app\common\library\ps\FactoryApiKey;
- use app\common\library\ps\FactoryContext;
- use app\common\model\ps\Product;
- use app\common\model\ps\Workorder;
- /**
- * 工厂 Open API:用 API Key 写入产品 / 工单,无需登录。
- *
- * 鉴权:请求头 X-Api-Key: psk_xxxx
- * 或 Authorization: Bearer psk_xxxx
- */
- class Open extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function _initialize()
- {
- parent::_initialize();
- $action = strtolower($this->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 : [];
- }
- }
|