Share.php 9.1 KB

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