Datadict.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\controller\stock;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use fast\Tree;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 字典分类
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Datadict extends Backend {
  14. /**
  15. * Datadict模型对象
  16. * @var \app\admin\model\stock\Datadict
  17. */
  18. protected $model = null;
  19. protected $dataDiclist = [];
  20. protected $noNeedRight = ['getjsTree'];
  21. public function _initialize() {
  22. parent::_initialize();
  23. $this->model = model('\app\admin\model\stock\Datadict');
  24. $this->view->assign("istreeList", $this->model->getIstreeList());
  25. $this->view->assign("enabledList", $this->model->getEnabledList());
  26. // 必须将结果集转换为数组
  27. $dataDiclist = collection($this->model->order('weigh', 'desc')->select())->toArray();
  28. Tree::instance()->init($dataDiclist);
  29. $this->dataDiclist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  30. $dataDicdata = [0 => '无'];
  31. foreach ($this->dataDiclist as $k => &$v) {
  32. $dataDicdata[$v['id']] = $v['name'];
  33. }
  34. $this->view->assign('dataDicdata', $dataDicdata);
  35. }
  36. /**
  37. * 查看
  38. */
  39. public function index() {
  40. if ($this->request->isAjax()) {
  41. $list = $this->dataDiclist;
  42. $total = count($this->dataDiclist);
  43. $result = array("total" => $total, "rows" => $list);
  44. return json($result);
  45. }
  46. return $this->view->fetch();
  47. }
  48. /**
  49. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  50. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  51. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  52. */
  53. public function getjsTree() {
  54. $list = collection($this->model->order('weigh', 'desc')->select())->toArray();
  55. $result = [];
  56. foreach ($list as $k => $v) {
  57. $n = [];
  58. $n['id'] = $v['id'];
  59. $n['parent'] = $v['pid'] == 0 ? "#" : $v['pid'];
  60. $n['text'] = $v['name'];
  61. $n['data'] = $v;
  62. $n['state'] = ["opend" => true, "disabled" => false];
  63. $result[] = $n;
  64. }
  65. return json($result);
  66. }
  67. /**
  68. * 编辑
  69. */
  70. public function edit($ids = null) {
  71. $row = $this->model->get($ids);
  72. if ($row->id == 9) {
  73. $this->error("系统保留分类,禁止修改!");
  74. }
  75. if (!$row) {
  76. $this->error(__('No Results were found'));
  77. }
  78. $adminIds = $this->getDataLimitAdminIds();
  79. if (is_array($adminIds)) {
  80. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  81. $this->error(__('You have no permission'));
  82. }
  83. }
  84. if ($this->request->isPost()) {
  85. $params = $this->request->post("row/a");
  86. if ($params) {
  87. $params = $this->preExcludeFields($params);
  88. $result = false;
  89. Db::startTrans();
  90. try {
  91. //是否采用模型验证
  92. if ($this->modelValidate) {
  93. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  94. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  95. $row->validateFailException(true)->validate($validate);
  96. }
  97. if ($row->id == 9) {
  98. $this->error("系统保留分类,禁止修改!");
  99. }
  100. $result = $row->allowField(true)->save($params);
  101. Db::commit();
  102. } catch (ValidateException $e) {
  103. Db::rollback();
  104. $this->error($e->getMessage());
  105. } catch (PDOException $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. } catch (Exception $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. }
  112. if ($result !== false) {
  113. $this->success();
  114. } else {
  115. $this->error(__('No rows were updated'));
  116. }
  117. }
  118. $this->error(__('Parameter %s can not be empty', ''));
  119. }
  120. $this->view->assign("row", $row);
  121. return $this->view->fetch();
  122. }
  123. /**
  124. * 删除
  125. */
  126. public function del($ids = "") {
  127. if ($ids) {
  128. $pk = $this->model->getPk();
  129. $adminIds = $this->getDataLimitAdminIds();
  130. if (is_array($adminIds)) {
  131. $this->model->where($this->dataLimitField, 'in', $adminIds);
  132. }
  133. $list = $this->model->where($pk, 'in', $ids)->select();
  134. $count = 0;
  135. Db::startTrans();
  136. try {
  137. foreach ($list as $k => $v) {
  138. if ($v->id == 9) {
  139. $this->error("系统保留分类,禁止删除!");
  140. }
  141. $count += $v->delete();
  142. }
  143. Db::commit();
  144. } catch (PDOException $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. } catch (Exception $e) {
  148. Db::rollback();
  149. $this->error($e->getMessage());
  150. }
  151. if ($count) {
  152. $this->success();
  153. } else {
  154. $this->error(__('No rows were deleted'));
  155. }
  156. }
  157. $this->error(__('Parameter %s can not be empty', 'ids'));
  158. }
  159. }