Share.php 11 KB

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