DeviceController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Actions\Device\Box;
  4. use App\Admin\Actions\Device\getRule;
  5. use App\Admin\Actions\Device\Lock;
  6. use App\Admin\Actions\Device\Reflash;
  7. use App\Admin\Actions\Device\sendRule;
  8. use App\Admin\Actions\Device\testRule;
  9. use App\Admin\Actions\Users\InRecords;
  10. use App\Admin\Actions\Users\PutRecords;
  11. use App\Admin\Actions\Users\WithDraw;
  12. use App\Admin\Controllers\Sta;
  13. use App\Model\DeviceInfo;
  14. use App\Model\DeviceType;
  15. use App\Model\UserInfo;
  16. use Encore\Admin\Facades\Admin;
  17. use Encore\Admin\Controllers\AdminController;
  18. use Encore\Admin\Form;
  19. use Encore\Admin\Grid;
  20. use Encore\Admin\Show;
  21. use Encore\Admin\Widgets\Echarts\Echarts;
  22. use function foo\func;
  23. use Zhusaidong\GridExporter\Exporter;
  24. /**
  25. * @author pxwei
  26. * Class DeviceController
  27. * @package App\Admin\Controllers\Device
  28. */
  29. class DeviceController extends AdminController
  30. {
  31. /**
  32. * Title for current resource.
  33. *
  34. * @var string
  35. */
  36. protected $title = '设备管理';
  37. /**
  38. * Make a grid builder.
  39. *
  40. * @return Grid
  41. */
  42. protected function grid()
  43. {
  44. $grid = new Grid(new DeviceInfo());
  45. $status = DeviceInfo::getStatus();
  46. $breakDown = DeviceInfo::getBrakdown();
  47. $grid->disableCreateButton(false);
  48. // $exporter = Exporter::get($grid);
  49. // $exporter->setFileName('设备导出.xlsx');
  50. $grid->tools(function (Grid\Tools $tools) {
  51. $tools->append(new Reflash());
  52. });
  53. $grid->model()->orderBy('status','asc');
  54. $grid->header(function (){
  55. return "<script src='/js/loadMiniApp.js?24'></script><style>.dropdown-menu{}</style>";
  56. });
  57. $grid->column('id', __('ID'))->sortable();
  58. $grid->column('device_name', __('设备名称'))->editable();
  59. $grid->column('device_type', __('类型'))->display(function ($w){
  60. if(empty($w)) return '无';
  61. return DeviceType::where(['id'=>$w])->value('name');
  62. });
  63. $grid->column('query', __('刷新时间'));
  64. $grid->column('status', __('设备状态'))->using($status)->sortable();;
  65. $grid->column('runningStatus', __('运行状态'))->display(function ($s){
  66. $arr = [['name'=>'正常','color'=>'success'],['name'=>'故障','color'=>'danger'],['name'=>'故障','color'=>'danger'],['name'=>'故障','color'=>'danger'],['name'=>'故障','color'=>'danger'],['name'=>'正常','color'=>'success']];
  67. return "<span class='label label-{$arr[$s]['color']}'>{$arr[$s]['name']}</span>";
  68. });
  69. $grid->column('updated_at', __('更新时间'))->sortable();
  70. $grid->actions(function ($actions){
  71. $actions->disableDelete(false);
  72. $actions->add(new Box());
  73. $actions->add(new Lock());
  74. $actions->add(new sendRule());
  75. $actions->add(new getRule());
  76. $actions->add(new testRule());
  77. });
  78. $grid->filter(function (Grid\Filter $filter){
  79. $filter->column(1/2, function ($filter) {
  80. $filter->like('device_name','设备名称');
  81. $filter->like('device_type','设备类型')->select(function (){
  82. return DeviceType::pluck('name','id');
  83. });
  84. $filter->equal('status','状态')->select(DeviceInfo::getStatus());
  85. });
  86. });
  87. return $grid;
  88. }
  89. /**
  90. * Make a show builder.
  91. *
  92. * @param mixed $id
  93. * @return Show
  94. */
  95. protected function detail($id)
  96. {
  97. $show = new Show(DeviceInfo::findOrFail($id));
  98. $isLock = DeviceInfo::getLock();
  99. $status = DeviceInfo::getStatus();
  100. $breakDown = DeviceInfo::getBrakdown();
  101. $show->field('id', __('ID'));
  102. $show->field('device_name', __('阿里云设备编号'));
  103. $show->field('device_comment', __('微信中名称'));
  104. $show->field('device_type', __('型号'));
  105. $show->field('iot_id', __('iot_id'));
  106. $show->field('product_key', __('product_key'));
  107. $show->field('device_secret', __('设备密匙'));
  108. $show->field('imei', __('imei'));
  109. $show->field('status', __('运行状态'))->using($status);
  110. $show->field('runningStatus', __('设备状态'))->using($breakDown);
  111. $show->field('lock_switch', __('是否锁定'))->using($isLock);
  112. $show->field('device_position', __('详细位置'));
  113. $show->field('device_weight', __('累计重量/kg'))->as(function ($w){
  114. return round($w/1000,3);
  115. });
  116. $show->field('device_qrcode', __('二维码'))->image();
  117. $show->field('device_tem', __('温度'));
  118. $show->field('device_light_start', __('灯牌开始点亮时间'));
  119. $show->field('device_light_end', __('灯牌结束点亮时间'));
  120. $show->field('serial_number', __('海康卫视序列号'));
  121. $show->field('device_memo', __('设备备注'));
  122. $show->field('created_at', __('创建时间'));
  123. $show->field('updated_at', __('更新时间'));
  124. return $show;
  125. }
  126. /**
  127. * Make a form builder.
  128. *
  129. * @return Form
  130. */
  131. protected function form()
  132. {
  133. $form = new Form(new DeviceInfo());
  134. if(Admin::user()->isAdministrator()){
  135. $form->text('id', __('ID'));
  136. } else {
  137. $form->display('id', __('ID'));
  138. }
  139. $form->select('device_type', __('设备类型'))->options(DeviceType::getType())->rules('required');
  140. $form->display('device_name', __('(阿里云)设备编号'))->rules('required');
  141. $form->display('iot_id', __('(阿里云)iot_id'))->rules('required');
  142. $form->display('product_key', __('(阿里云)product_key'))->help('阿里产品密匙')->rules('required');
  143. $form->display('device_secret', __('(阿里云)设备密匙'))->rules('required');
  144. $form->display('imei', __('(阿里云)设备唯一码'))->rules('required');
  145. $form->hidden('device_tem', __('设备温度'))->default(28)->rules('required');
  146. $form->text('query', '刷新时间')->help('单位秒')->rules('required');
  147. // $form->select('status', __('状态'))->options(['在营','离线','禁用','未激活']);
  148. //$form->hidden('runningStatus', __('运行状态'))->options(['满溢','修复','清空','未知']);
  149. // $form->latlong('device_latitude', 'device_longitude', '经纬度')->default(['lat' => 90, 'lng' => 90]);
  150. $form->textarea('remark', '设备备注');
  151. $form->display('created_at', __('创建时间'));
  152. $form->display('updated_at', __('更新时间'));
  153. return $form;
  154. }
  155. }