ChannelController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\Channel;
  7. use App\Models\Sentence;
  8. use App\Models\DhammaTerm;
  9. use App\Models\WbwBlock;
  10. use App\Models\PaliSentence;
  11. use App\Models\CustomBook;
  12. use App\Http\Resources\ChannelResource;
  13. use App\Services\AuthService;
  14. use App\Http\Api\StudioApi;
  15. use App\Http\Api\ShareApi;
  16. use App\Http\Api\PaliTextApi;
  17. use App\Http\Api\ChannelApi;
  18. class ChannelController extends Controller
  19. {
  20. /**
  21. * Display a listing of the resource.
  22. *
  23. * @return \Illuminate\Http\Response
  24. */
  25. public function index(Request $request)
  26. {
  27. //
  28. $result = false;
  29. $indexCol = [
  30. 'channels.uid',
  31. 'name',
  32. 'channels.summary',
  33. 'type',
  34. 'owner_uid',
  35. 'channels.lang',
  36. 'status',
  37. 'is_system',
  38. 'channels.updated_at',
  39. 'channels.created_at'
  40. ];
  41. if ($request->has("book")) {
  42. $indexCol[] = 'progress_chapters.progress';
  43. }
  44. switch ($request->input('view')) {
  45. case 'public':
  46. $table = Channel::select($indexCol)
  47. ->where('status', 30);
  48. break;
  49. case 'studio':
  50. # 获取studio内所有channel
  51. $user = AuthService::current($request);
  52. if (!$user) {
  53. return $this->error(__('auth.failed'));
  54. }
  55. //判断当前用户是否有指定的studio的权限
  56. $studioId = StudioApi::getIdByName($request->input('name'));
  57. if (!StudioApi::userCanList($user['user_uid'], $studioId)) {
  58. return $this->error(__('auth.failed'), 403, 403);
  59. }
  60. $table = Channel::select($indexCol);
  61. if ($request->input('view2', 'my') === 'my') {
  62. $table = $table->where('owner_uid', $studioId);
  63. } else {
  64. //协作
  65. $resList = ShareApi::getResList($studioId, 2);
  66. $resId = [];
  67. foreach ($resList as $res) {
  68. $resId[] = $res['res_id'];
  69. }
  70. $table = $table->whereIn('channels.uid', $resId);
  71. if ($request->input('collaborator', 'all') !== 'all') {
  72. $table = $table->where('owner_uid', $request->input('collaborator'));
  73. } else {
  74. $table = $table->where('owner_uid', '<>', $studioId);
  75. }
  76. }
  77. break;
  78. case 'studio-all':
  79. /**
  80. * studio 的和协作的
  81. */
  82. #获取user所有有权限的channel列表
  83. $user = AuthService::current($request);
  84. if (!$user) {
  85. return $this->error(__('auth.failed'));
  86. }
  87. //判断当前用户是否有指定的studio的权限
  88. if ($user['user_uid'] !== StudioApi::getIdByName($request->input('name'))) {
  89. return $this->error(__('auth.failed'));
  90. }
  91. $channelById = [];
  92. $channelId = [];
  93. //获取共享channel
  94. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  95. foreach ($allSharedChannels as $key => $value) {
  96. # code...
  97. $channelId[] = $value['res_id'];
  98. $channelById[$value['res_id']] = $value;
  99. }
  100. $table = Channel::select($indexCol)
  101. ->whereIn('uid', $channelId)
  102. ->orWhere('owner_uid', $user['user_uid']);
  103. break;
  104. case 'user-edit':
  105. /**
  106. * 某用户有编辑权限的
  107. */
  108. #获取user所有有权限的channel列表
  109. $user = AuthService::current($request);
  110. if (!$user) {
  111. return $this->error(__('auth.failed'));
  112. }
  113. $channelById = [];
  114. $channelId = [];
  115. //获取共享channel
  116. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  117. foreach ($allSharedChannels as $key => $value) {
  118. # code...
  119. if ($value['power'] >= 20) {
  120. $channelId[] = $value['res_id'];
  121. $channelById[$value['res_id']] = $value;
  122. }
  123. }
  124. $table = Channel::select($indexCol)
  125. ->whereIn('uid', $channelId)
  126. ->orWhere('owner_uid', $user['user_uid']);
  127. break;
  128. case 'user-in-chapter':
  129. #获取user 在某章节 所有有权限的channel列表
  130. $user = AuthService::current($request);
  131. if (!$user) {
  132. return $this->error(__('auth.failed'));
  133. }
  134. $channelById = [];
  135. $channelId = [];
  136. //获取共享channel
  137. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  138. foreach ($allSharedChannels as $key => $value) {
  139. # code...
  140. $channelId[] = $value['res_id'];
  141. $channelById[$value['res_id']] = $value;
  142. }
  143. //获取全网公开channel
  144. $chapter = PaliTextApi::getChapterStartEnd($request->input('book'), $request->input('para'));
  145. $publicChannelsWithContent = Sentence::where('book_id', $request->input('book'))
  146. ->whereBetween('paragraph', $chapter)
  147. ->where('strlen', '>', 0)
  148. ->where('status', 30)
  149. ->groupBy('channel_uid')
  150. ->select('channel_uid')
  151. ->get();
  152. foreach ($publicChannelsWithContent as $key => $value) {
  153. # code...
  154. $value['res_id'] = $value->channel_uid;
  155. $value['power'] = 10;
  156. $value['type'] = 2;
  157. if (!isset($channelById[$value['res_id']])) {
  158. $channelId[] = $value['res_id'];
  159. $channelById[$value['res_id']] = $value;
  160. }
  161. }
  162. $table = Channel::select($indexCol)
  163. ->whereIn('uid', $channelId)
  164. ->orWhere('owner_uid', $user['user_uid']);
  165. break;
  166. case 'system':
  167. $table = Channel::select($indexCol)
  168. ->where('owner_uid', config("mint.admin.root_uuid"));
  169. break;
  170. case 'paragraphs':
  171. $channels = Sentence::where('book_id', $request->input('book_id'))
  172. ->whereIn('paragraph', explode(',', $request->input('para')))
  173. ->groupBy('channel_uid')->select('channel_uid')->get();
  174. if (count($channels) > 0) {
  175. $channelIds = array_map(fn($item) => $item['channel_uid'], $channels->toArray());
  176. $table = Channel::select($indexCol)
  177. ->whereIn('uid', $channelIds);
  178. } else {
  179. $table = Channel::select($indexCol)->whereIsNull('uid');
  180. }
  181. break;
  182. case 'id':
  183. $table = Channel::select($indexCol)
  184. ->whereIn('uid', explode(',', $request->input("id")));
  185. }
  186. if ($request->has("book")) {
  187. if ($request->input("view") === "public") {
  188. $table = $table->leftJoin('progress_chapters', 'channels.uid', '=', 'progress_chapters.channel_id',)
  189. ->where('progress_chapters.book', $request->input("book"))
  190. ->where('progress_chapters.para', $request->input("paragraph"));
  191. } else {
  192. $table = $table->leftJoin('progress_chapters', function ($join) use ($request) {
  193. $join->on('channels.uid', '=', 'progress_chapters.channel_id')
  194. ->where('progress_chapters.book', $request->input("book"))
  195. ->where('progress_chapters.para', $request->input("paragraph")); // 条件写在这里!
  196. });
  197. }
  198. }
  199. //处理搜索
  200. if (!empty($request->input("search"))) {
  201. $table = $table->where('name', 'like', "%" . $request->input("search") . "%");
  202. }
  203. if ($request->has("type")) {
  204. $table = $table->where('type', $request->input("type"));
  205. }
  206. if ($request->has("updated_at")) {
  207. $table = $table->where('updated_at', '>', $request->input("updated_at"));
  208. }
  209. if ($request->has("created_at")) {
  210. $table = $table->where('created_at', '>', $request->input("created_at"));
  211. }
  212. //获取记录总条数
  213. $count = $table->count();
  214. //处理排序
  215. $table = $table->orderBy(
  216. $request->input("order", 'created_at'),
  217. $request->input("dir", 'desc')
  218. );
  219. //处理分页
  220. $table = $table->skip($request->input("offset", 0))
  221. ->take($request->input("limit", 200));
  222. //获取数据
  223. $result = $table->get();
  224. //TODO 将下面代码转移到resource
  225. if ($result) {
  226. if ($request->has('progress')) {
  227. //获取进度
  228. //获取单句长度
  229. $sentLen = PaliSentence::where('book', $request->input('book'))
  230. ->whereBetween('paragraph', $chapter)
  231. ->orderBy('word_begin')
  232. ->select(['book', 'paragraph', 'word_begin', 'word_end', 'length'])
  233. ->get();
  234. }
  235. foreach ($result as $key => $value) {
  236. if ($request->has('progress')) {
  237. //获取进度
  238. $finalTable = Sentence::where('book_id', $request->input('book'))
  239. ->whereBetween('paragraph', $chapter)
  240. ->where('channel_uid', $value->uid)
  241. ->where('strlen', '>', 0)
  242. ->select(['strlen', 'book_id', 'paragraph', 'word_start', 'word_end']);
  243. if ($finalTable->count() > 0) {
  244. $finished = $finalTable->get();
  245. $final = [];
  246. foreach ($sentLen as $sent) {
  247. # code...
  248. $first = Arr::first($finished, function ($value, $key) use ($sent) {
  249. return ($value->book_id == $sent->book &&
  250. $value->paragraph == $sent->paragraph &&
  251. $value->word_start == $sent->word_begin &&
  252. $value->word_end == $sent->word_end);
  253. });
  254. $final[] = [$sent->length, $first ? true : false];
  255. }
  256. $value['final'] = $final;
  257. }
  258. }
  259. //角色
  260. if (isset($user['user_uid'])) {
  261. if ($value->owner_uid === $user['user_uid']) {
  262. $value['role'] = 'owner';
  263. } else {
  264. if (isset($channelById) && isset($channelById[$value->uid])) {
  265. switch ($channelById[$value->uid]['power']) {
  266. case 10:
  267. # code...
  268. $value['role'] = 'member';
  269. break;
  270. case 20:
  271. $value['role'] = 'editor';
  272. break;
  273. case 30:
  274. $value['role'] = 'owner';
  275. break;
  276. default:
  277. # code...
  278. $value['role'] = $channelById[$value->uid]['power'];
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. # 获取studio信息
  285. $value->studio = StudioApi::getById($value->owner_uid);
  286. }
  287. return $this->ok(["rows" => $result, "count" => $count]);
  288. } else {
  289. return $this->ok(["rows" => [], "count" => 0]);
  290. }
  291. }
  292. /**
  293. * 获取我的,和协作channel数量
  294. *
  295. * @return \Illuminate\Http\Response
  296. */
  297. public function showMyNumber(Request $request)
  298. {
  299. $user = AuthService::current($request);
  300. if (!$user) {
  301. return $this->error(__('auth.failed'));
  302. }
  303. //判断当前用户是否有指定的studio的权限
  304. $studioId = StudioApi::getIdByName($request->input('studio'));
  305. if ($user['user_uid'] !== $studioId) {
  306. return $this->error(__('auth.failed'));
  307. }
  308. //我的
  309. $my = Channel::where('owner_uid', $studioId)->count();
  310. //协作
  311. $resList = ShareApi::getResList($studioId, 2);
  312. $resId = [];
  313. foreach ($resList as $res) {
  314. $resId[] = $res['res_id'];
  315. }
  316. $collaboration = Channel::whereIn('uid', $resId)->where('owner_uid', '<>', $studioId)->count();
  317. return $this->ok(['my' => $my, 'collaboration' => $collaboration]);
  318. }
  319. /**
  320. * 获取章节的进度
  321. *
  322. * @param \Illuminate\Http\Request $request
  323. * @return \Illuminate\Http\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 = array();
  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"] = \App\Http\Api\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. * @param \Illuminate\Http\Request $request
  518. * @return \Illuminate\Http\Response
  519. */
  520. public function store(Request $request)
  521. {
  522. //
  523. $user = AuthService::current($request);
  524. if (!$user) {
  525. return $this->error(__('auth.failed'), 401, 401);
  526. }
  527. //判断当前用户是否有指定的studio的权限
  528. $studioId = StudioApi::getIdByName($request->input('studio'));
  529. if (!StudioApi::userCanManage($user['user_uid'], $studioId)) {
  530. return $this->error(__('auth.failed'), 403, 403);
  531. }
  532. $studio = StudioApi::getById($studioId);
  533. //查询是否重复
  534. if (Channel::where('name', $request->input('name'))
  535. ->where('owner_uid', $studioId)
  536. ->exists()
  537. ) {
  538. return $this->error(__('validation.exists', ['name']), 200, 200);
  539. }
  540. $channel = new Channel;
  541. $channel->id = app('snowflake')->id();
  542. $channel->name = $request->input('name');
  543. $channel->owner_uid = $studioId;
  544. $channel->type = $request->input('type');
  545. $channel->lang = $request->input('lang');
  546. $channel->editor_id = $user['user_id'];
  547. if (isset($studio['roles'])) {
  548. if (in_array('basic', $studio['roles'])) {
  549. $channel->status = 5;
  550. }
  551. }
  552. $channel->create_time = time() * 1000;
  553. $channel->modify_time = time() * 1000;
  554. $channel->save();
  555. return $this->ok($channel);
  556. }
  557. /**
  558. * Display the specified resource.
  559. *
  560. * @param int $id
  561. * @return \Illuminate\Http\Response
  562. */
  563. public function show($id)
  564. {
  565. //
  566. $channel = Channel::find($id);
  567. if (!$channel) {
  568. return $this->error('no res');
  569. }
  570. $studio = StudioApi::getById($channel->owner_uid);
  571. $channel->studio = $studio;
  572. $channel->owner_info = ['nickname' => $studio['nickName'], 'username' => $studio['realName']];
  573. return $this->ok($channel);
  574. }
  575. /**
  576. * Display the specified resource.
  577. *
  578. * @param string $name
  579. * @return \Illuminate\Http\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. * @param \Illuminate\Http\Request $request
  596. * @param \App\Models\Channel $channel
  597. * @return \Illuminate\Http\Response
  598. */
  599. public function update(Request $request, Channel $channel)
  600. {
  601. //鉴权
  602. $user = AuthService::current($request);
  603. if (!$user) {
  604. return $this->error(__('auth.failed'), 401, 401);
  605. }
  606. if ($channel->is_system) {
  607. return $this->error('system channel', 403, 403);
  608. }
  609. if ($channel->owner_uid !== $user["user_uid"]) {
  610. //判断是否为协作
  611. $power = ShareApi::getResPower($user["user_uid"], $request->input('id'));
  612. if ($power < 30) {
  613. return $this->error(__('auth.failed'), 403, 403);
  614. }
  615. }
  616. $channel->name = $request->input('name');
  617. $channel->type = $request->input('type');
  618. $channel->summary = $request->input('summary');
  619. $channel->lang = $request->input('lang');
  620. $channel->status = $request->input('status');
  621. if ($request->has('source_type')) {
  622. $channel->source_type = $request->input('source_type');
  623. }
  624. if ($request->has('source_id')) {
  625. $channel->source_id = $request->input('source_id');
  626. }
  627. $channel->save();
  628. return $this->ok($channel);
  629. }
  630. /**
  631. * patch the specified resource in storage.
  632. *
  633. * @param \Illuminate\Http\Request $request
  634. * @param \App\Models\Channel $channel
  635. * @return \Illuminate\Http\Response
  636. */
  637. public function patch(Request $request, Channel $channel)
  638. {
  639. //鉴权
  640. $user = AuthService::current($request);
  641. if (!$user) {
  642. return $this->error(__('auth.failed'), [], 401);
  643. }
  644. if ($channel->is_system) {
  645. return $this->error('system channel', 403, 403);
  646. }
  647. if ($channel->owner_uid !== $user["user_uid"]) {
  648. //判断是否为协作
  649. $power = ShareApi::getResPower($user["user_uid"], $request->input('id'));
  650. if ($power < 30) {
  651. return $this->error(__('auth.failed'), [], 403);
  652. }
  653. }
  654. if ($request->has('name')) {
  655. $channel->name = $request->input('name');
  656. }
  657. if ($request->has('type')) {
  658. $channel->type = $request->input('type');
  659. }
  660. if ($request->has('summary')) {
  661. $channel->summary = $request->input('summary');
  662. }
  663. if ($request->has('lang')) {
  664. $channel->lang = $request->input('lang');
  665. }
  666. if ($request->has('status')) {
  667. $channel->status = $request->input('status');
  668. }
  669. if ($request->has('config')) {
  670. $channel->status = $request->input('config');
  671. }
  672. $channel->save();
  673. return $this->ok($channel);
  674. }
  675. /**
  676. * Remove the specified resource from storage.
  677. * @param \Illuminate\Http\Request $request
  678. * @param \App\Models\Channel $channel
  679. * @return \Illuminate\Http\Response
  680. */
  681. public function destroy(Request $request, Channel $channel)
  682. {
  683. //
  684. $user = AuthService::current($request);
  685. if (!$user) {
  686. return $this->error(__('auth.failed'));
  687. }
  688. //判断当前用户是否有指定的studio的权限
  689. if ($user['user_uid'] !== $channel->owner_uid) {
  690. return $this->error(__('auth.failed'));
  691. }
  692. //查询其他资源
  693. if (Sentence::where("channel_uid", $channel->uid)->exists()) {
  694. return $this->error("译文有数据无法删除");
  695. }
  696. if (DhammaTerm::where("channal", $channel->uid)->exists()) {
  697. return $this->error("术语有数据无法删除");
  698. }
  699. if (WbwBlock::where("channel_uid", $channel->uid)->exists()) {
  700. return $this->error("逐词解析有数据无法删除");
  701. }
  702. $delete = 0;
  703. DB::transaction(function () use ($channel, $delete) {
  704. //TODO 删除相关资源
  705. $delete = $channel->delete();
  706. });
  707. return $this->ok($delete);
  708. }
  709. }