0
0

Roles.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. use think\Exception;
  5. class Roles extends Auth
  6. {
  7. public function index(){
  8. if(request()->isAjax()){
  9. //分页参数
  10. $length = input('rows',10,'intval'); //每页条数
  11. $page = input('page',1,'intval'); //第几页
  12. $start = ($page - 1) * $length; //分页开始位置
  13. //排序
  14. $sortRow = input('sidx','id','trim'); //排序列
  15. $sort = input('sord','desc','trim'); //排序方式
  16. $order = $sortRow.' '.$sort;
  17. $title = input('name','','trim');
  18. if($title){
  19. $map[] = ['name','like','%'.$title.'%'];
  20. }
  21. $enable = input('enable','','trim');
  22. if($enable != ''){
  23. $map[] = ['enable','=',$enable];
  24. }
  25. $parnet_id = input('parent_id','','trim');
  26. if($parnet_id != ''){
  27. $map[] = ['parent_id','=',$parnet_id];
  28. }
  29. $map[] = ['org_id','=',$this->orgId];
  30. $map[] = ['del','=',0];
  31. $map= empty($map) ? true: $map;
  32. //数据查询
  33. $lists = db('roles')->where($map)->limit($start,$length)->order(['sort'=>'asc','id'=>'desc'])->select();
  34. foreach ($lists as $k=>$v){
  35. $lists[$k]['pname'] = db('roles')->where('id',$v['parent_id'])->value('name');
  36. }
  37. //数据返回
  38. $totalCount = db('roles')->where($map)->count();
  39. $totalPage = ceil($totalCount/$length);
  40. $result['page'] = $page;
  41. $result['total'] = $totalPage;
  42. $result['records'] = $totalCount;
  43. $result['rows'] = $lists;
  44. return json($result);
  45. }else{
  46. $commons = model('Roles')->getCommonList(1);
  47. $this->assign('commons',$commons);
  48. return $this->fetch();
  49. }
  50. }
  51. public function types(){
  52. if(request()->isAjax()){
  53. //分页参数
  54. $length = input('rows',10,'intval'); //每页条数
  55. $page = input('page',1,'intval'); //第几页
  56. $start = ($page - 1) * $length; //分页开始位置
  57. //排序
  58. $sortRow = input('sidx','id','trim'); //排序列
  59. $sort = input('sord','desc','trim'); //排序方式
  60. $order = $sortRow.' '.$sort;
  61. $title = input('name','','trim');
  62. if($title){
  63. $map[] = ['name','like','%'.$title.'%'];
  64. }
  65. $map[] = ['enable','=',1];
  66. $map[] = ['parent_id','=',0];
  67. $map[] = ['type','=',1];
  68. $map[] = ['del','=',0];
  69. $map= empty($map) ? true: $map;
  70. //数据查询
  71. $lists = Db::name('roles')->where($map)->limit($start,$length)->order(['sort'=>'asc','id'=>'asc'])->select();
  72. //数据返回
  73. $totalCount = Db::name('roles')->where($map)->count();
  74. $totalPage = ceil($totalCount/$length);
  75. $result['page'] = $page;
  76. $result['total'] = $totalPage;
  77. $result['records'] = $totalCount;
  78. $result['rows'] = $lists;
  79. return json($result);
  80. }else{
  81. return $this->fetch();
  82. }
  83. }
  84. /**
  85. * 新增/编辑
  86. */
  87. public function add($id=0){
  88. if(request()->isPost()){
  89. $res = model('Roles')->updates();
  90. if($res){
  91. $this->success('操作成功',url('index'));
  92. }else{
  93. $this->error(model('Roles')->getError());
  94. }
  95. }else{
  96. if($id){
  97. $info = db('roles')->where('id',$id)->find();
  98. $info['work_type_mode'] = $info['work_type_mode']?explode(',',$info['work_type_mode']):[];
  99. $this->assign('info',$info);
  100. }
  101. $workType = Db::name('work_type_mode')
  102. ->where('type',1)
  103. ->field('id,name as title')
  104. ->select();
  105. $commons = model('Roles')->getCommonList(1);
  106. $this->assign('commons',$commons);
  107. $this->assign('workType',$workType);
  108. return $this->fetch();
  109. }
  110. }
  111. /**
  112. * 删除记录
  113. * @param int $id
  114. */
  115. public function del($id=0){
  116. if(!$id){
  117. $this->error('参数错误');
  118. }
  119. $res = db('roles')->where('id',$id)->where('org_id',$this->orgId)->setField('del',1);
  120. if($res){
  121. $this->success('删除成功');
  122. }else{
  123. $this->error('删除失败');
  124. }
  125. }
  126. /**
  127. * 改变字段值
  128. * @param int $fv
  129. * @param string $fn
  130. * @param int $fv
  131. */
  132. public function changeField($id=0,$fn='',$fv=0){
  133. if(!$fn||!$id){
  134. $this->error('参数错误');
  135. }
  136. $res = db('roles')->where('id',$id)->where('org_id',$this->orgId)->setField($fn,$fv);
  137. if($res){
  138. $this->success('操作成功');
  139. }else{
  140. $this->error('操作失败');
  141. }
  142. }
  143. /**
  144. * 授权
  145. */
  146. public function auth(){
  147. if(request()->isPost()){
  148. $res = model('Roles')->authSave();
  149. if($res){
  150. $this->success('操作成功',url('index'));
  151. }else{
  152. $this->error(model('Roles')->getError());
  153. }
  154. }else{
  155. $type = input('type/d',0);
  156. $cate = input('cate/d',0);
  157. $rolesId = input('id/d',0);
  158. $info = Db::name('roles')->where('id',$rolesId)->find();
  159. if(!$info){
  160. $this->error('数据不存在');
  161. }
  162. if($cate==0){//后台权限
  163. $meuns = model('Menu')->getOrgAllMenuTree_new($this->orgId);
  164. $this->assign('menus',$meuns);
  165. if($type == 1){ // 推荐设置
  166. $sauth = model('Roles')->getRolesAuths($info['parent_id'],1);
  167. $this->assign('sauth',$sauth);
  168. }else{
  169. $sauth = model('Roles')->getRolesAuths($rolesId,1);
  170. $this->assign('sauth',$sauth);
  171. }
  172. }else{//app权限
  173. $appauths = model('AppIcon')->getOrgAllIconTree_new($this->orgId);
  174. $this->assign('menus',$appauths);
  175. if($type == 1){ // 推荐设置
  176. $sappauth = model('Roles')->getRolesAuths($info['parent_id'],2);
  177. $this->assign('sauth',$sappauth);
  178. }else{
  179. $sappauth = model('Roles')->getRolesAuths($rolesId,2);
  180. $this->assign('sauth',$sappauth);
  181. }
  182. }
  183. // $sauth = model('Roles')->getRolesAuths($rolesId,1);
  184. // $this->assign('sauth',$sauth);
  185. // $appauths = model('AppIcon')->getOrgAllIconTree($this->orgId);
  186. // $this->assign('appauths',$appauths);
  187. // $sappauth = model('Roles')->getRolesAuths($rolesId,2);
  188. // $this->assign('sappauth',$sappauth);
  189. $this->assign('rolesId',$rolesId);
  190. $this->assign('cate',$cate);
  191. return $this->fetch();
  192. }
  193. }
  194. /**
  195. * 授权
  196. */
  197. public function auth2(){
  198. if(request()->isPost()){
  199. $res = model('Roles')->authSave();
  200. if($res){
  201. $this->success('操作成功',url('index'));
  202. }else{
  203. $this->error(model('Roles')->getError());
  204. }
  205. }else{
  206. $rolesId = input('id/d',0);
  207. $meuns = model('Menu')->getAllMenuTree();
  208. $this->assign('menus',$meuns);
  209. $sauth = model('Roles')->getRolesAuths($rolesId,1);
  210. $this->assign('sauth',$sauth);
  211. $appauths = model('AppIcon')->getAllIconTree(0,1);
  212. $this->assign('appauths',$appauths);
  213. $sappauth = model('Roles')->getRolesAuths($rolesId,2);
  214. $this->assign('sappauth',$sappauth);
  215. $this->assign('rolesId',$rolesId);
  216. return $this->fetch();
  217. }
  218. }
  219. public function batchSort(){
  220. $data = input('data','','trim');
  221. if(!$data){
  222. $this->error('参数错误');
  223. }
  224. $data = json_decode($data,true);
  225. if(!$data){
  226. $this->error('参数错误');
  227. }
  228. Db::startTrans();
  229. try{
  230. foreach ($data as $k=>$v){
  231. Db::name('roles')->where('id',$v['id'])->setField('sort',$v['sort']);
  232. }
  233. Db::commit();
  234. }catch (Exception $e){
  235. Db::rollback();
  236. $this->error('操作失败');
  237. }
  238. $this->success('操作成功');
  239. }
  240. }