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