Org.php 8.0 KB

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