Share.php 13 KB

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