Share.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\Request;
  5. use think\facade\Db;
  6. use app\admin\model\ActionLog;
  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'])&&$post['action']!=='' ? trim($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. $data=[
  100. "action_collect"=>$collect,
  101. "share_user"=>$user,
  102. "to_user"=>$touser,
  103. "to_group"=>$togroup,
  104. "action"=>$action,
  105. "status"=>1,
  106. "addtime"=>date("Y-m-d H:i:s"),
  107. "updatetime"=>date("Y-m-d H:i:s")
  108. ];
  109. $result= Db::name("role_share")->save($data);
  110. $st = ["order_code"=>$collect,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  111. ActionLog::logAdd($token,$st,"role_share",1,$st);
  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. if($id===""){
  133. return error_show(1002,"参数id不能为空");
  134. }
  135. $collect = isset($post['collect'])&&$post['collect']!="" ? trim($post['collect']):"";
  136. $user = isset($post['userid'])&&$post['userid']!="" ? intval($post['userid']):"";
  137. $touser = isset($post['touserid'])&&$post['touserid']!="" ? intval($post['touserid']):"";
  138. $togroup = isset($post['togroupid'])&&$post['togroupid']!="" ? intval($post['togroupid']):"";
  139. $action = isset($post['action'])&&$post['action']!="" ? trim($post['action']):"";
  140. $info = Db::name("role_share")->where("id","=",$id)->find();
  141. if(!$info){
  142. return error_show(1002,"未找到对应数据");
  143. }
  144. if($collect==""){
  145. return error_show(1002,"菜单数据不能为空");
  146. }
  147. if($user==""){
  148. return error_show(1003,"数据源用户不能为空");
  149. }
  150. if($touser=="" && $togroup==""){
  151. return error_show(1004,"共享用户或用户组不能为空");
  152. }
  153. // if($action===""){
  154. // return error_show(1005,"共享数据权限不能为空");
  155. // }
  156. $data=[
  157. "action_collect"=>$collect,
  158. "share_user"=>$user,
  159. "to_user"=>$touser,
  160. "to_group"=>$togroup,
  161. "action"=>$action,
  162. "updatetime"=>date("Y-m-d H:i:s")
  163. ];
  164. $result= Db::name("role_share")->where("id","=",$id)->update($data);
  165. $st = ["order_code"=>$collect,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  166. ActionLog::logAdd($token,$st,"role_share",0,$st);
  167. return $result ? app_show(0,"更新成功"):error_show(1006,"更新失败");
  168. }
  169. /**
  170. * 显示指定的资源
  171. *
  172. * @param int $id
  173. * @return \think\Response
  174. */
  175. public function read()
  176. {
  177. $post =$this->request->post();
  178. $token = isset($post['token']) ? trim($post['token']) : "";
  179. if($token==""){
  180. return error_show(1001,'token不能为空');
  181. }
  182. $effetc = VerifyTokens($token);
  183. if(!empty($effetc) && $effetc['code']!=0){
  184. return error_show($effetc['code'],$effetc['message']);
  185. }
  186. $id = isset($post['id'])&&$post['id']!="" ? intval($post['id']):"";
  187. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  188. if(!$info){
  189. return error_show(1002,"未找到对应数据");
  190. }
  191. $menu =Db::name("admin_menu")->where("id in ({$info['action_collect']})")->column("menu_name");
  192. $info['actionlist']=implode(",",$menu);
  193. $userid = [];
  194. $info['share_user']!=""&&$info['share_user']!=0 ?$userid[]=$info['share_user']:"";
  195. $info['to_user']!=""&&$info['to_user']!=0 ?$userid[]=$info['to_user']:"";
  196. $cond = ['id' => $userid];
  197. $user = GetUserlist($token, $cond);
  198. $share_name = "";
  199. $to_name = "";
  200. if ($user['code'] == 0 && !empty($user['data'])) {
  201. foreach ($user['data'] as $v) {
  202. if($info['share_user']==$v['id']){
  203. $share_name= isset($v['nickname']) ?$v['nickname']:"";
  204. }
  205. if($info['to_user']==$v['id']){
  206. $to_name= isset($v['nickname']) ?$v['nickname']:"";
  207. }
  208. }
  209. }
  210. $info['to_group']==0 ? $info['to_group']='':"";
  211. $togroup= $info['to_group']!="" ?Db::name("role_group")->where("id","=",$info["to_group"])->find()
  212. :['group_name'=>""];
  213. $info['share_user_name']=$share_name;
  214. $info['to_user_name'] = $to_name;
  215. $info['to_group_name'] = $togroup['group_name'];
  216. return app_show(0,"获取成功",$info);
  217. }
  218. /**
  219. * 显示编辑资源表单页.
  220. *
  221. * @param int $id
  222. * @return \think\Response
  223. */
  224. public function status()
  225. {
  226. $post =$this->request->post();
  227. $token = isset($post['token']) ? trim($post['token']) : "";
  228. if($token==""){
  229. return error_show(1001,'token不能为空');
  230. }
  231. $effetc = VerifyTokens($token);
  232. if(!empty($effetc) && $effetc['code']!=0){
  233. return error_show($effetc['code'],$effetc['message']);
  234. }
  235. $id = isset($post['id']) ? intval($post['id']):"";
  236. if($id==""){
  237. return error_show(1002,"参数id 不能为空");
  238. }
  239. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  240. if(!$info){
  241. return error_show(1002,"未找到对应数据");
  242. }
  243. $status = isset($post['status']) && $post['status']!==""? intval($post['status']):"";
  244. if($status===""){
  245. return error_show(1002,"参数status 不能为空");
  246. }
  247. if(!in_array($status,[0,1])){
  248. return error_show(1002,"参数status 无效");
  249. }
  250. $item=$info['status'];
  251. $info['status']=$status;
  252. $info['updatetime']=date("Y-m-d H:i:s");
  253. $msg = $status==1?"启用":"禁用";
  254. $update = Db::name("role_share")->save($info);
  255. $st = ["order_code"=>$id,"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  256. ActionLog::logAdd($token,$st,"role_share",$info['status'],$st);
  257. return $update? app_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
  258. }
  259. /**
  260. * 保存更新的资源
  261. *
  262. * @param \think\Request $request
  263. * @param int $id
  264. * @return \think\Response
  265. */
  266. public function update(Request $request, $id)
  267. {
  268. //
  269. }
  270. /**
  271. * 删除指定资源
  272. *
  273. * @param int $id
  274. * @return \think\Response
  275. */
  276. public function delete()
  277. {
  278. $post =$this->request->post();
  279. $token = isset($post['token']) ? trim($post['token']) : "";
  280. if($token==""){
  281. return error_show(1001,'token不能为空');
  282. }
  283. $effetc = VerifyTokens($token);
  284. if(!empty($effetc) && $effetc['code']!=0){
  285. return error_show($effetc['code'],$effetc['message']);
  286. }
  287. $id = isset($post['id']) ? intval($post['id']):"";
  288. if($id==""){
  289. return error_show(1002,"参数id 不能为空");
  290. }
  291. $info = Db::name("role_share")->where([["id","=",$id],["is_del","=",0]])->find();
  292. if(!$info){
  293. return error_show(1002,"未找到对应数据");
  294. }
  295. $teme = $info['status'];
  296. $info["status"]=0;
  297. $info["is_del"]=1;
  298. $info["updatetime"]=date("Y-m-d H:i:s");
  299. $update = Db::name("role_share")->save($info);
  300. $st = ["order_code"=>$id,"status"=> $teme,"action_remark"=>'',"action_type"=>"delete"];
  301. ActionLog::logAdd($token,$st,"role_share",$info['status'],$st);
  302. return $update? app_show(0,"删除成功"):error_show(1004,"删除失败");
  303. }
  304. }