DeviceController.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace app\admin\controller\device;
  3. use app\admin\validate\device\DeviceValidate;
  4. use app\controller\Curd;
  5. use app\model\Device;
  6. use app\model\SysSerial;
  7. use support\Request;
  8. use support\Response;
  9. class DeviceController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new Device();
  14. $this->validate = true;
  15. $this->validateClass = new DeviceValidate();
  16. }
  17. public function select(Request $request): Response
  18. {
  19. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  20. $order = $request->get('order', 'desc');
  21. $field = $field ?? 'device_addtimes';
  22. $query = $this->doSelect($where, $field, $order);
  23. return $this->doFormat($query, $format, $limit);
  24. }
  25. /* 设备列表(下拉选项)*/
  26. public function selectList()
  27. {
  28. $class = get_class($this->model);
  29. $data = $class::where('device_status', 'ACTIVED')
  30. ->select('device_id', 'device_name','device_identify')
  31. ->get()
  32. ->toArray();
  33. return json_success('', $data);
  34. }
  35. protected function insertInput(Request $request): array
  36. {
  37. $data = $this->inputFilter($request->post());
  38. $data['device_id'] = $this->generateDeviceId();
  39. return $data;
  40. }
  41. /**
  42. * @Desc 生成设备ID
  43. * @Author Gorden
  44. * @Date 2024/3/26 13:32
  45. *
  46. * @return string
  47. * @throws \support\exception\BusinessException
  48. */
  49. private function generateDeviceId()
  50. {
  51. $id = SysSerial::getSerial();
  52. return "DE".str_pad($id,16,'0',STR_PAD_LEFT).random_string(8);
  53. }
  54. }