Options.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\admin\controller\manage;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 质控选项
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Options extends Backend
  14. {
  15. /**
  16. * Options模型对象
  17. * @var \app\admin\model\manage\Options
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\manage\Options;
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. $dimension = $_GET['dimension'];
  36. $this->view->assign('dimension',$dimension);
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. $ins = Db::table('qc_dimension')->column('id,title');
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $total = $this->model
  47. ->where($where)
  48. ->where('dimension_id',$dimension)
  49. ->order($sort, $order)
  50. ->count();
  51. $list = $this->model
  52. ->where($where)
  53. ->where('dimension_id',$dimension)
  54. ->order($sort, $order)
  55. ->limit($offset, $limit)
  56. ->select();
  57. $list = collection($list)->toArray();
  58. foreach ($list as $k=>$v)
  59. {
  60. $list[$k]['dimension_name'] = $ins[$v['dimension_id']] ?? '';
  61. }
  62. $result = array("total" => $total, "rows" => $list);
  63. return json($result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 添加
  69. */
  70. public function add()
  71. {
  72. if ($this->request->isPost()) {
  73. $params = $this->request->post("row/a");
  74. if ($params) {
  75. $params = $this->preExcludeFields($params);
  76. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  77. $params[$this->dataLimitField] = $this->auth->id;
  78. }
  79. $result = false;
  80. Db::startTrans();
  81. try {
  82. //是否采用模型验证
  83. if ($this->modelValidate) {
  84. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  85. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  86. $this->model->validateFailException(true)->validate($validate);
  87. }
  88. $params['dimension_id'] = $_GET['dimension'];
  89. $result = $this->model->allowField(true)->save($params);
  90. Db::commit();
  91. } catch (ValidateException $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. } catch (PDOException $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. } catch (Exception $e) {
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. }
  101. if ($result !== false) {
  102. $this->success();
  103. } else {
  104. $this->error(__('No rows were inserted'));
  105. }
  106. }
  107. $this->error(__('Parameter %s can not be empty', ''));
  108. }
  109. return $this->view->fetch();
  110. }
  111. }