Index.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\admin\controller\department;
  3. use app\common\controller\Backend;
  4. use \app\admin\model\department\Department as DepartmentModel;
  5. use fast\Tree;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. /**
  11. * 部门管理
  12. */
  13. class Index extends Backend
  14. {
  15. protected $tree = null;
  16. protected $departmentList;
  17. protected $noNeedRight=['selectpage'];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new DepartmentModel;
  22. $this->tree = Tree::instance();
  23. $this->tree->init(DepartmentModel::allDepartment(), 'parent_id');
  24. $this->departmentList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name');
  25. $this->view->assign("departmentList", $this->departmentList);
  26. $this->view->assign("statusList", DepartmentModel::getStatusList());
  27. }
  28. /**
  29. * 部门列表
  30. */
  31. public function index()
  32. {
  33. //设置过滤方法
  34. $this->request->filter(['strip_tags', 'trim']);
  35. if ($this->request->isAjax()) {
  36. //如果发送的来源是Selectpage,则转发到Selectpage
  37. $searchValue = $this->request->request("searchValue");
  38. $search = $this->request->request("search");
  39. //构造父类select列表选项数据
  40. $list = [];
  41. if ($search||$searchValue) {
  42. foreach ($this->departmentList as $k => &$v) {
  43. if ($search&&stripos($v['name'], $search) !== false) {
  44. $list[] = $v;
  45. }
  46. if ($searchValue&&in_array($v['id'], explode(',',$searchValue)) !== false) {
  47. $v['name']=preg_replace("/(\s|\&nbsp\;| |\xc2\xa0)/", " ", strip_tags($v['name'])); //过滤空格
  48. $list[] = $v;
  49. }
  50. }
  51. } else {
  52. $list = $this->departmentList;
  53. }
  54. $list = array_values($list);
  55. foreach ($list as $k => &$v) {
  56. $v['pid'] = $v['parent_id'];
  57. }
  58. $total = count($list);
  59. $result = array("total" => $total, "rows" => $list);
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 添加
  66. */
  67. public function add()
  68. {
  69. if ($this->request->isPost()) {
  70. $params = $this->request->post("row/a");
  71. if ($params) {
  72. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  73. $params[$this->dataLimitField] = $this->auth->id;
  74. }
  75. try {
  76. //是否采用模型验证
  77. if ($this->modelValidate) {
  78. $name = basename(str_replace('\\', '/', get_class($this->model)));
  79. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  80. $this->model->validate($validate);
  81. }
  82. $nameArr = array_filter(explode("\n", str_replace("\r\n", "\n", $params['name'])));
  83. //获取组织最高的ID;
  84. $params['organise_id'] = DepartmentModel::getOrganiseID(isset($params['parent_id']) ? $params['parent_id'] : 0);
  85. if (count($nameArr) > 1) {
  86. foreach ($nameArr as $index => $item) {
  87. $params['name'] = $item;
  88. $result = $this->model->allowField(true)->isUpdate(false)->data($params)->save();
  89. }
  90. } else {
  91. $result = $this->model->allowField(true)->save($params);
  92. }
  93. if ($result !== false) {
  94. //清空部门缓存
  95. DepartmentModel::clearCache();
  96. $this->success();
  97. } else {
  98. $this->error($this->model->getError());
  99. }
  100. } catch (\think\exception\PDOException $e) {
  101. $this->error($e->getMessage());
  102. }
  103. }
  104. $this->error(__('Parameter %s can not be empty', ''));
  105. }
  106. return $this->view->fetch();
  107. }
  108. public function edit($ids = null)
  109. {
  110. $row = DepartmentModel::get($ids);
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. if ($this->request->isPost()) {
  115. $params = $this->request->post("row/a");
  116. if ($params) {
  117. $params = $this->preExcludeFields($params);
  118. $result = false;
  119. Db::startTrans();
  120. try {
  121. //是否采用模型验证
  122. if ($this->modelValidate) {
  123. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  124. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  125. $row->validateFailException(true)->validate($validate);
  126. }
  127. //最顶级的不能修改成下级
  128. if ($row['parent_id']==0&&$params['parent_id']){
  129. \exception(__("Top-level organization cannot be modified to lower level organization"));
  130. }
  131. //获取修改后的组织最高的ID;
  132. //获取组织最高的ID;
  133. $organise_id = isset($params['parent_id']) ? DepartmentModel::getOrganiseID($params['parent_id']) : 0;
  134. //判断修改的父类是否和之前的一样并且organise_id不一样就要修改,含子类
  135. if ($params['parent_id'] != $row['parent_id'] && $organise_id != $row['organise_id']) {
  136. $departmentIds = DepartmentModel::getChildrenIds($row->id, true);
  137. $mapwhere['id'] = ['in', $departmentIds];
  138. $departmentIds = DepartmentModel::update(['organise_id' => $organise_id], $mapwhere);
  139. }
  140. $result = $row->allowField(true)->save($params);
  141. Db::commit();
  142. } catch (ValidateException $e) {
  143. Db::rollback();
  144. $this->error($e->getMessage());
  145. } catch (PDOException $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. } catch (Exception $e) {
  149. Db::rollback();
  150. $this->error($e->getMessage());
  151. }
  152. if ($result !== false) {
  153. //清空部门缓存
  154. DepartmentModel::clearCache();
  155. $this->success();
  156. } else {
  157. $this->error(__('No rows were updated'));
  158. }
  159. }
  160. $this->error(__('Parameter %s can not be empty', ''));
  161. } else {
  162. $row = $row->toArray();
  163. $childrenIds = $this->tree->getChildrenIds($row['id'], true);
  164. $this->view->assign('childrenIds', $childrenIds);
  165. $this->view->assign("row", $row);
  166. return $this->view->fetch();
  167. }
  168. }
  169. public function selectpage(){
  170. $type= $this->request->param('type','');
  171. if ($type=='all'){
  172. $type=true;
  173. }else{
  174. $type=$this->auth->isSuperAdmin()||$this->auth->data_scope?true:false;
  175. }
  176. $departmentList = [];
  177. $this->allDepartment = \app\admin\model\department\Admin::getAllDepartmentsArray($this->auth->id,$type);
  178. $this->departmentList=collection($this->allDepartment)->toArray();
  179. ;
  180. return $this->index();
  181. }
  182. }