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