ShareApi.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Models\GroupMember;
  4. use App\Models\Share;
  5. use App\Models\Article;
  6. use App\Models\Channel;
  7. use App\Models\Collection;
  8. class ShareApi{
  9. /*
  10. 获取某用户的可见的协作资源
  11. $res_type 见readme.md#资源类型 -1全部类型资源
  12. */
  13. public static function getResList($user_uid,$res_type=-1){
  14. # 找我加入的群
  15. $my_group = GroupMember::where("user_id",$user_uid)->select('group_id')->get();
  16. $userList[] = $user_uid;
  17. foreach ($my_group as $key => $value) {
  18. # code...
  19. $userList[]=$value["group_id"];
  20. }
  21. if($res_type==-1){
  22. #所有类型资源
  23. $Fetch =Share::whereIn("cooperator_id",$userList)->select(['res_id','res_type','power'])->get();
  24. }
  25. else{
  26. #指定类型资源
  27. $Fetch =Share::whereIn("cooperator_id",$userList)
  28. ->where('res_type',$res_type)
  29. ->select(['res_id','res_type','power'])->get();
  30. }
  31. $resOutput = array();
  32. foreach ($Fetch as $key => $value) {
  33. # 查重
  34. if(isset($resOutput[$value["res_id"]])){
  35. if($value["power"]>$resOutput[$value["res_id"]]["power"]){
  36. $resOutput[$value["res_id"]]["power"] = $value["power"];
  37. }
  38. }
  39. else{
  40. $resOutput[$value["res_id"]]= array("power"=> $value["power"],"type" => $value["res_type"]);
  41. }
  42. }
  43. $resList=array();
  44. foreach ($resOutput as $key => $value) {
  45. # code...
  46. $resList[]=array("res_id"=>$key,"res_type"=>(int)$value["type"],"power"=>(int)$value["power"]);
  47. }
  48. foreach ($resList as $key => $res) {
  49. # 获取资源标题 和所有者
  50. $resList[$key]["res_title"]="_unknown_";
  51. $resList[$key]["res_owner_id"]="_unknown_";
  52. $resList[$key]["type"]="_unknown_";
  53. $resList[$key]["status"]="0";
  54. $resList[$key]["lang"]="_unknown_";
  55. switch ($res["res_type"]) {
  56. case 1:
  57. # pcs 文档
  58. $resList[$key]["res_title"]="title";
  59. break;
  60. case 2:
  61. # channel
  62. $channelInfo = Channel::where('uid',$res["res_id"])->first();
  63. if($channelInfo){
  64. $resList[$key]["res_title"]=$channelInfo["name"];
  65. $resList[$key]["res_owner_id"]=$channelInfo["owner_uid"];
  66. $resList[$key]["type"]=$channelInfo["type"];
  67. $resList[$key]["status"]=$channelInfo["status"];
  68. $resList[$key]["lang"]=$channelInfo["lang"];
  69. }
  70. break;
  71. case 3:
  72. # 3 Article 文章
  73. $aInfo = Article::where('uid',$res["res_id"])->first();
  74. if($aInfo){
  75. $resList[$key]["res_title"]=$aInfo["title"];
  76. $resList[$key]["res_owner_id"]=$aInfo["owner"];
  77. $resList[$key]["status"]=$aInfo["status"];
  78. $resList[$key]["lang"]='';
  79. }
  80. break;
  81. case 4:
  82. # 4 Collection 文集
  83. $aInfo = Collection::where('uid',$res["res_id"])->first();
  84. if($aInfo){
  85. $resList[$key]["res_title"]=$aInfo["title"];
  86. $resList[$key]["res_owner_id"]=$aInfo["owner"];
  87. $resList[$key]["status"]=$aInfo["status"];
  88. $resList[$key]["lang"]=$aInfo["lang"];
  89. }
  90. break;
  91. case 5:
  92. # code...
  93. break;
  94. default:
  95. # code...
  96. break;
  97. }
  98. }
  99. return $resList;
  100. }
  101. /**
  102. * 获取对某个共享资源的权限
  103. */
  104. public static function getResPower($user_uid,$res_id){
  105. if($userid==='0'){
  106. #未登录用户 没有共享资源
  107. return 0;
  108. }
  109. # 找我加入的群
  110. $my_group = Group::where("user_id",$user_uid)->select('group_id')->get();
  111. $userList[] = $user_uid;
  112. foreach ($my_group as $key => $value) {
  113. $userList[]=$value["group_id"];
  114. }
  115. $Fetch =Share::whereIn("cooperator_id",$userList)
  116. ->where('res_id',$res_id)
  117. ->select(['power'])->get();
  118. $power=0;
  119. foreach ($Fetch as $key => $value) {
  120. # code...
  121. if((int)$value["power"]>$power){
  122. $power = $value["power"];
  123. }
  124. }
  125. return $power;
  126. }
  127. }