ChannelController.php 29 KB

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