Role.php 13 KB

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