Jianceproject.php 4.2 KB

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