Role.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. class Role extends BaseController
  7. {
  8. public function __construct(App $app)
  9. {
  10. parent::__construct($app);
  11. $post =$this->request->post();
  12. $token = isset($post['token']) ? trim($post['token']) : "";
  13. if($token==""){
  14. return error_show(101,'token不能为空');
  15. }
  16. $effetc = VerifyTokens($token);
  17. if(!empty($effetc) && $effetc['code']!=0){
  18. return error_show($effetc['code'],$effetc['message']);
  19. }
  20. }
  21. /**
  22. * @param status
  23. * @param role_name
  24. * @param level
  25. * @param page
  26. * @param size
  27. * @return \think\response\Json
  28. * @throws \think\exception\DbException
  29. */
  30. public function RoleList(){
  31. $post =$this->request->post();
  32. $condition=[];
  33. isset($post['status'])&&$post['status']!=="" ? $condition[]=['a.status',"=",$post['status']]:"";
  34. isset($post['role_name'])&&$post['role_name']!=="" ? $condition[]=['a.role_name',"like","%".$post['role_name']."%"]:"";
  35. isset($post['item'])&&$post['item']!=="" ? $condition[]=["a.item","=",$post['item']]:"";
  36. isset($post['createrId'])&&$post['createrId']!=="" ? $condition[]=["a.createrId","=",$post['createrId']]:"";
  37. $roleList = Db::name("role");
  38. $count =$roleList->alias("a")->where($condition)->count();
  39. $page = isset($post['page'])&& $post['page']!=''? intval($post['page']) : 1;
  40. $size = isset($post['size'])&& $post['size']!=''? intval($post['size']) : 10;
  41. $page >=ceil($count/$size) ? $page=ceil($count/$size) :"";
  42. $list=$roleList->alias("a")->leftJoin("role_action t","a.id=t.role_id")->leftJoin("company_item i","a
  43. .item=i.id")->field("a.*,i.name as item_name,t.action_conllect as action,t.action_data")->where($condition)
  44. ->page($page,$size)->select();
  45. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  46. }
  47. /**
  48. * @param role_name
  49. * @param level
  50. * @param action
  51. * @param action_data
  52. * @param role_name
  53. * @return \think\response\Json
  54. * @throws \think\exception\DbException
  55. */
  56. public function RoleAdd(){
  57. $post =$this->request->post();
  58. $creater= GetUserInfo($post['token']);
  59. if(isset($creater['code'])&&$creater['code']==0){
  60. $uid= isset($userinfo['data']['id']) ? $userinfo['data']['id']: "0";
  61. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  62. }else{
  63. $username = '';
  64. $uid= 0;
  65. }
  66. $rolename = isset($post['role_name']) ? trim($post['role_name']) : "";
  67. if($rolename==""){
  68. return error_show(1002,"角色名称不能为空");
  69. }
  70. $isT=Db::name("role")->where(['role_name'=>$rolename])->find();
  71. if($isT){
  72. return error_show(1002,"角色名称已存在");
  73. }
  74. $role_code= isset($post['role_code'])&&$post['role_code']!=''?trim($post['role_code']) :"";
  75. // if($role_code==""){
  76. // return error_show(1002,"参数role_code 不能为空");
  77. // }
  78. // $isCode=Db::name("role")->where(['role_code'=>$role_code])->find();
  79. // if($isCode){
  80. // return error_show(1002,"角色编号已存在");
  81. // }
  82. // $item = isset($post['item']) ? trim($post['item']) : "";
  83. // if($item==""){
  84. // return error_show(1003,"参数item 不能为空");
  85. // }
  86. // $iteminfo = Db::name('company_item')->where('id',"=",$item)->find();
  87. // if(empty($iteminfo) || $iteminfo['is_del']==1){
  88. // return error_show(1003,"该部门不存在");
  89. // }
  90. $level = isset($post['level']) ? intval($post['level']) : 0;
  91. $remark = isset($post['remark']) ? trim($post['remark']) : '';
  92. // if($level==""){
  93. // return error_show(1003,"参数不能为空");
  94. // }
  95. $action = isset($post['action'])&&is_array($post['action']) ? implode(",",$post['action']) : "";
  96. if($action==""){
  97. return error_show(1004,"功能权限不能为空");
  98. }
  99. $data = isset($post['action_data']) &&is_array($post['action_data'])?implode(",",$post['action_data']): "";
  100. $private_data = isset($post['private_data']) &&is_array($post['private_data'])?implode(",",$post['private_data']): "";
  101. // if($data==""){
  102. // return error_show(1004,"字段权限不能为空");
  103. // }
  104. Db::startTrans();
  105. try {
  106. $list = [
  107. "role_name"=>$rolename,
  108. "role_code"=>$role_code,
  109. "status"=>1,
  110. 'creater'=>$username,
  111. 'createrId'=>$uid,
  112. "addtime"=>date("Y-m-d H:i:s"),
  113. "updatetime"=>date("Y-m-d H:i:s"),
  114. "level"=>$level,
  115. "desc"=>$remark,
  116. "item"=>"",
  117. ];
  118. $role= Db::name("role")->insert($list,true);
  119. if($role>0){
  120. $role_action = [
  121. "role_id"=>$role,
  122. "action_conllect"=>$action,
  123. "action_data"=>$data,
  124. "private_data"=>$private_data,
  125. "status"=>1,
  126. "addtime"=>date("Y-m-d H:i:s"),
  127. "updatetime"=>date("Y-m-d H:i:s"),
  128. ];
  129. $roleaction= Db::name("role_action")->insert($role_action,true);
  130. if($roleaction){
  131. Db::commit();
  132. return app_show(0,"新建成功");
  133. }else{
  134. Db::rollback();
  135. return error_show(1005,"权限录入失败");
  136. }
  137. }else{
  138. Db::rollback();
  139. return error_show(1006,"角色新建失败");
  140. }
  141. }catch (\Exception $e){
  142. Db::rollback();
  143. return error_show(1008,$e->getMessage());
  144. }
  145. }
  146. /**
  147. * @param roleid
  148. * @return \think\response\Json
  149. * @throws \think\exception\DbException
  150. */
  151. public function RoleInfo(){
  152. $post =$this->request->post();
  153. $roleid= isset($post['roleid']) ? trim($post['roleid']) : "";
  154. if($roleid==""){
  155. return error_show(1001,'roleid不能为空');
  156. }
  157. $info = $list=Db::name("role")->alias("a")->leftJoin("role_action t","a.id=t.role_id")->field("a.*,t.action_conllect,t.action_data,t.private_data")->where("a.id","=",$roleid)->find();
  158. if(!$info){
  159. return error_show(1002,"未找到对应的数据");
  160. }
  161. $info['item_name'] ='';
  162. if($info['item']!=''){
  163. $iteminfo = Db::name('company_item')->where('id',"=",$info['item'])->find();
  164. $info['item_name'] = isset($iteminfo['name'])? $iteminfo['name']:"";
  165. }
  166. $info['action'] =explode(",",$info['action_conllect']);
  167. $info['action_data'] =$info['action_data']!=""?explode(",",$info['action_data']):"";
  168. $info['private_data'] =$info['private_data']!=""?explode(",",$info['private_data']) :"";
  169. return app_show(0,"获取成功",$info);
  170. }
  171. /**
  172. * @param role_name
  173. * @param level
  174. * @param action
  175. * @param action_data
  176. * @param roleid
  177. * @return \think\response\Json
  178. * @throws \think\exception\DbException
  179. */
  180. public function RoleSave(){
  181. $post =$this->request->post();
  182. $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
  183. if($roleid==""){
  184. return error_show(1001,'roleid不能为空');
  185. }
  186. $info =Db::name("role")->where("id","=",$roleid)->find();
  187. if(!$info){
  188. return error_show(1002,"未找到对应的数据");
  189. }
  190. $rolename = isset($post['role_name']) ? trim($post['role_name']) : "";
  191. if($rolename==""){
  192. return error_show(1002,"角色名称不能为空");
  193. }
  194. $vers= Db::name("role")->where("id",'<>',$roleid)->where("role_name","=",$rolename)->find();
  195. if($vers){
  196. return error_show(1002,"角色名已存在");
  197. }
  198. $role_code= isset($post['role_code'])&&$post['role_code']!=''?trim($post['role_code']) :"";
  199. // if($role_code==""){
  200. // return error_show(1002,"参数role_code 不能为空");
  201. // }
  202. // $isCode=Db::name("role")->where(['role_code'=>$role_code])->find();
  203. // if($isCode){
  204. // return error_show(1002,"角色编号已存在");
  205. // }
  206. // $item = isset($post['item']) ? trim($post['item']) : "";
  207. // if($item==""){
  208. // return error_show(1003,"参数item 不能为空");
  209. // }
  210. // $iteminfo = Db::name('company_item')->where('id',"=",$item)->find();
  211. // if($iteminfo['is_del']==1){
  212. // return error_show(1003,"该部门不存在");
  213. // }
  214. $level = isset($post['level']) ? intval($post['level']) : 0;
  215. $remark = isset($post['remark']) ? trim($post['remark']) : '';
  216. $action = isset($post['action'])&&is_array($post['action']) ? implode(",",$post['action']) : "";
  217. if($action==""){
  218. return error_show(1004,"功能权限不能为空");
  219. }
  220. $data = isset($post['action_data']) &&is_array($post['action_data'])?implode(",",$post['action_data']): "";
  221. $private_data = isset($post['private_data']) &&is_array($post['private_data'])?implode(",",$post['private_data']): "";
  222. // if($data==""){
  223. // return error_show(1004,"字段权限不能为空");
  224. // }
  225. Db::startTrans();
  226. try {
  227. $list = [
  228. "role_name"=>$rolename,
  229. "level"=>$level,
  230. 'desc'=>$remark,
  231. 'role_code'=>$role_code,
  232. 'item'=>"",
  233. "updatetime"=>date("Y-m-d H:i:s"),
  234. 'id'=>$roleid
  235. ];
  236. $role= Db::name("role")->save($list);
  237. if($role){
  238. $role_action = [
  239. "role_id"=>$roleid,
  240. "action_conllect"=>$action,
  241. "action_data"=>$data,
  242. "private_data"=>$private_data,
  243. "updatetime"=>date("Y-m-d H:i:s")
  244. ];
  245. $roleaction= Db::name("role_action")->where("role_id","=",$roleid)->update($role_action);
  246. if($roleaction){
  247. Db::commit();
  248. return app_show(0,"更新成功");
  249. }else{
  250. Db::rollback();
  251. return app_show(1005,"权限更新失败");
  252. }
  253. }else{
  254. Db::rollback();
  255. return app_show(1006,"角色更新失败");
  256. }
  257. }catch (\Exception $e){
  258. Db::rollback();
  259. return error_show(1008,$e->getMessage());
  260. }
  261. }
  262. /**
  263. * @return \think\response\Json
  264. * @throws \think\exception\DbException
  265. */
  266. public function RoleStatus(){
  267. $post =$this->request->post();
  268. $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
  269. if($roleid==""){
  270. return error_show(1001,'roleid不能为空');
  271. }
  272. $status = isset($post['status']) ? intval($post['status']) : "";
  273. if($status===""){
  274. return error_show(1001,'status不能为空');
  275. }
  276. if(!in_array($status,[0,1])){
  277. return error_show(1001,'status参数非法');
  278. }
  279. Db::startTrans();
  280. try {
  281. // $re = PasRoleList::update(['status'=>$status,'update'=>date("Y-m-d H:i:s")],['id'=>$roleid]);
  282. $dat=[
  283. 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s"), 'id'=>$roleid
  284. ];
  285. $re = Db::name("role")->save($dat);
  286. if($re){
  287. $dat2=[
  288. 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s")
  289. ];
  290. $action = Db::name("role_action")->where("role_id","=",$roleid)->update($dat2);
  291. if($action){
  292. Db::commit();
  293. return app_show(0,"状态更新成功");
  294. }
  295. }
  296. Db::rollback();
  297. return error_show(1003,"状态更新失败");
  298. }catch (\Exception $e){
  299. Db::rollback();
  300. return error_show(1002,$e->getMessage());
  301. }
  302. }
  303. /**
  304. * @return \think\response\Json
  305. * @throws \think\exception\DbException
  306. */
  307. public function RoleAll(){
  308. $post =$this->request->post();
  309. $list =Db::name("role")->select();
  310. return app_show(0,"获取成功",$list);
  311. }
  312. }