ChannelController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\Channel;
  8. use App\Models\Sentence;
  9. use App\Models\DhammaTerm;
  10. use App\Models\WbwBlock;
  11. use App\Models\PaliSentence;
  12. use App\Models\CustomBook;
  13. use App\Http\Resources\ChannelResource;
  14. use App\Services\AuthService;
  15. use App\Http\Api\StudioApi;
  16. use App\Http\Api\ShareApi;
  17. use App\Http\Api\PaliTextApi;
  18. use App\Http\Api\ChannelApi;
  19. class ChannelController extends Controller
  20. {
  21. /**
  22. * Display a listing of the resource.
  23. *
  24. * @return \Illuminate\Http\Response
  25. */
  26. public function index(Request $request)
  27. {
  28. //
  29. $result = false;
  30. $indexCol = [
  31. 'channels.uid',
  32. 'name',
  33. 'channels.summary',
  34. 'type',
  35. 'owner_uid',
  36. 'channels.lang',
  37. 'status',
  38. 'is_system',
  39. 'channels.updated_at',
  40. 'channels.created_at'
  41. ];
  42. if ($request->has("book")) {
  43. $indexCol[] = 'progress_chapters.progress';
  44. }
  45. switch ($request->input('view')) {
  46. case 'public':
  47. $table = Channel::select($indexCol)
  48. ->where('status', 30);
  49. break;
  50. case 'studio':
  51. # 获取studio内所有channel
  52. $user = AuthService::current($request);
  53. if (!$user) {
  54. return $this->error(__('auth.failed'));
  55. }
  56. //判断当前用户是否有指定的studio的权限
  57. $studioId = StudioApi::getIdByName($request->input('name'));
  58. if (!StudioApi::userCanList($user['user_uid'], $studioId)) {
  59. return $this->error(__('auth.failed'), 403, 403);
  60. }
  61. $table = Channel::select($indexCol);
  62. if ($request->input('view2', 'my') === 'my') {
  63. $table = $table->where('owner_uid', $studioId);
  64. } else {
  65. //协作
  66. $resList = ShareApi::getResList($studioId, 2);
  67. $resId = [];
  68. foreach ($resList as $res) {
  69. $resId[] = $res['res_id'];
  70. }
  71. $table = $table->whereIn('channels.uid', $resId);
  72. if ($request->input('collaborator', 'all') !== 'all') {
  73. $table = $table->where('owner_uid', $request->input('collaborator'));
  74. } else {
  75. $table = $table->where('owner_uid', '<>', $studioId);
  76. }
  77. }
  78. break;
  79. case 'studio-all':
  80. /**
  81. * studio 的和协作的
  82. */
  83. #获取user所有有权限的channel列表
  84. $user = AuthService::current($request);
  85. if (!$user) {
  86. return $this->error(__('auth.failed'));
  87. }
  88. //判断当前用户是否有指定的studio的权限
  89. if ($user['user_uid'] !== StudioApi::getIdByName($request->input('name'))) {
  90. return $this->error(__('auth.failed'));
  91. }
  92. $channelById = [];
  93. $channelId = [];
  94. //获取共享channel
  95. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  96. foreach ($allSharedChannels as $key => $value) {
  97. # code...
  98. $channelId[] = $value['res_id'];
  99. $channelById[$value['res_id']] = $value;
  100. }
  101. $table = Channel::select($indexCol)
  102. ->whereIn('uid', $channelId)
  103. ->orWhere('owner_uid', $user['user_uid']);
  104. break;
  105. case 'user-edit':
  106. /**
  107. * 某用户有编辑权限的
  108. */
  109. #获取user所有有权限的channel列表
  110. $user = AuthService::current($request);
  111. if (!$user) {
  112. return $this->error(__('auth.failed'));
  113. }
  114. $channelById = [];
  115. $channelId = [];
  116. //获取共享channel
  117. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  118. foreach ($allSharedChannels as $key => $value) {
  119. # code...
  120. if ($value['power'] >= 20) {
  121. $channelId[] = $value['res_id'];
  122. $channelById[$value['res_id']] = $value;
  123. }
  124. }
  125. $table = Channel::select($indexCol)
  126. ->whereIn('uid', $channelId)
  127. ->orWhere('owner_uid', $user['user_uid']);
  128. break;
  129. case 'user-in-chapter':
  130. #获取user 在某章节 所有有权限的channel列表
  131. $user = AuthService::current($request);
  132. if (!$user) {
  133. return $this->error(__('auth.failed'));
  134. }
  135. $channelById = [];
  136. $channelId = [];
  137. //获取共享channel
  138. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  139. foreach ($allSharedChannels as $key => $value) {
  140. # code...
  141. $channelId[] = $value['res_id'];
  142. $channelById[$value['res_id']] = $value;
  143. }
  144. //获取全网公开channel
  145. $chapter = PaliTextApi::getChapterStartEnd($request->input('book'), $request->input('para'));
  146. $publicChannelsWithContent = Sentence::where('book_id', $request->input('book'))
  147. ->whereBetween('paragraph', $chapter)
  148. ->where('strlen', '>', 0)
  149. ->where('status', 30)
  150. ->groupBy('channel_uid')
  151. ->select('channel_uid')
  152. ->get();
  153. foreach ($publicChannelsWithContent as $key => $value) {
  154. # code...
  155. $value['res_id'] = $value->channel_uid;
  156. $value['power'] = 10;
  157. $value['type'] = 2;
  158. if (!isset($channelById[$value['res_id']])) {
  159. $channelId[] = $value['res_id'];
  160. $channelById[$value['res_id']] = $value;
  161. }
  162. }
  163. $table = Channel::select($indexCol)
  164. ->whereIn('uid', $channelId)
  165. ->orWhere('owner_uid', $user['user_uid']);
  166. break;
  167. case 'system':
  168. $table = Channel::select($indexCol)
  169. ->where('owner_uid', config("mint.admin.root_uuid"));
  170. break;
  171. case 'paragraphs':
  172. $channels = Sentence::where('ver', '>', 1)
  173. ->where('book_id', $request->input('book_id'))
  174. ->whereIn('paragraph', explode(',', $request->input('para')))
  175. ->groupBy('channel_uid')->select('channel_uid')->get();
  176. if (count($channels) > 0) {
  177. $table = Channel::select($indexCol)
  178. ->where(
  179. 'uid',
  180. array_map(fn($item) => $item['channel_uid'], $channels->toArray())
  181. );
  182. } else {
  183. $table = Channel::select($indexCol)->whereIsNull('uid');
  184. }
  185. break;
  186. case 'id':
  187. $table = Channel::select($indexCol)
  188. ->whereIn('uid', explode(',', $request->input("id")));
  189. }
  190. if ($request->has("book")) {
  191. if ($request->input("view") === "public") {
  192. $table = $table->leftJoin('progress_chapters', 'channels.uid', '=', 'progress_chapters.channel_id',)
  193. ->where('progress_chapters.book', $request->input("book"))
  194. ->where('progress_chapters.para', $request->input("paragraph"));
  195. } else {
  196. $table = $table->leftJoin('progress_chapters', function ($join) use ($request) {
  197. $join->on('channels.uid', '=', 'progress_chapters.channel_id')
  198. ->where('progress_chapters.book', $request->input("book"))
  199. ->where('progress_chapters.para', $request->input("paragraph")); // 条件写在这里!
  200. });
  201. }
  202. }
  203. //处理搜索
  204. if (!empty($request->input("search"))) {
  205. $table = $table->where('name', 'like', "%" . $request->input("search") . "%");
  206. }
  207. if ($request->has("type")) {
  208. $table = $table->where('type', $request->input("type"));
  209. }
  210. if ($request->has("updated_at")) {
  211. $table = $table->where('updated_at', '>', $request->input("updated_at"));
  212. }
  213. if ($request->has("created_at")) {
  214. $table = $table->where('created_at', '>', $request->input("created_at"));
  215. }
  216. //获取记录总条数
  217. $count = $table->count();
  218. //处理排序
  219. $table = $table->orderBy(
  220. $request->input("order", 'created_at'),
  221. $request->input("dir", 'desc')
  222. );
  223. //处理分页
  224. $table = $table->skip($request->input("offset", 0))
  225. ->take($request->input("limit", 200));
  226. Log::debug('channel sql ' . $table->toSql());
  227. //获取数据
  228. $result = $table->get();
  229. //TODO 将下面代码转移到resource
  230. if ($result) {
  231. if ($request->has('progress')) {
  232. //获取进度
  233. //获取单句长度
  234. $sentLen = PaliSentence::where('book', $request->input('book'))
  235. ->whereBetween('paragraph', $chapter)
  236. ->orderBy('word_begin')
  237. ->select(['book', 'paragraph', 'word_begin', 'word_end', 'length'])
  238. ->get();
  239. }
  240. foreach ($result as $key => $value) {
  241. if ($request->has('progress')) {
  242. //获取进度
  243. $finalTable = Sentence::where('book_id', $request->input('book'))
  244. ->whereBetween('paragraph', $chapter)
  245. ->where('channel_uid', $value->uid)
  246. ->where('strlen', '>', 0)
  247. ->select(['strlen', 'book_id', 'paragraph', 'word_start', 'word_end']);
  248. if ($finalTable->count() > 0) {
  249. $finished = $finalTable->get();
  250. $final = [];
  251. foreach ($sentLen as $sent) {
  252. # code...
  253. $first = Arr::first($finished, function ($value, $key) use ($sent) {
  254. return ($value->book_id == $sent->book &&
  255. $value->paragraph == $sent->paragraph &&
  256. $value->word_start == $sent->word_begin &&
  257. $value->word_end == $sent->word_end);
  258. });
  259. $final[] = [$sent->length, $first ? true : false];
  260. }
  261. $value['final'] = $final;
  262. }
  263. }
  264. //角色
  265. if (isset($user['user_uid'])) {
  266. if ($value->owner_uid === $user['user_uid']) {
  267. $value['role'] = 'owner';
  268. } else {
  269. if (isset($channelById) && isset($channelById[$value->uid])) {
  270. switch ($channelById[$value->uid]['power']) {
  271. case 10:
  272. # code...
  273. $value['role'] = 'member';
  274. break;
  275. case 20:
  276. $value['role'] = 'editor';
  277. break;
  278. case 30:
  279. $value['role'] = 'owner';
  280. break;
  281. default:
  282. # code...
  283. $value['role'] = $channelById[$value->uid]['power'];
  284. break;
  285. }
  286. }
  287. }
  288. }
  289. # 获取studio信息
  290. $value->studio = StudioApi::getById($value->owner_uid);
  291. }
  292. return $this->ok(["rows" => $result, "count" => $count]);
  293. } else {
  294. return $this->ok(["rows" => [], "count" => 0]);
  295. }
  296. }
  297. /**
  298. * 获取我的,和协作channel数量
  299. *
  300. * @return \Illuminate\Http\Response
  301. */
  302. public function showMyNumber(Request $request)
  303. {
  304. $user = AuthService::current($request);
  305. if (!$user) {
  306. return $this->error(__('auth.failed'));
  307. }
  308. //判断当前用户是否有指定的studio的权限
  309. $studioId = StudioApi::getIdByName($request->input('studio'));
  310. if ($user['user_uid'] !== $studioId) {
  311. return $this->error(__('auth.failed'));
  312. }
  313. //我的
  314. $my = Channel::where('owner_uid', $studioId)->count();
  315. //协作
  316. $resList = ShareApi::getResList($studioId, 2);
  317. $resId = [];
  318. foreach ($resList as $res) {
  319. $resId[] = $res['res_id'];
  320. }
  321. $collaboration = Channel::whereIn('uid', $resId)->where('owner_uid', '<>', $studioId)->count();
  322. return $this->ok(['my' => $my, 'collaboration' => $collaboration]);
  323. }
  324. /**
  325. * 获取章节的进度
  326. *
  327. * @param \Illuminate\Http\Request $request
  328. * @return \Illuminate\Http\Response
  329. */
  330. public function progress(Request $request)
  331. {
  332. $indexCol = ['uid', 'name', 'summary', 'type', 'owner_uid', 'lang', 'status', 'updated_at', 'created_at'];
  333. $sent = $request->input('sentence');
  334. $query = [];
  335. $queryWithChannel = [];
  336. $sentContainer = [];
  337. $sentLenContainer = [];
  338. $paliChannel = ChannelApi::getSysChannel('_System_Pali_VRI_');
  339. $customBookChannel = array();
  340. foreach ($sent as $value) {
  341. $ids = explode('-', $value);
  342. $idWithChannel = $ids;
  343. if (count($ids) === 4) {
  344. if ($ids[0] < 1000) {
  345. $idWithChannel[] = $paliChannel;
  346. } else {
  347. if (!isset($customBookChannel[$ids[0]])) {
  348. $cbChannel = CustomBook::where('book_id', $ids[0])->value('channel_id');
  349. if ($cbChannel) {
  350. $customBookChannel[$ids[0]] = $cbChannel;
  351. } else {
  352. $customBookChannel[$ids[0]] = $paliChannel;
  353. }
  354. }
  355. $idWithChannel[] = $customBookChannel[$ids[0]];
  356. }
  357. $sentContainer[$value] = false;
  358. $query[] = $ids;
  359. $queryWithChannel[] = $idWithChannel;
  360. }
  361. }
  362. //获取单句长度
  363. if (count($query) > 0) {
  364. $table = Sentence::whereIns([
  365. 'book_id',
  366. 'paragraph',
  367. 'word_start',
  368. 'word_end',
  369. 'channel_uid'
  370. ], $queryWithChannel)
  371. ->select(['book_id', 'paragraph', 'word_start', 'word_end', 'strlen']);
  372. $sentLen = $table->get();
  373. foreach ($sentLen as $value) {
  374. $strlen = $value->strlen;
  375. if (empty($strlen)) {
  376. $strlen = 0;
  377. }
  378. $sentId = "{$value->book_id}-{$value->paragraph}-{$value->word_start}-{$value->word_end}";
  379. $sentLenContainer[$sentId] = $strlen;
  380. }
  381. }
  382. $channelById = [];
  383. $channelId = [];
  384. //获取全网公开的有译文的channel
  385. if ($request->input('owner') === 'all' || $request->input('owner') === 'public') {
  386. if (count($query) > 0) {
  387. $fields = ['book_id', 'paragraph', 'word_start', 'word_end'];
  388. $publicChannelsWithContent = Sentence::whereIns($fields, $query)
  389. ->where('strlen', '>', 0)
  390. ->where('status', 30)
  391. ->groupBy('channel_uid')
  392. ->select('channel_uid')
  393. ->get();
  394. foreach ($publicChannelsWithContent as $key => $value) {
  395. # code...
  396. $value['res_id'] = $value->channel_uid;
  397. $value['power'] = 10;
  398. $value['type'] = 2;
  399. if (!isset($channelById[$value['res_id']])) {
  400. $channelId[] = $value['res_id'];
  401. $channelById[$value['res_id']] = $value;
  402. }
  403. }
  404. }
  405. }
  406. #获取 user 在某章节 所有有权限的 channel 列表
  407. $user = AuthService::current($request);
  408. if ($user !== false) {
  409. //我自己的
  410. if ($request->input('owner') === 'all' || $request->input('owner') === 'my') {
  411. $my = Channel::select($indexCol)->where('owner_uid', $user['user_uid'])->get();
  412. foreach ($my as $key => $value) {
  413. $channelId[] = $value->uid;
  414. $channelById[$value->uid] = [
  415. 'res_id' => $value->uid,
  416. 'power' => 30,
  417. 'type' => 2,
  418. ];
  419. }
  420. }
  421. //获取共享channel
  422. if ($request->input('owner') === 'all' || $request->input('owner') === 'collaborator') {
  423. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  424. foreach ($allSharedChannels as $key => $value) {
  425. # code...
  426. if (!in_array($value['res_id'], $channelId)) {
  427. $channelId[] = $value['res_id'];
  428. $channelById[$value['res_id']] = $value;
  429. }
  430. }
  431. }
  432. }
  433. //所有有这些句子译文的channel
  434. if (count($query) > 0) {
  435. $allChannels = Sentence::whereIns(['book_id', 'paragraph', 'word_start', 'word_end'], $query)
  436. ->where('strlen', '>', 0)
  437. ->groupBy('channel_uid')
  438. ->select('channel_uid')
  439. ->get();
  440. }
  441. //所有需要查询的channel
  442. $table = Channel::select(['uid', 'name', 'summary', 'type', 'owner_uid', 'lang', 'status', 'updated_at', 'created_at'])
  443. ->whereIn('uid', $channelId);
  444. if ($user !== false) {
  445. $table->orWhere('owner_uid', $user['user_uid']);
  446. }
  447. $result = $table->get();
  448. foreach ($result as $key => $value) {
  449. //角色
  450. if ($user !== false && $value->owner_uid === $user['user_uid']) {
  451. $value['role'] = 'owner';
  452. } else {
  453. if (isset($channelById[$value->uid])) {
  454. switch ($channelById[$value->uid]['power']) {
  455. case 10:
  456. # code...
  457. $value['role'] = 'member';
  458. break;
  459. case 20:
  460. $value['role'] = 'editor';
  461. break;
  462. case 30:
  463. $value['role'] = 'owner';
  464. break;
  465. default:
  466. # code...
  467. $value['role'] = $channelById[$value->uid]['power'];
  468. break;
  469. }
  470. }
  471. }
  472. # 获取studio信息
  473. $result[$key]["studio"] = \App\Http\Api\StudioApi::getById($value->owner_uid);
  474. //获取进度
  475. if (count($query) > 0) {
  476. $currChannelId = $value->uid;
  477. $hasContent = Arr::first($allChannels, function ($value, $key) use ($currChannelId) {
  478. return ($value->channel_uid === $currChannelId);
  479. });
  480. if ($hasContent && count($query) > 0) {
  481. $finalTable = Sentence::whereIns(['book_id', 'paragraph', 'word_start', 'word_end'], $query)
  482. ->where('channel_uid', $currChannelId)
  483. ->where('strlen', '>', 0)
  484. ->select(['strlen', 'book_id', 'paragraph', 'word_start', 'word_end', 'created_at', 'updated_at']);
  485. $created_at = time();
  486. $edit_at = 0;
  487. if ($finalTable->count() > 0) {
  488. $finished = $finalTable->get();
  489. $currChannel = [];
  490. foreach ($finished as $rowFinish) {
  491. $createTime = strtotime($rowFinish->created_at);
  492. $updateTime = strtotime($rowFinish->updated_at);
  493. if ($createTime < $created_at) {
  494. $created_at = $createTime;
  495. }
  496. if ($updateTime > $edit_at) {
  497. $edit_at = $updateTime;
  498. }
  499. $currChannel["{$rowFinish->book_id}-{$rowFinish->paragraph}-{$rowFinish->word_start}-{$rowFinish->word_end}"] = 1;
  500. }
  501. $final = [];
  502. foreach ($sentContainer as $sentId => $rowSent) {
  503. # code...
  504. if (isset($currChannel[$sentId])) {
  505. $final[] = [$sentLenContainer[$sentId], true];
  506. } else {
  507. $final[] = [$sentLenContainer[$sentId], false];
  508. }
  509. }
  510. $result[$key]['final'] = $final;
  511. $result[$key]['content_created_at'] = date('Y-m-d H:i:s', $created_at);
  512. $result[$key]['content_updated_at'] = date('Y-m-d H:i:s', $edit_at);
  513. }
  514. }
  515. }
  516. }
  517. return $this->ok(["rows" => $result, count($result)]);
  518. }
  519. /**
  520. * Store a newly created resource in storage.
  521. *
  522. * @param \Illuminate\Http\Request $request
  523. * @return \Illuminate\Http\Response
  524. */
  525. public function store(Request $request)
  526. {
  527. //
  528. $user = AuthService::current($request);
  529. if (!$user) {
  530. return $this->error(__('auth.failed'), 401, 401);
  531. }
  532. //判断当前用户是否有指定的studio的权限
  533. $studioId = StudioApi::getIdByName($request->input('studio'));
  534. if (!StudioApi::userCanManage($user['user_uid'], $studioId)) {
  535. return $this->error(__('auth.failed'), 403, 403);
  536. }
  537. $studio = StudioApi::getById($studioId);
  538. //查询是否重复
  539. if (Channel::where('name', $request->input('name'))
  540. ->where('owner_uid', $studioId)
  541. ->exists()
  542. ) {
  543. return $this->error(__('validation.exists', ['name']), 200, 200);
  544. }
  545. $channel = new Channel;
  546. $channel->id = app('snowflake')->id();
  547. $channel->name = $request->input('name');
  548. $channel->owner_uid = $studioId;
  549. $channel->type = $request->input('type');
  550. $channel->lang = $request->input('lang');
  551. $channel->editor_id = $user['user_id'];
  552. if (isset($studio['roles'])) {
  553. if (in_array('basic', $studio['roles'])) {
  554. $channel->status = 5;
  555. }
  556. }
  557. $channel->create_time = time() * 1000;
  558. $channel->modify_time = time() * 1000;
  559. $channel->save();
  560. return $this->ok($channel);
  561. }
  562. /**
  563. * Display the specified resource.
  564. *
  565. * @param int $id
  566. * @return \Illuminate\Http\Response
  567. */
  568. public function show($id)
  569. {
  570. //
  571. $channel = Channel::find($id);
  572. if (!$channel) {
  573. return $this->error('no res');
  574. }
  575. $studio = StudioApi::getById($channel->owner_uid);
  576. $channel->studio = $studio;
  577. $channel->owner_info = ['nickname' => $studio['nickName'], 'username' => $studio['realName']];
  578. return $this->ok($channel);
  579. }
  580. /**
  581. * Display the specified resource.
  582. *
  583. * @param string $name
  584. * @return \Illuminate\Http\Response
  585. */
  586. public function showByName(string $name)
  587. {
  588. //
  589. $indexCol = ['uid', 'name', 'summary', 'type', 'owner_uid', 'lang', 'is_system', 'status', 'updated_at', 'created_at'];
  590. $channel = Channel::where("name", $name)->select($indexCol)->first();
  591. if ($channel) {
  592. return $this->ok(new ChannelResource($channel));
  593. } else {
  594. return $this->error('no channel');
  595. }
  596. }
  597. /**
  598. * Update the specified resource in storage.
  599. *
  600. * @param \Illuminate\Http\Request $request
  601. * @param \App\Models\Channel $channel
  602. * @return \Illuminate\Http\Response
  603. */
  604. public function update(Request $request, Channel $channel)
  605. {
  606. //鉴权
  607. $user = AuthService::current($request);
  608. if (!$user) {
  609. return $this->error(__('auth.failed'), 401, 401);
  610. }
  611. if ($channel->is_system) {
  612. return $this->error('system channel', 403, 403);
  613. }
  614. if ($channel->owner_uid !== $user["user_uid"]) {
  615. //判断是否为协作
  616. $power = ShareApi::getResPower($user["user_uid"], $request->input('id'));
  617. if ($power < 30) {
  618. return $this->error(__('auth.failed'), 403, 403);
  619. }
  620. }
  621. $channel->name = $request->input('name');
  622. $channel->type = $request->input('type');
  623. $channel->summary = $request->input('summary');
  624. $channel->lang = $request->input('lang');
  625. $channel->status = $request->input('status');
  626. if ($request->has('source_type')) {
  627. $channel->source_type = $request->input('source_type');
  628. }
  629. if ($request->has('source_id')) {
  630. $channel->source_id = $request->input('source_id');
  631. }
  632. $channel->save();
  633. return $this->ok($channel);
  634. }
  635. /**
  636. * patch the specified resource in storage.
  637. *
  638. * @param \Illuminate\Http\Request $request
  639. * @param \App\Models\Channel $channel
  640. * @return \Illuminate\Http\Response
  641. */
  642. public function patch(Request $request, Channel $channel)
  643. {
  644. //鉴权
  645. $user = AuthService::current($request);
  646. if (!$user) {
  647. return $this->error(__('auth.failed'), [], 401);
  648. }
  649. if ($channel->is_system) {
  650. return $this->error('system channel', 403, 403);
  651. }
  652. if ($channel->owner_uid !== $user["user_uid"]) {
  653. //判断是否为协作
  654. $power = ShareApi::getResPower($user["user_uid"], $request->input('id'));
  655. if ($power < 30) {
  656. return $this->error(__('auth.failed'), [], 403);
  657. }
  658. }
  659. if ($request->has('name')) {
  660. $channel->name = $request->input('name');
  661. }
  662. if ($request->has('type')) {
  663. $channel->type = $request->input('type');
  664. }
  665. if ($request->has('summary')) {
  666. $channel->summary = $request->input('summary');
  667. }
  668. if ($request->has('lang')) {
  669. $channel->lang = $request->input('lang');
  670. }
  671. if ($request->has('status')) {
  672. $channel->status = $request->input('status');
  673. }
  674. if ($request->has('config')) {
  675. $channel->status = $request->input('config');
  676. }
  677. $channel->save();
  678. return $this->ok($channel);
  679. }
  680. /**
  681. * Remove the specified resource from storage.
  682. * @param \Illuminate\Http\Request $request
  683. * @param \App\Models\Channel $channel
  684. * @return \Illuminate\Http\Response
  685. */
  686. public function destroy(Request $request, Channel $channel)
  687. {
  688. //
  689. $user = AuthService::current($request);
  690. if (!$user) {
  691. return $this->error(__('auth.failed'));
  692. }
  693. //判断当前用户是否有指定的studio的权限
  694. if ($user['user_uid'] !== $channel->owner_uid) {
  695. return $this->error(__('auth.failed'));
  696. }
  697. //查询其他资源
  698. if (Sentence::where("channel_uid", $channel->uid)->exists()) {
  699. return $this->error("译文有数据无法删除");
  700. }
  701. if (DhammaTerm::where("channal", $channel->uid)->exists()) {
  702. return $this->error("术语有数据无法删除");
  703. }
  704. if (WbwBlock::where("channel_uid", $channel->uid)->exists()) {
  705. return $this->error("逐词解析有数据无法删除");
  706. }
  707. $delete = 0;
  708. DB::transaction(function () use ($channel, $delete) {
  709. //TODO 删除相关资源
  710. $delete = $channel->delete();
  711. });
  712. return $this->ok($delete);
  713. }
  714. }