ChannelController.php 29 KB

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