You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.1 KiB

5 years ago
  1. <?php
  2. namespace App\Controller\v3;
  3. use App\Controller\BaseController;
  4. use App\Service\v3\Interfaces\DeviceServiceInterface;
  5. use Hyperf\Di\Annotation\Inject;
  6. use Hyperf\Validation\ValidationException;
  7. class DeviceController extends BaseController
  8. {
  9. /**
  10. * @Inject
  11. * @var DeviceServiceInterface
  12. */
  13. protected $deviceService;
  14. public function bind()
  15. {
  16. $validator = $this->validationFactory->make(
  17. $this->request->all(),
  18. [
  19. 'store_id' => 'required|nonempty|integer',
  20. 'device_name' => 'required|nonempty|alpha_num',
  21. ],
  22. [
  23. 'store_id.required' => '参数不正确',
  24. 'store_id.nonempty' => '参数不正确',
  25. 'store_id.integer' => '参数不正确',
  26. 'device_name.required' => '参数不正确',
  27. 'device_name.nonempty' => '参数不正确',
  28. 'device_name.alpha_num' => '参数不正确',
  29. ]
  30. );
  31. if ($validator->fails()) {
  32. // Handle exception
  33. throw new ValidationException($validator);
  34. return;
  35. }
  36. $store_id = $this->request->input('store_id');
  37. $device_name = $this->request->input('device_name');
  38. $sd = $this->deviceService->bindByStoreId($device_name, $store_id);
  39. if (is_null($sd)) {
  40. return $this->result(100, '', '绑定失败: 设备号已经被绑定或不存在');
  41. }
  42. return $this->success($sd, '绑定成功');
  43. }
  44. public function list()
  45. {
  46. $validator = $this->validationFactory->make(
  47. $this->request->all(),
  48. [
  49. 'store_id' => 'required|nonempty|integer',
  50. ],
  51. [
  52. 'store_id.required' => '参数不正确',
  53. 'store_id.nonempty' => '参数不正确',
  54. 'store_id.integer' => '参数不正确',
  55. ]
  56. );
  57. if ($validator->fails()) {
  58. // Handle exception
  59. throw new ValidationException($validator);
  60. return;
  61. }
  62. $store_id = $this->request->input('store_id');
  63. $devices = $this->deviceService->getListByStoreId($store_id);
  64. return $this->success($devices);
  65. }
  66. public function unbind()
  67. {
  68. $validator = $this->validationFactory->make(
  69. $this->request->all(),
  70. [
  71. 'bind_id' => 'required|nonempty|integer',
  72. ],
  73. [
  74. 'bind_id.required' => '参数不正确',
  75. 'bind_id.nonempty' => '参数不正确',
  76. 'bind_id.integer' => '参数不正确',
  77. ]
  78. );
  79. if ($validator->fails()) {
  80. // Handle exception
  81. throw new ValidationException($validator);
  82. return;
  83. }
  84. $bind_id = $this->request->input('bind_id');
  85. $unbind_num = $this->deviceService->unbindById($bind_id);
  86. if ($unbind_num == 0) {
  87. return $this->result(100, '', '解绑失败: 设备已经解绑或不存在');
  88. }
  89. return $this->success(['unbind' => $unbind_num]);
  90. }
  91. }