0
0

Org.php 7.7 KB

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