Open.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\api\controller\ps;
  3. use app\common\controller\Api;
  4. use app\common\library\ps\BizIngest;
  5. use app\common\library\ps\FactoryApiKey;
  6. use app\common\library\ps\FactoryContext;
  7. use app\common\model\ps\Product;
  8. use app\common\model\ps\Workorder;
  9. /**
  10. * 工厂 Open API:用 API Key 写入产品 / 工单,无需登录。
  11. *
  12. * 鉴权:请求头 X-Api-Key: psk_xxxx
  13. * 或 Authorization: Bearer psk_xxxx
  14. */
  15. class Open extends Api
  16. {
  17. protected $noNeedLogin = ['*'];
  18. protected $noNeedRight = ['*'];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $action = strtolower($this->request->action());
  23. if ($action === 'docs') {
  24. return;
  25. }
  26. $plain = FactoryApiKey::fromRequest($this->request);
  27. $factory = FactoryApiKey::findFactory($plain);
  28. if (!$factory) {
  29. $this->error('API Key 无效', null, 401);
  30. }
  31. FactoryContext::bindFactory((int)$factory->id);
  32. }
  33. /**
  34. * 连通性
  35. */
  36. public function ping()
  37. {
  38. $this->success('ok', [
  39. 'factory_id' => FactoryContext::id(),
  40. 'time' => date('c'),
  41. ]);
  42. }
  43. /**
  44. * GET 按编码查询;POST 新增或按编码更新产品
  45. */
  46. public function product()
  47. {
  48. if ($this->request->isGet()) {
  49. $code = trim((string)$this->request->request('code', ''));
  50. $query = (new Product())->order('id desc');
  51. if ($code !== '') {
  52. $row = $query->where('code', $code)->with('processes')->find();
  53. if (!$row) {
  54. $this->error('产品不存在', null, 404);
  55. }
  56. $this->success('', $row);
  57. }
  58. $list = $query->paginate($this->request->request('limit/d', 20));
  59. $this->success('', ['total' => $list->total(), 'rows' => $list->items()]);
  60. }
  61. $payload = $this->jsonBody();
  62. try {
  63. $row = BizIngest::saveProduct($payload, 0);
  64. } catch (\Exception $e) {
  65. $this->error($e->getMessage());
  66. }
  67. $row = Product::with('processes')->find($row->id);
  68. $this->success('已保存', $row);
  69. }
  70. /**
  71. * GET 按工单号查询;POST 新增或按工单号更新
  72. */
  73. public function workorder()
  74. {
  75. if ($this->request->isGet()) {
  76. $no = trim((string)$this->request->request('no', ''));
  77. $query = (new Workorder())->order('id desc');
  78. if ($no !== '') {
  79. $row = $query->where('no', $no)->with('processes')->find();
  80. if (!$row) {
  81. $this->error('工单不存在', null, 404);
  82. }
  83. $this->success('', $row);
  84. }
  85. $list = $query->paginate($this->request->request('limit/d', 20));
  86. $this->success('', ['total' => $list->total(), 'rows' => $list->items()]);
  87. }
  88. $payload = $this->jsonBody();
  89. try {
  90. $row = BizIngest::saveWorkorder($payload, 0);
  91. } catch (\Exception $e) {
  92. $this->error($e->getMessage());
  93. }
  94. $row = Workorder::with('processes')->find($row->id);
  95. $this->success('已保存', $row);
  96. }
  97. /**
  98. * 字段说明(无需 Key)
  99. */
  100. public function docs()
  101. {
  102. $this->success('', [
  103. 'auth' => [
  104. 'header' => 'X-Api-Key: psk_xxxxxxxx',
  105. 'alt' => 'Authorization: Bearer psk_xxxxxxxx',
  106. ],
  107. 'base' => '/api/ps/open',
  108. 'product' => [
  109. 'method' => 'POST /api/ps/open/product',
  110. 'fields' => [
  111. 'code' => '产品编号,必填,本厂内唯一,存在则更新',
  112. 'name' => '产品名称',
  113. 'spec' => '规格',
  114. 'unit' => '单位,默认件',
  115. 'remark' => '备注',
  116. 'processes' => [
  117. ['code' => '工艺编号', 'seq' => 1, 'name' => '工序名', 'workstation' => '工位', 'standard_time' => '工时', 'qc_points' => '质检要点'],
  118. ],
  119. ],
  120. ],
  121. 'workorder' => [
  122. 'method' => 'POST /api/ps/open/workorder',
  123. 'fields' => [
  124. 'no' => '工单号,必填,本厂内唯一,存在则更新',
  125. 'product_code' => '产品编号,可选;填写则关联已有产品或自动建档',
  126. 'product_name' => '产品名称。不选产品时必填;选了产品可带出',
  127. 'batch_no' => '批次',
  128. 'customer' => '客户',
  129. 'plan_date' => '计划日期 YYYY-MM-DD',
  130. 'remark' => '备注',
  131. 'processes' => [
  132. ['code' => '工艺编号', 'seq' => 1, 'name' => '工序名'],
  133. ],
  134. ],
  135. ],
  136. ]);
  137. }
  138. /**
  139. * @return array
  140. */
  141. protected function jsonBody()
  142. {
  143. $payload = $this->request->post();
  144. if (!$payload) {
  145. $raw = (string)file_get_contents('php://input');
  146. $decoded = json_decode($raw, true);
  147. if (is_array($decoded)) {
  148. $payload = $decoded;
  149. }
  150. }
  151. return is_array($payload) ? $payload : [];
  152. }
  153. }