Version.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller\log;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. *
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Version extends Backend
  11. {
  12. /**
  13. * Version模型对象
  14. * @var \app\admin\model\log\Version
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\log\Version;
  21. }
  22. /**
  23. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  24. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  25. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  26. */
  27. public function index()
  28. {
  29. //设置过滤方法
  30. $this->request->filter(['strip_tags']);
  31. if ($this->request->isAjax()) {
  32. //如果发送的来源是Selectpage,则转发到Selectpage
  33. if ($this->request->request('keyField')) {
  34. return $this->selectpage();
  35. }
  36. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  37. $total = $this->model
  38. ->where($where)
  39. ->order($sort, $order)
  40. ->count();
  41. $list = $this->model
  42. ->alias('a')
  43. ->join(['version_project'=>'p'],'p.project_id=a.project')
  44. ->where($where)
  45. ->field('a.*,p.project_name')
  46. ->order($sort, $order)
  47. ->limit($offset, $limit)
  48. ->select();
  49. $list = collection($list)->toArray();
  50. $result = array("total" => $total, "rows" => $list);
  51. return json($result);
  52. }
  53. return $this->view->fetch();
  54. }
  55. /**
  56. * 添加
  57. */
  58. public function add()
  59. {
  60. if ($this->request->isPost()) {
  61. $params = $this->request->post("row/a");
  62. if ($params) {
  63. $params = $this->preExcludeFields($params);
  64. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  65. $params[$this->dataLimitField] = $this->auth->id;
  66. }
  67. $result = false;
  68. Db::startTrans();
  69. try {
  70. //是否采用模型验证
  71. if ($this->modelValidate) {
  72. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  73. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  74. $this->model->validateFailException(true)->validate($validate);
  75. }
  76. $result = $this->model->allowField(true)->save($params);
  77. Db::commit();
  78. } catch (ValidateException $e) {
  79. Db::rollback();
  80. $this->error($e->getMessage());
  81. } catch (PDOException $e) {
  82. Db::rollback();
  83. $this->error($e->getMessage());
  84. } catch (Exception $e) {
  85. Db::rollback();
  86. $this->error($e->getMessage());
  87. }
  88. if ($result !== false) {
  89. $this->success();
  90. } else {
  91. $this->error(__('No rows were inserted'));
  92. }
  93. }
  94. $this->error(__('Parameter %s can not be empty', ''));
  95. }
  96. $list = $this->getProject();
  97. $this->view->assign('list', $list);
  98. return $this->view->fetch();
  99. }
  100. public function getProject()
  101. {
  102. $project_list = Db::table('version_project')->select();
  103. $list = [];
  104. foreach ($project_list as $k=>$v)
  105. {
  106. $list[$v['project_id']] = $v['project_name'];
  107. }
  108. return $list;
  109. }
  110. /**
  111. * 编辑
  112. */
  113. public function edit($ids = null)
  114. {
  115. $row = $this->model->get($ids);
  116. if (!$row) {
  117. $this->error(__('No Results were found'));
  118. }
  119. $adminIds = $this->getDataLimitAdminIds();
  120. if (is_array($adminIds)) {
  121. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  122. $this->error(__('You have no permission'));
  123. }
  124. }
  125. if ($this->request->isPost()) {
  126. $params = $this->request->post("row/a");
  127. if ($params) {
  128. $params = $this->preExcludeFields($params);
  129. $result = false;
  130. Db::startTrans();
  131. try {
  132. //是否采用模型验证
  133. if ($this->modelValidate) {
  134. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  135. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  136. $row->validateFailException(true)->validate($validate);
  137. }
  138. $result = $row->allowField(true)->save($params);
  139. Db::commit();
  140. } catch (ValidateException $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. } catch (PDOException $e) {
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. } catch (Exception $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. }
  150. if ($result !== false) {
  151. $this->success();
  152. } else {
  153. $this->error(__('No rows were updated'));
  154. }
  155. }
  156. $this->error(__('Parameter %s can not be empty', ''));
  157. }
  158. $list = $this->getProject();
  159. $this->view->assign('list', $list);
  160. $this->view->assign("row", $row);
  161. return $this->view->fetch();
  162. }
  163. }