Role.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. $token = isset($post['token']) ? trim($post['token']) : "";
  59. if($token==""){
  60. return error_show(101,'token不能为空');
  61. }
  62. $effetc = VerifyTokens($token);
  63. if(!empty($effetc) && $effetc['code']!=0){
  64. return error_show($effetc['code'],$effetc['message']);
  65. }
  66. $uid= isset($effetc['data']['id']) ? $effetc['data']['id']: "0";
  67. $username = isset($effetc['data']['nickname']) ? $effetc['data']['nickname']: "";
  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"=>"",
  119. ];
  120. $role= Db::name("role")->insert($list,true);
  121. // $st = ["order_code"=>$role_code,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  122. // ActionLog::logAdd($token,$st,"role",1,$st);
  123. if($role>0){
  124. $role_action = [
  125. "role_id"=>$role,
  126. "action_conllect"=>$action,
  127. "action_data"=>$data,
  128. "private_data"=>$private_data,
  129. "status"=>1,
  130. "addtime"=>date("Y-m-d H:i:s"),
  131. "updatetime"=>date("Y-m-d H:i:s"),
  132. ];
  133. $roleaction= Db::name("role_action")->insert($role_action,true);
  134. if($roleaction){
  135. // $sty = ["order_code"=>$role,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  136. // ActionLog::logAdd($token,$sty,"role_action",1,$sty);
  137. Db::commit();
  138. return app_show(0,"新建成功");
  139. }else{
  140. Db::rollback();
  141. return error_show(1005,"权限录入失败");
  142. }
  143. }else{
  144. Db::rollback();
  145. return error_show(1006,"角色新建失败");
  146. }
  147. }catch (\Exception $e){
  148. Db::rollback();
  149. return error_show(1008,$e->getMessage());
  150. }
  151. }
  152. /**
  153. * @param roleid
  154. * @return \think\response\Json
  155. * @throws \think\exception\DbException
  156. */
  157. public function RoleInfo(){
  158. $post =$this->request->post();
  159. $roleid= isset($post['roleid']) ? trim($post['roleid']) : "";
  160. if($roleid==""){
  161. return error_show(1001,'roleid不能为空');
  162. }
  163. $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();
  164. if(!$info){
  165. return error_show(1002,"未找到对应的数据");
  166. }
  167. $info['item_name'] ='';
  168. if($info['item']!=''){
  169. $iteminfo = Db::name('company_item')->where('id',"=",$info['item'])->find();
  170. $info['item_name'] = isset($iteminfo['name'])? $iteminfo['name']:"";
  171. }
  172. $info['action'] =explode(",",$info['action_conllect']);
  173. $info['action_data'] =$info['action_data']!=""?explode(",",$info['action_data']):"";
  174. $info['private_data'] =$info['private_data']!=""?explode(",",$info['private_data']) :"";
  175. return app_show(0,"获取成功",$info);
  176. }
  177. /**
  178. * @param role_name
  179. * @param level
  180. * @param action
  181. * @param action_data
  182. * @param roleid
  183. * @return \think\response\Json
  184. * @throws \think\exception\DbException
  185. */
  186. public function RoleSave(){
  187. $post =$this->request->post();
  188. $token = isset($post['token']) ? trim($post['token']) : "";
  189. if($token==""){
  190. return error_show(101,'token不能为空');
  191. }
  192. $effetc = VerifyTokens($token);
  193. if(!empty($effetc) && $effetc['code']!=0){
  194. return error_show($effetc['code'],$effetc['message']);
  195. }
  196. $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
  197. if($roleid==""){
  198. return error_show(1001,'roleid不能为空');
  199. }
  200. $info =Db::name("role")->where("id","=",$roleid)->find();
  201. if(!$info){
  202. return error_show(1002,"未找到对应的数据");
  203. }
  204. $rolename = isset($post['role_name']) ? trim($post['role_name']) : "";
  205. if($rolename==""){
  206. return error_show(1002,"角色名称不能为空");
  207. }
  208. $vers= Db::name("role")->where("id",'<>',$roleid)->where("role_name","=",$rolename)->find();
  209. if($vers){
  210. return error_show(1002,"角色名已存在");
  211. }
  212. $role_code= isset($post['role_code'])&&$post['role_code']!=''?trim($post['role_code']) :"";
  213. // if($role_code==""){
  214. // return error_show(1002,"参数role_code 不能为空");
  215. // }
  216. // $isCode=Db::name("role")->where(['role_code'=>$role_code])->find();
  217. // if($isCode){
  218. // return error_show(1002,"角色编号已存在");
  219. // }
  220. // $item = isset($post['item']) ? trim($post['item']) : "";
  221. // if($item==""){
  222. // return error_show(1003,"参数item 不能为空");
  223. // }
  224. // $iteminfo = Db::name('company_item')->where('id',"=",$item)->find();
  225. // if($iteminfo['is_del']==1){
  226. // return error_show(1003,"该部门不存在");
  227. // }
  228. $level = isset($post['level']) ? intval($post['level']) : 0;
  229. $remark = isset($post['remark']) ? trim($post['remark']) : '';
  230. $action = isset($post['action'])&&is_array($post['action']) ? implode(",",$post['action']) : "";
  231. if($action==""){
  232. return error_show(1004,"功能权限不能为空");
  233. }
  234. $data = isset($post['action_data']) &&is_array($post['action_data'])?implode(",",$post['action_data']): "";
  235. $private_data = isset($post['private_data']) &&is_array($post['private_data'])?implode(",",$post['private_data']): "";
  236. // if($data==""){
  237. // return error_show(1004,"字段权限不能为空");
  238. // }
  239. Db::startTrans();
  240. try {
  241. $list = [
  242. "role_name"=>$rolename,
  243. "level"=>$level,
  244. 'desc'=>$remark,
  245. 'role_code'=>$role_code,
  246. 'item'=>"",
  247. "updatetime"=>date("Y-m-d H:i:s"),
  248. 'id'=>$roleid
  249. ];
  250. $role= Db::name("role")->save($list);
  251. // $stm = ["order_code"=>"YS","status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  252. // ActionLog::logAdd($token,$stm,"resign_info",0,$stm);
  253. if($role){
  254. $role_action = [
  255. "role_id"=>$roleid,
  256. "action_conllect"=>$action,
  257. "action_data"=>$data,
  258. "private_data"=>$private_data,
  259. "updatetime"=>date("Y-m-d H:i:s")
  260. ];
  261. $roleaction= Db::name("role_action")->where("role_id","=",$roleid)->update($role_action);
  262. if($roleaction){
  263. // $stl = ["order_code"=>"YS","status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  264. // ActionLog::logAdd($token,$stl,"role_action",0,$stl);
  265. Db::commit();
  266. return app_show(0,"更新成功");
  267. }else{
  268. Db::rollback();
  269. return app_show(1005,"权限更新失败");
  270. }
  271. }else{
  272. Db::rollback();
  273. return app_show(1006,"角色更新失败");
  274. }
  275. }catch (\Exception $e){
  276. Db::rollback();
  277. return error_show(1008,$e->getMessage());
  278. }
  279. }
  280. /**
  281. * @return \think\response\Json
  282. * @throws \think\exception\DbException
  283. */
  284. public function RoleStatus(){
  285. $post =$this->request->post();
  286. $token = isset($post['token']) ? trim($post['token']) : "";
  287. if($token==""){
  288. return error_show(101,'token不能为空');
  289. }
  290. $effetc = VerifyTokens($token);
  291. if(!empty($effetc) && $effetc['code']!=0){
  292. return error_show($effetc['code'],$effetc['message']);
  293. }
  294. $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
  295. if($roleid==""){
  296. return error_show(1001,'roleid不能为空');
  297. }
  298. $status = isset($post['status']) ? intval($post['status']) : "";
  299. if($status===""){
  300. return error_show(1001,'status不能为空');
  301. }
  302. if(!in_array($status,[0,1])){
  303. return error_show(1001,'status参数非法');
  304. }
  305. Db::startTrans();
  306. try {
  307. // $re = PasRoleList::update(['status'=>$status,'update'=>date("Y-m-d H:i:s")],['id'=>$roleid]);
  308. $dat=[
  309. 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s"), 'id'=>$roleid
  310. ];
  311. $re = Db::name("role")->save($dat);
  312. if($re){
  313. $dat2=[
  314. 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s")
  315. ];
  316. $action = Db::name("role_action")->where("role_id","=",$roleid)->update($dat2);
  317. if($action){
  318. // $sti = ["order_code"=>$roleid,"status"=>$status,"action_remark"=>'',"action_type"=>"edit"];
  319. // ActionLog::logAdd($token,$sti,"role_action",$status,$sti);
  320. Db::commit();
  321. return app_show(0,"状态更新成功");
  322. }
  323. }
  324. Db::rollback();
  325. return error_show(1003,"状态更新失败");
  326. }catch (\Exception $e){
  327. Db::rollback();
  328. return error_show(1002,$e->getMessage());
  329. }
  330. }
  331. /**
  332. * @return \think\response\Json
  333. * @throws \think\exception\DbException
  334. */
  335. public function RoleAll(){
  336. $post =$this->request->post();
  337. $list =Db::name("role")->select();
  338. return app_show(0,"获取成功",$list);
  339. }
  340. }