Machine.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. *
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Machine extends Backend
  11. {
  12. /**
  13. * Machine模型对象
  14. * @var \app\admin\model\Machine
  15. */
  16. protected $model = null;
  17. protected $searchFields = "status,machinen_name";//搜索查询字段
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Machine;
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //当前是否为关联查询
  35. $this->relationSearch = false;
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags', 'trim']);
  38. if ($this->request->isAjax()) {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $list = $this->model
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->paginate($limit);
  48. foreach ($list as $row) {
  49. $row->visible(['id','serial','machinen_name','status']);
  50. }
  51. $result = array("total" => $list->total(), "rows" => $list->items());
  52. return json($result);
  53. }
  54. return $this->view->fetch();
  55. }
  56. public function add(){
  57. if($this->request->isPost()){
  58. $post = input('');
  59. $data = [ // 创建要插入的数据
  60. 'serial' => $post['row']['serial'],
  61. 'machinen_name' => $post['row']['machinen_name'],
  62. 'status' => $post['row']['status']
  63. ];
  64. $result = Db::table('machineneserial')->insert($data); // 插入数据到数据库
  65. if($result){
  66. $this->success('数据添加成功');
  67. }else{
  68. $this->error('数据添加失败');
  69. }
  70. } else {
  71. return $this->view->fetch();
  72. }
  73. }
  74. public function edit($ids = null) {
  75. if ($this->request->isPost()) {
  76. $post = $this->request->post();
  77. // 更新数据库
  78. $result = Db::name('machineneserial')->where('id', $post['id'])->update([
  79. 'serial' => $post['serial'],
  80. 'machinen_name' => $post['machinen_name'],
  81. 'status' => $post['status'],
  82. ]);
  83. if ($result !== false) {
  84. $this->success('更新成功');
  85. } else {
  86. $this->error('更新失败');
  87. }
  88. } else {
  89. $id = $this->request->param('ids');
  90. $result = Db::name('machineneserial')->where('id', $id)->find();
  91. $this->assign('result', $result); // 将数据分配给视图
  92. return $this->view->fetch();
  93. }
  94. }
  95. public function del($ids = null) {
  96. $result = Db::name('machineneserial')->where('id', $ids)->delete();
  97. if ($result) {
  98. $this->success('删除成功');
  99. } else {
  100. $this->error('删除失败');
  101. }
  102. }
  103. public function jiashicang(){
  104. return $this->view->fetch();
  105. }
  106. }