Share.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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']) ? intval($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. if($action===""){
  99. return error_show(1005,"共享数据权限不能为空");
  100. }
  101. $data=[
  102. "action_collect"=>$collect,
  103. "share_user"=>$user,
  104. "to_user"=>$touser,
  105. "to_group"=>$togroup,
  106. "action"=>$action,
  107. "status"=>1,
  108. "addtime"=>date("Y-m-d H:i:s"),
  109. "updatetime"=>date("Y-m-d H:i:s")
  110. ];
  111. $result= Db::name("role_share")->save($data);
  112. return $result ? app_show(0,"新建成功"):error_show(1006,"新建失败");
  113. }
  114. /**
  115. * 保存新建的资源
  116. *
  117. * @param \think\Request $request
  118. * @return \think\Response
  119. */
  120. public function save()
  121. {
  122. $post =$this->request->post();
  123. $token = isset($post['token']) ? trim($post['token']) : "";
  124. if($token==""){
  125. return error_show(1001,'token不能为空');
  126. }
  127. $effetc = VerifyTokens($token);
  128. if(!empty($effetc) && $effetc['code']!=0){
  129. return error_show($effetc['code'],$effetc['message']);
  130. }
  131. $id = isset($post['id'])&&$post['id']!="" ? intval($post['id']):"";
  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,
  157. "to_group"=>$togroup,
  158. "action"=>$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. $togroup= $info['to_group']!="" ?Db::name("role_group")->where("id","=",$info["to_group"])->find()
  207. :['group_name'=>""];
  208. $info['share_user_name']=$share_name;
  209. $info['to_user_name'] = $to_name;
  210. $info['to_group_name'] = $togroup['group_name'];
  211. return app_show(0,"获取成功",$info);
  212. }
  213. /**
  214. * 显示编辑资源表单页.
  215. *
  216. * @param int $id
  217. * @return \think\Response
  218. */
  219. public function status()
  220. {
  221. $post =$this->request->post();
  222. $token = isset($post['token']) ? trim($post['token']) : "";
  223. if($token==""){
  224. return error_show(1001,'token不能为空');
  225. }
  226. $effetc = VerifyTokens($token);
  227. if(!empty($effetc) && $effetc['code']!=0){
  228. return error_show($effetc['code'],$effetc['message']);
  229. }
  230. $id = isset($post['id']) ? intval($post['id']):"";
  231. if($id==""){
  232. return error_show(1002,"参数id 不能为空");
  233. }
  234. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  235. if(!$info){
  236. return error_show(1002,"未找到对应数据");
  237. }
  238. $status = isset($post['status']) && $post['status']!=""? intval($post['status']):"";
  239. if($status===""){
  240. return error_show(1002,"参数status 不能为空");
  241. }
  242. if(!in_array($status,[0,1])){
  243. return error_show(1002,"参数status 无效");
  244. }
  245. $info['status']=$status;
  246. $info['updatetime']=date("Y-m-d H:i:s");
  247. $msg = $status==1?"启用":"禁用";
  248. $update = Db::name("role_share")->save($info);
  249. return $update? app_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  250. }
  251. /**
  252. * 保存更新的资源
  253. *
  254. * @param \think\Request $request
  255. * @param int $id
  256. * @return \think\Response
  257. */
  258. public function update(Request $request, $id)
  259. {
  260. //
  261. }
  262. /**
  263. * 删除指定资源
  264. *
  265. * @param int $id
  266. * @return \think\Response
  267. */
  268. public function delete()
  269. {
  270. $post =$this->request->post();
  271. $token = isset($post['token']) ? trim($post['token']) : "";
  272. if($token==""){
  273. return error_show(1001,'token不能为空');
  274. }
  275. $effetc = VerifyTokens($token);
  276. if(!empty($effetc) && $effetc['code']!=0){
  277. return error_show($effetc['code'],$effetc['message']);
  278. }
  279. $id = isset($post['id']) ? intval($post['id']):"";
  280. if($id==""){
  281. return error_show(1002,"参数id 不能为空");
  282. }
  283. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  284. if(!$info){
  285. return error_show(1002,"未找到对应数据");
  286. }
  287. $info["status"]=0;
  288. $info["is_del"]=1;
  289. $info["updatetime"]=date("Y-m-d H:i:s");
  290. $update = Db::name("role_share")->save($info);
  291. return $update? app_show(0,"删除成功"):error_show(1004,"删除失败");
  292. }
  293. }