ShareApi.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. use App\Models\Project;
  9. use App\Http\Api\ChannelApi;
  10. class ShareApi
  11. {
  12. /**
  13. * 获取某用户的可见的协作资源
  14. * $res_type 见readme.md#资源类型 -1全部类型资源
  15. * ## 资源类型
  16. * 1 PCS 文档
  17. * 2 Channel 版本
  18. * 3 Article 文章
  19. * 4 Collection 文集
  20. * 5 版本片段
  21. * power 权限 10: 只读 20:编辑 30: 拥有者
  22. */
  23. public static function getResList($user_uid, $res_type = -1)
  24. {
  25. # 找我加入的群
  26. $my_group = GroupMember::where("user_id", $user_uid)->select('group_id')->get();
  27. $userList[] = $user_uid;
  28. foreach ($my_group as $key => $value) {
  29. # code...
  30. $userList[] = $value["group_id"];
  31. }
  32. if ($res_type == -1) {
  33. #所有类型资源
  34. $Fetch = Share::whereIn("cooperator_id", $userList)->select(['res_id', 'res_type', 'power'])->get();
  35. } else {
  36. #指定类型资源
  37. $Fetch = Share::whereIn("cooperator_id", $userList)
  38. ->where('res_type', $res_type)
  39. ->select(['res_id', 'res_type', 'power'])->get();
  40. }
  41. $resOutput = array();
  42. foreach ($Fetch as $key => $value) {
  43. # 查重
  44. if (isset($resOutput[$value["res_id"]])) {
  45. if ($value["power"] > $resOutput[$value["res_id"]]["power"]) {
  46. $resOutput[$value["res_id"]]["power"] = $value["power"];
  47. }
  48. } else {
  49. $resOutput[$value["res_id"]] = array("power" => $value["power"], "type" => $value["res_type"]);
  50. }
  51. }
  52. $resList = array();
  53. foreach ($resOutput as $key => $value) {
  54. # code...
  55. $resList[] = array("res_id" => $key, "res_type" => (int)$value["type"], "power" => (int)$value["power"]);
  56. }
  57. foreach ($resList as $key => $res) {
  58. # 获取资源标题 和所有者
  59. $resList[$key]["res_title"] = "_unknown_";
  60. $resList[$key]["res_owner_id"] = "_unknown_";
  61. $resList[$key]["type"] = "_unknown_";
  62. $resList[$key]["status"] = "0";
  63. $resList[$key]["lang"] = "_unknown_";
  64. switch ($res["res_type"]) {
  65. case 1:
  66. # pcs 文档
  67. $resList[$key]["res_title"] = "title";
  68. break;
  69. case 2:
  70. # channel
  71. $channelInfo = Channel::where('uid', $res["res_id"])->first();
  72. if ($channelInfo) {
  73. $resList[$key]["res_title"] = $channelInfo["name"];
  74. $resList[$key]["res_owner_id"] = $channelInfo["owner_uid"];
  75. $resList[$key]["type"] = $channelInfo["type"];
  76. $resList[$key]["status"] = $channelInfo["status"];
  77. $resList[$key]["lang"] = $channelInfo["lang"];
  78. }
  79. break;
  80. case 3:
  81. # 3 Article 文章
  82. $aInfo = Article::where('uid', $res["res_id"])->first();
  83. if ($aInfo) {
  84. $resList[$key]["res_title"] = $aInfo["title"];
  85. $resList[$key]["res_owner_id"] = $aInfo["owner"];
  86. $resList[$key]["status"] = $aInfo["status"];
  87. $resList[$key]["lang"] = '';
  88. }
  89. break;
  90. case 4:
  91. # 4 Collection 文集
  92. $aInfo = Collection::where('uid', $res["res_id"])->first();
  93. if ($aInfo) {
  94. $resList[$key]["res_title"] = $aInfo["title"];
  95. $resList[$key]["res_owner_id"] = $aInfo["owner"];
  96. $resList[$key]["status"] = $aInfo["status"];
  97. $resList[$key]["lang"] = $aInfo["lang"];
  98. }
  99. break;
  100. case 5:
  101. # code...
  102. break;
  103. case 6:
  104. $aInfo = Project::where('uid', $res["res_id"])->first();
  105. if ($aInfo) {
  106. $resList[$key]["res_title"] = $aInfo["title"];
  107. $resList[$key]["res_owner_id"] = $aInfo["owner_id"];
  108. $resList[$key]["status"] = $aInfo["status"];
  109. $resList[$key]["lang"] = '';
  110. }
  111. break;
  112. default:
  113. # code...
  114. break;
  115. }
  116. }
  117. return $resList;
  118. }
  119. /**
  120. * 获取对某个共享资源的权限
  121. */
  122. public static function getResPower($user_uid, $res_id, $res_type = 0)
  123. {
  124. if (empty($user_uid)) {
  125. #未登录用户 没有共享资源
  126. return 0;
  127. }
  128. //查看是否为资源拥有者
  129. if ($res_type != 0) {
  130. switch ($res_type) {
  131. case 2:
  132. # channel
  133. $channel = ChannelApi::getById($res_id);
  134. if ($channel) {
  135. if ($channel['studio_id'] === $user_uid) {
  136. return 30;
  137. }
  138. }
  139. break;
  140. case 3:
  141. //Article
  142. $owner = Article::where('uid', $res_id)->value('owner');
  143. if ($owner === $user_uid) {
  144. return 30;
  145. }
  146. break;
  147. case 4:
  148. $owner = Collection::where('uid', $res_id)->value('owner');
  149. if ($owner === $user_uid) {
  150. return 30;
  151. }
  152. //文集
  153. break;
  154. case 6: //workflow
  155. $owner = Project::where('uid', $res_id)->value('owner_id');
  156. if ($owner === $user_uid) {
  157. return 30;
  158. }
  159. break;
  160. }
  161. }
  162. # 找我加入的群
  163. $my_group = GroupMember::where("user_id", $user_uid)->select('group_id')->get();
  164. $userList[] = $user_uid;
  165. foreach ($my_group as $key => $value) {
  166. $userList[] = $value["group_id"];
  167. }
  168. $Fetch = Share::whereIn("cooperator_id", $userList)
  169. ->where('res_id', $res_id)
  170. ->select(['power'])->get();
  171. $power = 0;
  172. foreach ($Fetch as $key => $value) {
  173. # code...
  174. if ((int)$value["power"] > $power) {
  175. $power = $value["power"];
  176. }
  177. }
  178. return $power;
  179. }
  180. }