Share.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\Request;
  5. use think\facade\Db;
  6. use app\admin\model\ActionLog;
  7. //角色权限共享
  8. class Share extends BaseController
  9. {
  10. /**
  11. * 显示资源列表
  12. *
  13. * @return \think\Response
  14. */
  15. public function index()
  16. {
  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. $page = isset($post['page']) ? intval($post['page']): 1;
  27. $size = isset($post['size']) ? intval($post['size']):10;
  28. $condition=[];
  29. $condition[]=["is_del","=",0];
  30. $count =Db::name("role_share")->where($condition)->count();
  31. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  32. $page = $page>=$total?intval($total):$page;
  33. $list = Db::name("role_share")->where($condition)->page($page,$size)->select();
  34. $result = [];
  35. foreach ($list as $key=>$val){
  36. $temp = [];
  37. $temp['id']=$val['id'];
  38. $menu = Db::name("admin_menu")->where("id in ({$val['action_collect']})")->column("menu_name");
  39. $temp['actionlist']=implode(",",$menu);
  40. $userid = [];
  41. $val['share_user']!=""&&$val['share_user']!=0 ?$userid[]=$val['share_user']:"";
  42. $val['to_user']!=""&&$val['to_user']!=0 ?$userid[]=$val['to_user']:"";
  43. $cond = ['id' => $userid];
  44. $user = GetUserlist($token, $cond);
  45. $share_name = "";
  46. $to_name = "";
  47. if ($user['code'] == 0 && !empty($user['data'])) {
  48. foreach ($user['data'] as $v) {
  49. if($val['share_user']==$v['id']){
  50. $share_name= isset($v['nickname']) ?$v['nickname']:"";
  51. }
  52. if($val['to_user']==$v['id']){
  53. $to_name= isset($v['nickname']) ?$v['nickname']:"";
  54. }
  55. }
  56. }
  57. $temp['share_user']=$share_name;
  58. $togroup = $val['to_group']!=""&&$val['to_group']!=0?Db::name("role_group")->where("id","=",
  59. $val["to_group"])->find()
  60. :['group_name'=>""];
  61. $temp['to_user']=$to_name;
  62. $temp['to_group']=$togroup['group_name'];
  63. $temp['status']=$val['status'];
  64. $temp['action']=$val['action'];
  65. $temp['addtime']=$val['addtime'];
  66. array_push($result,$temp);
  67. }
  68. return app_show(0,"获取成功",['list'=>$result,"count"=>$count]);
  69. }
  70. /**
  71. * 显示创建资源表单页.
  72. *
  73. * @return \think\Response
  74. */
  75. public function create()
  76. {
  77. $post =$this->request->post();
  78. $token = isset($post['token']) ? trim($post['token']) : "";
  79. if($token==""){
  80. return error_show(101,'token不能为空');
  81. }
  82. $effetc = VerifyTokens($token);
  83. if(!empty($effetc) && $effetc['code']!=0){
  84. return error_show($effetc['code'],$effetc['message']);
  85. }
  86. $collect = isset($post['collect']) ? trim($post['collect']):"";
  87. $user = isset($post['userid']) ? intval($post['userid']):"";
  88. $touser = isset($post['touserid']) ? intval($post['touserid']):"";
  89. $togroup = isset($post['togroupid']) ? intval($post['togroupid']):"";
  90. $action = isset($post['action'])&&$post['action']!=='' ? trim($post['action']):"";
  91. if($collect==""){
  92. return error_show(1002,"菜单数据不能为空");
  93. }
  94. if($user==""){
  95. return error_show(1003,"数据源用户不能为空");
  96. }
  97. if($touser=="" && $togroup==""){
  98. return error_show(1004,"共享用户或用户组不能为空");
  99. }
  100. $data=[
  101. "action_collect"=>$collect,
  102. "share_user"=>$user,
  103. "to_user"=>$touser==0?"":$touser,
  104. "to_group"=>$togroup==0?'':$togroup,
  105. "action"=>$action==0?'':$action,
  106. "status"=>1,
  107. "addtime"=>date("Y-m-d H:i:s"),
  108. "updatetime"=>date("Y-m-d H:i:s")
  109. ];
  110. $result= Db::name("role_share")->save($data);
  111. $st = ["order_code"=>$collect,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  112. ActionLog::logAdd($token,$st,"role_share",1,$st);
  113. return $result ? app_show(0,"新建成功"):error_show(1006,"新建失败");
  114. }
  115. /**
  116. * 保存新建的资源
  117. *
  118. * @param \think\Request $request
  119. * @return \think\Response
  120. */
  121. public function save()
  122. {
  123. $post =$this->request->post();
  124. $token = isset($post['token']) ? trim($post['token']) : "";
  125. if($token==""){
  126. return error_show(1001,'token不能为空');
  127. }
  128. $effetc = VerifyTokens($token);
  129. if(!empty($effetc) && $effetc['code']!=0){
  130. return error_show($effetc['code'],$effetc['message']);
  131. }
  132. $id = isset($post['id'])&&$post['id']!="" ? intval($post['id']):"";
  133. if($id===""){
  134. return error_show(1002,"参数id不能为空");
  135. }
  136. $collect = isset($post['collect'])&&$post['collect']!="" ? trim($post['collect']):"";
  137. $user = isset($post['userid'])&&$post['userid']!="" ? intval($post['userid']):"";
  138. $touser = isset($post['touserid'])&&$post['touserid']!="" ? intval($post['touserid']):"";
  139. $togroup = isset($post['togroupid'])&&$post['togroupid']!="" ? intval($post['togroupid']):"";
  140. $action = isset($post['action'])&&$post['action']!="" ? trim($post['action']):"";
  141. $info = Db::name("role_share")->where("id","=",$id)->find();
  142. if(!$info){
  143. return error_show(1002,"未找到对应数据");
  144. }
  145. if($collect==""){
  146. return error_show(1002,"菜单数据不能为空");
  147. }
  148. if($user==""){
  149. return error_show(1003,"数据源用户不能为空");
  150. }
  151. if($touser=="" && $togroup==""){
  152. return error_show(1004,"共享用户或用户组不能为空");
  153. }
  154. // if($action===""){
  155. // return error_show(1005,"共享数据权限不能为空");
  156. // }
  157. $data=[
  158. "action_collect"=>$collect,
  159. "share_user"=>$user,
  160. "to_user"=>$touser==0?"":$touser,
  161. "to_group"=>$togroup==0?'':$togroup,
  162. "action"=>$action==0?'':$action,
  163. "updatetime"=>date("Y-m-d H:i:s")
  164. ];
  165. $result= Db::name("role_share")->where("id","=",$id)->update($data);
  166. $st = ["order_code"=>$collect,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  167. ActionLog::logAdd($token,$st,"role_share",0,$st);
  168. return $result ? app_show(0,"更新成功"):error_show(1006,"更新失败");
  169. }
  170. /**
  171. * 显示指定的资源
  172. *
  173. * @param int $id
  174. * @return \think\Response
  175. */
  176. public function read()
  177. {
  178. $post =$this->request->post();
  179. $token = isset($post['token']) ? trim($post['token']) : "";
  180. if($token==""){
  181. return error_show(1001,'token不能为空');
  182. }
  183. $effetc = VerifyTokens($token);
  184. if(!empty($effetc) && $effetc['code']!=0){
  185. return error_show($effetc['code'],$effetc['message']);
  186. }
  187. $id = isset($post['id'])&&$post['id']!="" ? intval($post['id']):"";
  188. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  189. if(!$info){
  190. return error_show(1002,"未找到对应数据");
  191. }
  192. $menu =Db::name("admin_menu")->where("id in ({$info['action_collect']})")->column("menu_name");
  193. $info['actionlist']=implode(",",$menu);
  194. $userid = [];
  195. $info['share_user']!=""&&$info['share_user']!=0 ?$userid[]=$info['share_user']:"";
  196. $info['to_user']!=""&&$info['to_user']!=0 ?$userid[]=$info['to_user']:"";
  197. $cond = ['id' => $userid];
  198. $user = GetUserlist($token, $cond);
  199. $share_name = "";
  200. $to_name = "";
  201. if ($user['code'] == 0 && !empty($user['data'])) {
  202. foreach ($user['data'] as $v) {
  203. if($info['share_user']==$v['id']){
  204. $share_name= isset($v['nickname']) ?$v['nickname']:"";
  205. }
  206. if($info['to_user']==$v['id']){
  207. $to_name= isset($v['nickname']) ?$v['nickname']:"";
  208. }
  209. }
  210. }
  211. $info['to_group']==0 ? $info['to_group']='':"";
  212. $info['to_user']==0 ? $info['to_user']='':"";
  213. $togroup= $info['to_group']!="" ?Db::name("role_group")->where("id","=",$info["to_group"])->find()
  214. :['group_name'=>""];
  215. $info['share_user_name']=$share_name;
  216. $info['to_user_name'] = $to_name;
  217. $info['to_group_name'] = $togroup['group_name'];
  218. return app_show(0,"获取成功",$info);
  219. }
  220. /**
  221. * 显示编辑资源表单页.
  222. *
  223. * @param int $id
  224. * @return \think\Response
  225. */
  226. public function status()
  227. {
  228. $post =$this->request->post();
  229. $token = isset($post['token']) ? trim($post['token']) : "";
  230. if($token==""){
  231. return error_show(1001,'token不能为空');
  232. }
  233. $effetc = VerifyTokens($token);
  234. if(!empty($effetc) && $effetc['code']!=0){
  235. return error_show($effetc['code'],$effetc['message']);
  236. }
  237. $id = isset($post['id']) ? intval($post['id']):"";
  238. if($id==""){
  239. return error_show(1002,"参数id 不能为空");
  240. }
  241. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  242. if(!$info){
  243. return error_show(1002,"未找到对应数据");
  244. }
  245. $status = isset($post['status']) && $post['status']!==""? intval($post['status']):"";
  246. if($status===""){
  247. return error_show(1002,"参数status 不能为空");
  248. }
  249. if(!in_array($status,[0,1])){
  250. return error_show(1002,"参数status 无效");
  251. }
  252. $item=$info['status'];
  253. $info['status']=$status;
  254. $info['updatetime']=date("Y-m-d H:i:s");
  255. $msg = $status==1?"启用":"禁用";
  256. $update = Db::name("role_share")->save($info);
  257. $st = ["order_code"=>$id,"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  258. ActionLog::logAdd($token,$st,"role_share",$info['status'],$st);
  259. return $update? app_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  260. }
  261. /**
  262. * 保存更新的资源
  263. *
  264. * @param \think\Request $request
  265. * @param int $id
  266. * @return \think\Response
  267. */
  268. public function update(Request $request, $id)
  269. {
  270. //
  271. }
  272. /**
  273. * 删除指定资源
  274. *
  275. * @param int $id
  276. * @return \think\Response
  277. */
  278. public function delete()
  279. {
  280. $post =$this->request->post();
  281. $token = isset($post['token']) ? trim($post['token']) : "";
  282. if($token==""){
  283. return error_show(1001,'token不能为空');
  284. }
  285. $effetc = VerifyTokens($token);
  286. if(!empty($effetc) && $effetc['code']!=0){
  287. return error_show($effetc['code'],$effetc['message']);
  288. }
  289. $id = isset($post['id']) ? intval($post['id']):"";
  290. if($id==""){
  291. return error_show(1002,"参数id 不能为空");
  292. }
  293. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  294. if(!$info){
  295. return error_show(1002,"未找到对应数据");
  296. }
  297. $teme = $info['status'];
  298. $info["status"]=0;
  299. $info["is_del"]=1;
  300. $info["updatetime"]=date("Y-m-d H:i:s");
  301. $update = Db::name("role_share")->save($info);
  302. $st = ["order_code"=>$id,"status"=> $teme,"action_remark"=>'',"action_type"=>"delete"];
  303. ActionLog::logAdd($token,$st,"role_share",$info['status'],$st);
  304. return $update? app_show(0,"删除成功"):error_show(1004,"删除失败");
  305. }
  306. }