Index.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. $letter = "区队";
  89. if (strpos($item, $letter) !== false) {
  90. $params['level'] =5;
  91. }
  92. $result = $this->model->allowField(true)->isUpdate(false)->data($params)->save();
  93. }
  94. } else {
  95. $letter = "区队";
  96. if (strpos($params['name'], $letter) !== false) {
  97. $params['level'] =5;
  98. }
  99. $result = $this->model->allowField(true)->save($params);
  100. }
  101. if ($result !== false) {
  102. //清空部门缓存
  103. DepartmentModel::clearCache();
  104. \think\Hook::listen("wipecache_after");
  105. $this->success();
  106. } else {
  107. $this->error($this->model->getError());
  108. }
  109. } catch (\think\exception\PDOException $e) {
  110. $this->error($e->getMessage());
  111. }
  112. }
  113. $this->error(__('Parameter %s can not be empty', ''));
  114. }
  115. return $this->view->fetch();
  116. }
  117. public function edit($ids = null)
  118. {
  119. $row = DepartmentModel::get($ids);
  120. if (!$row) {
  121. $this->error(__('No Results were found'));
  122. }
  123. if ($this->request->isPost()) {
  124. $params = $this->request->post("row/a");
  125. if ($params) {
  126. $params = $this->preExcludeFields($params);
  127. $result = false;
  128. Db::startTrans();
  129. try {
  130. //是否采用模型验证
  131. if ($this->modelValidate) {
  132. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  133. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  134. $row->validateFailException(true)->validate($validate);
  135. }
  136. //最顶级的不能修改成下级
  137. if ($row['parent_id']==0&&$params['parent_id']){
  138. \exception(__("Top-level organization cannot be modified to lower level organization"));
  139. }
  140. //获取修改后的组织最高的ID;
  141. //获取组织最高的ID;
  142. $organise_id = isset($params['parent_id']) ? DepartmentModel::getOrganiseID($params['parent_id']) : 0;
  143. //判断修改的父类是否和之前的一样并且organise_id不一样就要修改,含子类
  144. if ($params['parent_id'] != $row['parent_id'] && $organise_id != $row['organise_id']) {
  145. $departmentIds = DepartmentModel::getChildrenIds($row->id, true);
  146. $mapwhere['id'] = ['in', $departmentIds];
  147. $departmentIds = DepartmentModel::update(['organise_id' => $organise_id], $mapwhere);
  148. }
  149. $result = $row->allowField(true)->save($params);
  150. Db::commit();
  151. } catch (ValidateException $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. } catch (PDOException $e) {
  155. Db::rollback();
  156. $this->error($e->getMessage());
  157. } catch (Exception $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. if ($result !== false) {
  162. //清空部门缓存
  163. DepartmentModel::clearCache();
  164. \think\Hook::listen("wipecache_after");
  165. $this->success();
  166. } else {
  167. $this->error(__('No rows were updated'));
  168. }
  169. }
  170. $this->error(__('Parameter %s can not be empty', ''));
  171. } else {
  172. $row = $row->toArray();
  173. $childrenIds = $this->tree->getChildrenIds($row['id'], true);
  174. $this->view->assign('childrenIds', $childrenIds);
  175. $this->view->assign("row", $row);
  176. return $this->view->fetch();
  177. }
  178. }
  179. public function selectpage(){
  180. $type= $this->request->param('type','');
  181. if ($type=='all'){
  182. $type=true;
  183. }else{
  184. $type=$this->auth->isSuperAdmin()||$this->auth->data_scope?true:false;
  185. }
  186. $departmentList = [];
  187. $this->allDepartment = \app\admin\model\department\Admin::getAllDepartmentsArray($this->auth->id,$type);
  188. $this->departmentList=collection($this->allDepartment)->toArray();
  189. ;
  190. return $this->index();
  191. }
  192. }