Org.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. use think\Exception;
  5. class Org extends Auth
  6. {
  7. public function index(){
  8. $tree = model('Org')->showAllTree();
  9. $this->assign('tree',$tree);
  10. return $this->fetch();
  11. }
  12. /**
  13. * 新增
  14. */
  15. public function add($pid=0,$t=0){
  16. if(request()->isPost()){
  17. $res = model('Org')->addSave();
  18. if($res){
  19. $this->success('操作成功',url('index'));
  20. }else{
  21. $this->error(model('Org')->getError());
  22. }
  23. }else{
  24. $fpid = Db::name('org')->where('id',$pid)->value('parent_id');
  25. $flist = Db::name('org')->where('del',0)->where('type',1)->select();
  26. foreach ($flist as $k=>$v){
  27. $flist[$k]['title'] = $v['name'];
  28. }
  29. $this->assign('flist',$flist);
  30. $this->assign('fpid',$fpid);
  31. $this->assign('pid',$pid);
  32. $this->assign('t',$t);
  33. return $this->fetch();
  34. }
  35. }
  36. /**
  37. * 编辑
  38. */
  39. public function edit($id=0){
  40. if(request()->isPost()){
  41. $res = model('Org')->editSave();
  42. if($res){
  43. $this->success('操作成功',url('index'));
  44. }else{
  45. $this->error(model('Org')->getError());
  46. }
  47. }else{
  48. $info = db('org')->where('id',$id)->find();
  49. $info['location'] = '';
  50. if($info && $info['lat'] && $info['lng']){
  51. $info['location'] = $info['lat'].'-'.$info['lng'];
  52. }
  53. $orgList = Db::name('org')
  54. ->where('del',0)
  55. ->where('type',1)
  56. ->select();
  57. $this->assign('orgList',$orgList);
  58. $flist = Db::name('org')->where('del',0)->where('type',1)->where('id','<>',$id)->select();
  59. foreach ($flist as $k=>$v){
  60. $flist[$k]['title'] = $v['name'];
  61. }
  62. $this->assign('flist',$flist);
  63. $this->assign('info',$info);
  64. $this->assign('t',$info['type']);
  65. $this->assign('fpid',$info['parent_id']);
  66. return $this->fetch();
  67. }
  68. }
  69. public function changeEnable($id=0,$enable=0){
  70. if($id <= 0){
  71. $this->error('参数错误');
  72. }
  73. $ret = Db::name('org')->where('id',$id)->setField('enable',$enable);
  74. if(!$ret){
  75. $this->error('操作失败');
  76. }
  77. $this->success('操作成功');
  78. }
  79. /**
  80. * 删除记录
  81. * @param int $id
  82. */
  83. public function del($id=0){
  84. if(!$id){
  85. $this->error('参数错误');
  86. }
  87. // 检查是否有子级
  88. $ret = Db::name('org')->where('parent_id',$id)->where('del',0)->find();
  89. if($ret){
  90. $this->error('有子级不能删除');
  91. }
  92. $res = Db::name('org')->where('id',$id)->setField('del',1);
  93. if($res){
  94. $this->success('删除成功');
  95. }else{
  96. $this->error('删除失败');
  97. }
  98. }
  99. /**
  100. * 授权
  101. */
  102. public function auth(){
  103. if(request()->isPost()){
  104. $res = model('Org')->authSave();
  105. if($res){
  106. $this->success('操作成功',url('index'));
  107. }else{
  108. $this->error(model('Org')->getError());
  109. }
  110. }else{
  111. $orgId = input('id/d',0);
  112. $meuns = model('Menu')->getAllMenuTree();
  113. $this->assign('menus',$meuns);
  114. $sauth = model('Org')->getOrgAuths($orgId,1);
  115. $this->assign('sauth',$sauth);
  116. $appauths = model('AppIcon')->getAllIconTree();
  117. $this->assign('appauths',$appauths);
  118. $sappauth = model('Org')->getOrgAuths($orgId,2);
  119. $this->assign('sappauth',$sappauth);
  120. $this->assign('orgId',$orgId);
  121. return $this->fetch();
  122. }
  123. }
  124. public function copyAuth($id){
  125. if(request()->isPost()){
  126. $id = input('id',0);
  127. $orgId = input('orgId',0);
  128. if($id <1){
  129. $this->error('参数错误');
  130. }
  131. if($orgId < 1 ){
  132. $this->error('请选择复制的项目');
  133. }
  134. $oinfo = Db::name('org')->where('id',$orgId)->find();
  135. $data = [
  136. 'auths'=>$oinfo['auths'],
  137. 'appauths'=>$oinfo['appauths'],
  138. 'update_time'=>getTime()
  139. ];
  140. $res = Db::name('org')->where('id',$id)->update($data);
  141. if($res){
  142. $this->success('操作成功',url('index'));
  143. }else{
  144. $this->error('操作失败');
  145. }
  146. }else{
  147. $orgList = Db::name('org')
  148. ->where('del',0)
  149. ->where('type',2)
  150. ->select();
  151. foreach ($orgList as $k=>$v){
  152. $orgList[$k]['title'] = $v['name'];
  153. }
  154. $this->assign('id',$id);
  155. $this->assign('orgList',$orgList);
  156. return $this->fetch();
  157. }
  158. }
  159. public function orgBatchAuth(){
  160. if(request()->isPost()){
  161. $ids = input('ids',[]);
  162. $appids = input('appids',[]);
  163. $orgs = input('orgs',[]);
  164. if(!$orgs){
  165. $this->error('请选择组织');
  166. }
  167. if(!$ids && !$appids){
  168. $this->error('请勾选权限');
  169. }
  170. Db::startTrans();
  171. try {
  172. foreach ($orgs as $k=>$v){
  173. $info = Db::name('org')
  174. ->where('id',$v)
  175. ->where('type',2)
  176. ->where('del',0)
  177. ->find();
  178. $auths = $info['auths'] ? explode(',',$info['auths']):[];
  179. if($ids){
  180. $diff = array_diff($ids,$auths);
  181. $newAuths = array_merge($diff,$auths);
  182. $data['auths'] = implode(',',$newAuths);
  183. }
  184. if($appids){
  185. $appauths = $info['appauths'] ? explode(',',$info['appauths']):[];
  186. $appdiff = array_diff($appids,$appauths);
  187. $newAppauths = array_merge($appdiff,$appauths);
  188. $data['appauths'] = implode(',',$newAppauths);
  189. }
  190. $data['update_time'] = getTime();
  191. Db::name('org')->where('id',$v)->update($data);
  192. }
  193. Db::commit();
  194. $this->success('操作成功',url('orgBatchAuth'));
  195. } catch (Exception $e) {
  196. Db::rollback();
  197. $this->error($e->getMessage());
  198. }
  199. }else{
  200. $rolesId = input('id/d',0);
  201. $meuns = model('Menu')->getOrgAllMenuTree($this->orgId);
  202. $this->assign('menus',$meuns);
  203. // $sauth = model('Roles')->getRolesAuths($rolesId,1);
  204. // $this->assign('sauth',$sauth);
  205. $appauths = model('AppIcon')->getOrgAllIconTree($this->orgId);
  206. $this->assign('appauths',$appauths);
  207. // $sappauth = model('Roles')->getRolesAuths($rolesId,2);
  208. // $this->assign('sappauth',$sappauth);
  209. $orgList = Db::name('org')->where('del',0)->where('type',2)->select();
  210. $this->assign('orgList',$orgList);
  211. $this->assign('rolesId',$rolesId);
  212. return $this->fetch();
  213. }
  214. }
  215. }