Structures.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 组织架构
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use think\Hook;
  9. use think\Request;
  10. use think\Db;
  11. class Structures extends ApiCommon
  12. {
  13. /**
  14. * 用于判断权限
  15. * @permission 无限制
  16. * @allow 登录用户可访问
  17. * @other 其他根据系统设置
  18. **/
  19. public function _initialize()
  20. {
  21. $action = [
  22. 'permission'=>[''],
  23. 'allow'=>['index','read','save','update','delete','deletes','enables','listdialog','subindex','getsubuserbystructrue']
  24. ];
  25. Hook::listen('check_auth',$action);
  26. $request = Request::instance();
  27. $a = strtolower($request->action());
  28. if (!in_array($a, $action['permission'])) {
  29. parent::_initialize();
  30. }
  31. }
  32. //获取权限范围内的部门
  33. public function subIndex()
  34. {
  35. $param = $this->param;
  36. $userInfo = $this->userInfo;
  37. $userModel = model('User');
  38. $m = $param['m'] ? : '';
  39. $c = $param['c'] ? : '';
  40. $a = $param['a'] ? : '';
  41. $ret = $userModel->getUserByPer($m, $c, $a);
  42. $where['au.id'] = ['in',$ret];
  43. $structure_ids = Db::name('AdminUser')
  44. ->alias('au')
  45. ->join('AdminStructure ast','ast.id = au.structure_id','LEFT')
  46. ->where($where)
  47. ->group('structure_id')
  48. ->order('structure_id asc')
  49. ->column('ast.id');
  50. $list = Db::name('AdminStructure')
  51. ->group('id')
  52. ->order('id asc')
  53. ->select();
  54. $result = getSubObj(0, $list, '', 1);
  55. $adminTypes = adminGroupTypes($userInfo['id']);
  56. if(!in_array(1,$adminTypes)){
  57. foreach ($result as $key => $value) {
  58. if(!in_array($value['id'],$structure_ids)){
  59. unset($result[$key]);
  60. }
  61. }
  62. }
  63. return resultArray(['data'=>$result]);
  64. }
  65. //获取部门下权限范围内员工
  66. public function getSubUserByStructrue()
  67. {
  68. $param = $this->param;
  69. $userModel = model('User');
  70. $structureList = $userModel->getSubUserByStr($param['structure_id'],2);
  71. if($param['structure_id']){
  72. $where['id'] = ['in',$structureList];
  73. }
  74. $list =Db::name('AdminUser')->field('id,realname')->where($where)->select();
  75. $list = $list?:array();
  76. return resultArray(['data'=>$list]);
  77. }
  78. /**
  79. * 部门列表
  80. * @author Michael_xu
  81. * @param
  82. * @return
  83. */
  84. public function index()
  85. {
  86. //权限判断
  87. // if (!checkPerByAction('admin', 'users', 'index')) {
  88. // header('Content-Type:application/json; charset=utf-8');
  89. // exit(json_encode(['code'=>102,'error'=>'无权操作']));
  90. // }
  91. $structureModel = model('Structure');
  92. $param = $this->param;
  93. $type = $param['type'] ? 'tree' : '';
  94. $data = $structureModel->getDataList($type);
  95. return resultArray(['data' => $data]);
  96. }
  97. /**
  98. * 部门详情
  99. * @author Michael_xu
  100. * @param
  101. * @return
  102. */
  103. public function read()
  104. {
  105. $structureModel = model('Structure');
  106. $param = $this->param;
  107. $data = $structureModel->getDataById($param['id']);
  108. if (!$data) {
  109. return resultArray(['error' => $structureModel->getError()]);
  110. }
  111. return resultArray(['data' => $data]);
  112. }
  113. /**
  114. * 部门添加
  115. * @author Michael_xu
  116. * @param
  117. * @return
  118. */
  119. public function save()
  120. {
  121. //权限判断
  122. if (!checkPerByAction('admin', 'users', 'structures_save')) {
  123. header('Content-Type:application/json; charset=utf-8');
  124. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  125. }
  126. $structureModel = model('Structure');
  127. $param = $this->param;
  128. if(!$param['pid']){
  129. resultArray(['error' => '请选择上级部门']);
  130. }
  131. $data = $structureModel->createData($param);
  132. if (!$data) {
  133. return resultArray(['error' => $structureModel->getError()]);
  134. }
  135. return resultArray(['data' => '添加成功']);
  136. }
  137. /**
  138. * 部门编辑
  139. * @author Michael_xu
  140. * @param
  141. * @return
  142. */
  143. public function update()
  144. {
  145. //权限判断
  146. if (!checkPerByAction('admin', 'users', 'structures_update')) {
  147. header('Content-Type:application/json; charset=utf-8');
  148. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  149. }
  150. $structureModel = model('Structure');
  151. $param = $this->param;
  152. $dataInfo = $structureModel->getDataByID($param['id']);
  153. if (empty($dataInfo['pid']) || $param['id'] == '1') {
  154. unset($param['pid']);
  155. }
  156. $data = $structureModel->updateDataById($param, $param['id']);
  157. if (!$data) {
  158. return resultArray(['error' => $structureModel->getError()]);
  159. }
  160. return resultArray(['data' => '编辑成功']);
  161. }
  162. /**
  163. * 部门删除
  164. * @author Michael_xu
  165. * @param
  166. * @return
  167. */
  168. public function delete()
  169. {
  170. //权限判断
  171. if (!checkPerByAction('admin', 'users', 'structures_delete')) {
  172. header('Content-Type:application/json; charset=utf-8');
  173. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  174. }
  175. $structureModel = model('Structure');
  176. $param = $this->param;
  177. $data = $structureModel->delStrById($param['id']);
  178. if (!$data) {
  179. return resultArray(['error' => $structureModel->getError()]);
  180. }
  181. return resultArray(['data' => '删除成功']);
  182. }
  183. /**
  184. * 部门启用、停用
  185. * @author Michael_xu
  186. * @param
  187. * @return
  188. */
  189. public function enables()
  190. {
  191. //权限判断
  192. if (!checkPerByAction('admin', 'users', 'structures_update')) {
  193. header('Content-Type:application/json; charset=utf-8');
  194. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  195. }
  196. $structureModel = model('Structure');
  197. $param = $this->param;
  198. $data = $structureModel->enableDatas($param['ids'], $param['status'], true);
  199. if (!$data) {
  200. return resultArray(['error' => $structureModel->getError()]);
  201. }
  202. return resultArray(['data' => '操作成功']);
  203. }
  204. /**
  205. * 部门list
  206. * @author Michael_xu
  207. * @param
  208. * @return
  209. */
  210. public function listDialog()
  211. {
  212. $param = $this->param;
  213. $structure_id = $param['id'];
  214. $type = $param['type'];
  215. if (!$structure_id) {
  216. return resultArray(['error' => '参数错误']);
  217. }
  218. $structureList = db('admin_structure')->select();
  219. if ($type == 'update') {
  220. //去除自身及下属部门
  221. foreach ($structureList as $k => $v) {
  222. if (($v['id'] == $structure_id) || ($v['pid'] == $structure_id)) {
  223. unset($structureList[$k]);
  224. }
  225. }
  226. }
  227. $structureList = getSubObj(0, $structureList, '', 1);
  228. return resultArray(['data' => $structureList]);
  229. }
  230. }