Role.php 11 KB

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