ShareApi.php 4.9 KB

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