Role.php 15 KB

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