Role.php 14 KB

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