ChannelApi.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Models\Channel;
  4. use App\Services\ChannelService;
  5. use App\Services\V3\ChannelAbilityService;
  6. /**
  7. * **v2 专用,v3 代码一律不准引用本类。**
  8. *
  9. * 两部分:
  10. * - 纯能力(取 channel 摘要、系统 channel、自定义书 channel):**冻结的转发壳**,实现全在
  11. * {@see ChannelService},新代码直接注入它。保留这些方法只为 v2 的几十个调用点不必改。
  12. * - 权限判定(`getCanReadByUser` … `canManageByUser`):v2 的旧口径——只认拥有者本人 + 共享,
  13. * 不认团队角色、不排除软删以外的状态。**不转发到 v3**:v3 的口径(团队角色、status 20 公开不列出)
  14. * 在 {@see ChannelAbilityService},把 v2 转过去等于改 v2 权限(铁律 1)。
  15. * 原地留着,随 v4 下线删除。
  16. *
  17. * @deprecated 纯能力用 App\Services\ChannelService;权限用 App\Services\V3\ChannelAbilityService
  18. */
  19. class ChannelApi
  20. {
  21. /** @deprecated 用 ChannelService::summary() */
  22. public static function getById(?string $id)
  23. {
  24. return self::service()->summary($id);
  25. }
  26. /** @deprecated 用 ChannelService::summaries() */
  27. public static function getByIds(array $ids)
  28. {
  29. return self::service()->summaries($ids);
  30. }
  31. /** v2 口径,v3 用 ChannelAbilityService::scopeListable() / abilities() */
  32. public static function getCanReadByUser($userUuid = null)
  33. {
  34. // 获取 user 在某章节 所有有权限的 channel 列表
  35. $channelId = [];
  36. // 我自己的
  37. if ($userUuid) {
  38. $my = Channel::select('uid')->where('owner_uid', $userUuid)->get();
  39. foreach ($my as $key => $value) {
  40. $channelId[$value->uid] = $value->uid;
  41. }
  42. // 获取共享channel
  43. $allSharedChannels = ShareApi::getResList($userUuid, 2);
  44. foreach ($allSharedChannels as $key => $value) {
  45. $channelId[$value['res_id']] = $value['res_id'];
  46. }
  47. }
  48. // 获取全网公开的channel
  49. $my = Channel::select('uid')->where('status', 30)->get();
  50. foreach ($my as $key => $value) {
  51. $channelId[$value->uid] = $value->uid;
  52. }
  53. $output = [];
  54. foreach ($channelId as $key => $value) {
  55. $output[] = $key;
  56. }
  57. return $output;
  58. }
  59. /** v2 口径,v3 用 ChannelAbilityService::writableUids() */
  60. public static function getCanEditByUser($userUuid = null)
  61. {
  62. // 获取 user 在某章节 所有有权限的 channel 列表
  63. $channelId = [];
  64. // 我自己的
  65. if ($userUuid) {
  66. $my = Channel::select('uid')->where('owner_uid', $userUuid)->get();
  67. foreach ($my as $key => $value) {
  68. $channelId[$value->uid] = $value->uid;
  69. }
  70. // 获取共享channel
  71. $allSharedChannels = ShareApi::getResList($userUuid, 2);
  72. foreach ($allSharedChannels as $key => $value) {
  73. if ($value['power'] >= 20) {
  74. $channelId[$value['res_id']] = $value['res_id'];
  75. }
  76. }
  77. }
  78. $output = [];
  79. foreach ($channelId as $key => $value) {
  80. $output[] = $key;
  81. }
  82. return $output;
  83. }
  84. public static function userCanRead($userUid, $channelUid)
  85. {
  86. $channels = ChannelApi::getCanReadByUser($userUid);
  87. return in_array($channelUid, $channels);
  88. }
  89. /** v2 口径,v3 用 ChannelAbilityService::canWrite() */
  90. public static function userCanEdit($userUid, $channelUid)
  91. {
  92. $channels = ChannelApi::getCanEditByUser($userUid);
  93. return in_array($channelUid, $channels);
  94. }
  95. public static function canManageByUser($channelId, $userUuid)
  96. {
  97. $isOwner = Channel::where('owner_uid', $userUuid)
  98. ->where('uid', $channelId)->exists();
  99. return $isOwner;
  100. }
  101. /** @deprecated 用 ChannelService::systemUid() */
  102. public static function getSysChannel($channel_name, $fallback = '')
  103. {
  104. return self::service()->systemUid((string) $channel_name, (string) $fallback);
  105. }
  106. /** @deprecated 用 ChannelService::byNameOrFail() */
  107. public static function getChannelByName($name)
  108. {
  109. return self::service()->byNameOrFail((string) $name);
  110. }
  111. /**
  112. * 获取某个studio 的某个语言的自定义书的channel
  113. * 如果没有,建立
  114. *
  115. * @deprecated 用 ChannelService::userBookUid()
  116. */
  117. public static function userBookGetOrCreate($studioId, $lang, $status)
  118. {
  119. return self::service()->userBookUid((string) $studioId, (string) $lang, (int) $status);
  120. }
  121. private static function service(): ChannelService
  122. {
  123. return app(ChannelService::class);
  124. }
  125. }