ChannelController.php 29 KB

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