Share.php 11 KB

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