Share.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use think\App;use think\facade\Db;
  5. class Share extends Base
  6. {
  7. public function __construct(App $app) {parent::__construct($app);}
  8. /**
  9. * 权限分享数据列表
  10. *
  11. * @return \think\Response
  12. */
  13. public function index()
  14. {
  15. $post = $this->post;
  16. $page = isset($post['page']) ? intval($post['page']): 1;
  17. $size = isset($post['size']) ? intval($post['size']):10;
  18. $condition=[];
  19. $condition[]=["is_del","=",0];
  20. $count =Db::name("role_share")->where($condition)->count();
  21. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  22. $page = $page>=$total?intval($total):$page;
  23. $list = Db::name("role_share")->where($condition)->page($page,$size)->select();
  24. $result = [];
  25. foreach ($list as $key=>$val){
  26. $temp = [];
  27. $temp['id']=$val['id'];
  28. $menu = Db::name("admin_menu")->where("id in ({$val['action_collect']})")->column("menu_name");
  29. $temp['actionlist']=implode(",",$menu);
  30. $userid = [];
  31. $val['share_user']!=""&&$val['share_user']!=0 ?$userid[]=$val['share_user']:"";
  32. $val['to_user']!=""&&$val['to_user']!=0 ?$userid[]=$val['to_user']:"";
  33. $cond = ['id' => $userid];
  34. $user = GetUserlist($post['token'], $cond);
  35. $share_name = "";
  36. $to_name = "";
  37. if ($user['code'] == 0 && !empty($user['data'])) {
  38. foreach ($user['data'] as $v) {
  39. if($val['share_user']==$v['id']){
  40. $share_name= isset($v['nickname']) ?$v['nickname']:"";
  41. }
  42. if($val['to_user']==$v['id']){
  43. $to_name= isset($v['nickname']) ?$v['nickname']:"";
  44. }
  45. }
  46. }
  47. $temp['share_user']=$share_name;
  48. $togroup = $val['to_group']!=""&&$val['to_group']!=0?Db::name("role_group")->where("id","=",
  49. $val["to_group"])->find()
  50. :['group_name'=>""];
  51. $temp['to_user']=$to_name;
  52. $temp['to_group']=$togroup['group_name'];
  53. $temp['status']=$val['status'];
  54. $temp['action']=$val['action'];
  55. $temp['addtime']=$val['addtime'];
  56. array_push($result,$temp);
  57. }
  58. return app_show(0,"获取成功",['list'=>$result,"count"=>$count]);
  59. }
  60. /**
  61. * 权限分享数据新建
  62. *
  63. * @return \think\Response
  64. */
  65. public function create()
  66. {
  67. $post = $this->post;
  68. $collect = isset($post['collect']) ? trim($post['collect']):"";
  69. $user = isset($post['userid']) ? intval($post['userid']):"";
  70. $touser = isset($post['touserid']) ? intval($post['touserid']):"";
  71. $togroup = isset($post['togroupid']) ? intval($post['togroupid']):"";
  72. $action = isset($post['action']) ? intval($post['action']):"";
  73. if($collect==""){
  74. return error_show(1002,"菜单数据不能为空");
  75. }
  76. if($user==""){
  77. return error_show(1003,"数据源用户不能为空");
  78. }
  79. if($touser=="" && $togroup==""){
  80. return error_show(1004,"共享用户或用户组不能为空");
  81. }
  82. if($action===""){
  83. return error_show(1005,"共享数据权限不能为空");
  84. }
  85. $data=[
  86. "action_collect"=>$collect,
  87. "share_user"=>$user,
  88. "to_user"=>$touser,
  89. "to_group"=>$togroup,
  90. "action"=>$action,
  91. "status"=>1,
  92. "addtime"=>date("Y-m-d H:i:s"),
  93. "updatetime"=>date("Y-m-d H:i:s")
  94. ];
  95. $result= Db::name("role_share")->save($data);
  96. return $result ? app_show(0,"新建成功"):error_show(1006,"新建失败");
  97. }
  98. /**
  99. * 权限分享数据编辑
  100. *
  101. * @param \think\Request $request
  102. * @return \think\Response
  103. */
  104. public function save()
  105. {
  106. $post = $this->post;
  107. $id = isset($post['id'])&&$post['id']!="" ? intval($post['id']):"";
  108. $collect = isset($post['collect'])&&$post['collect']!="" ? trim($post['collect']):"";
  109. $user = isset($post['userid'])&&$post['userid']!="" ? intval($post['userid']):"";
  110. $touser = isset($post['touserid'])&&$post['touserid']!="" ? intval($post['touserid']):"";
  111. $togroup = isset($post['togroupid'])&&$post['togroupid']!="" ? intval($post['togroupid']):"";
  112. $action = isset($post['action'])&&$post['action']!="" ? trim($post['action']):"";
  113. $info = Db::name("role_share")->where("id","=",$id)->find();
  114. if(!$info){
  115. return error_show(1002,"未找到对应数据");
  116. }
  117. if($collect==""){
  118. return error_show(1002,"菜单数据不能为空");
  119. }
  120. if($user==""){
  121. return error_show(1003,"数据源用户不能为空");
  122. }
  123. if($touser=="" && $togroup==""){
  124. return error_show(1004,"共享用户或用户组不能为空");
  125. }
  126. $data=[
  127. "action_collect"=>$collect,
  128. "share_user"=>$user,
  129. "to_user"=>$touser,
  130. "to_group"=>$togroup,
  131. "action"=>$action,
  132. "updatetime"=>date("Y-m-d H:i:s")
  133. ];
  134. $result= Db::name("role_share")->where("id","=",$id)->update($data);
  135. return $result ? app_show(0,"更新成功"):error_show(1006,"更新失败");
  136. }
  137. /**
  138. * 权限数据分享信息
  139. *
  140. * @param int $id
  141. * @return \think\Response
  142. */
  143. public function read()
  144. {
  145. $post = $this->post;
  146. $id = isset($post['id'])&&$post['id']!="" ? intval($post['id']):"";
  147. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  148. if(!$info){
  149. return error_show(1002,"未找到对应数据");
  150. }
  151. $menu =Db::name("admin_menu")->where("id in ({$info['action_collect']})")->column("menu_name");
  152. $info['actionlist']=implode(",",$menu);
  153. $userid = [];
  154. $info['share_user']!=""&&$info['share_user']!=0 ?$userid[]=$info['share_user']:"";
  155. $info['to_user']!=""&&$info['to_user']!=0 ?$userid[]=$info['to_user']:"";
  156. $cond = ['id' => $userid];
  157. $user = GetUserlist($post['token'], $cond);
  158. $share_name = "";
  159. $to_name = "";
  160. if ($user['code'] == 0 && !empty($user['data'])) {
  161. foreach ($user['data'] as $v) {
  162. if($info['share_user']==$v['id']){
  163. $share_name= isset($v['nickname']) ?$v['nickname']:"";
  164. }
  165. if($info['to_user']==$v['id']){
  166. $to_name= isset($v['nickname']) ?$v['nickname']:"";
  167. }
  168. }
  169. }
  170. $info['to_group']==0 ? $info['to_group']='':"";
  171. $togroup= $info['to_group']!="" ?Db::name("role_group")->where("id","=",$info["to_group"])->find()
  172. :['group_name'=>""];
  173. $info['share_user_name']=$share_name;
  174. $info['to_user_name'] = $to_name;
  175. $info['to_group_name'] = $togroup['group_name'];
  176. return app_show(0,"获取成功",$info);
  177. }
  178. /**
  179. * 权限数据状态设置
  180. * @param int $id
  181. * @return \think\Response
  182. */
  183. public function status()
  184. {
  185. $post = $this->post;
  186. $id = isset($post['id']) ? intval($post['id']):"";
  187. if($id==""){
  188. return error_show(1002,"参数id 不能为空");
  189. }
  190. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  191. if(!$info){
  192. return error_show(1002,"未找到对应数据");
  193. }
  194. $status = isset($post['status']) && $post['status']!=""? intval($post['status']):"";
  195. if($status===""){
  196. return error_show(1002,"参数status 不能为空");
  197. }
  198. if(!in_array($status,[0,1])){
  199. return error_show(1002,"参数status 无效");
  200. }
  201. $info['status']=$status;
  202. $info['updatetime']=date("Y-m-d H:i:s");
  203. $msg = $status==1?"启用":"禁用";
  204. $update = Db::name("role_share")->save($info);
  205. return $update? app_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  206. }
  207. /**
  208. * 保存更新的资源
  209. *
  210. * @param \think\Request $request
  211. * @param int $id
  212. * @return \think\Response
  213. */
  214. public function update(Request $request, $id)
  215. {
  216. //
  217. }
  218. /**
  219. * 删除指定资源
  220. *
  221. * @param int $id
  222. * @return \think\Response
  223. */
  224. public function delete()
  225. {
  226. $post = $this->post;
  227. $id = isset($post['id']) ? intval($post['id']):"";
  228. if($id==""){
  229. return error_show(1002,"参数id 不能为空");
  230. }
  231. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  232. if(!$info){
  233. return error_show(1002,"未找到对应数据");
  234. }
  235. $info["status"]=0;
  236. $info["is_del"]=1;
  237. $info["updatetime"]=date("Y-m-d H:i:s");
  238. $update = Db::name("role_share")->save($info);
  239. return $update? app_show(0,"删除成功"):error_show(1004,"删除失败");
  240. }
  241. }