ChannelController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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\Http\Api\AuthApi;
  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. /*
  50. if ($request->has("book")) {
  51. $table = $table->leftJoin('progress_chapters', 'channels.uid', '=', 'progress_chapters.channel_id',)
  52. ->where('progress_chapters.book', $request->input("book"))
  53. ->where('progress_chapters.para', $request->input("paragraph"));
  54. }*/
  55. break;
  56. case 'studio':
  57. # 获取studio内所有channel
  58. $user = AuthApi::current($request);
  59. if (!$user) {
  60. return $this->error(__('auth.failed'));
  61. }
  62. //判断当前用户是否有指定的studio的权限
  63. $studioId = StudioApi::getIdByName($request->input('name'));
  64. if (!StudioApi::userCanList($user['user_uid'], $studioId)) {
  65. return $this->error(__('auth.failed'), 403, 403);
  66. }
  67. $table = Channel::select($indexCol);
  68. if ($request->input('view2', 'my') === 'my') {
  69. $table = $table->where('owner_uid', $studioId);
  70. } else {
  71. //协作
  72. $resList = ShareApi::getResList($studioId, 2);
  73. $resId = [];
  74. foreach ($resList as $res) {
  75. $resId[] = $res['res_id'];
  76. }
  77. $table = $table->whereIn('channels.uid', $resId);
  78. if ($request->input('collaborator', 'all') !== 'all') {
  79. $table = $table->where('owner_uid', $request->input('collaborator'));
  80. } else {
  81. $table = $table->where('owner_uid', '<>', $studioId);
  82. }
  83. }
  84. break;
  85. case 'studio-all':
  86. /**
  87. * studio 的和协作的
  88. */
  89. #获取user所有有权限的channel列表
  90. $user = AuthApi::current($request);
  91. if (!$user) {
  92. return $this->error(__('auth.failed'));
  93. }
  94. //判断当前用户是否有指定的studio的权限
  95. if ($user['user_uid'] !== StudioApi::getIdByName($request->input('name'))) {
  96. return $this->error(__('auth.failed'));
  97. }
  98. $channelById = [];
  99. $channelId = [];
  100. //获取共享channel
  101. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  102. foreach ($allSharedChannels as $key => $value) {
  103. # code...
  104. $channelId[] = $value['res_id'];
  105. $channelById[$value['res_id']] = $value;
  106. }
  107. $table = Channel::select($indexCol)
  108. ->whereIn('uid', $channelId)
  109. ->orWhere('owner_uid', $user['user_uid']);
  110. break;
  111. case 'user-edit':
  112. /**
  113. * 某用户有编辑权限的
  114. */
  115. #获取user所有有权限的channel列表
  116. $user = AuthApi::current($request);
  117. if (!$user) {
  118. return $this->error(__('auth.failed'));
  119. }
  120. $channelById = [];
  121. $channelId = [];
  122. //获取共享channel
  123. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  124. foreach ($allSharedChannels as $key => $value) {
  125. # code...
  126. if ($value['power'] >= 20) {
  127. $channelId[] = $value['res_id'];
  128. $channelById[$value['res_id']] = $value;
  129. }
  130. }
  131. $table = Channel::select($indexCol)
  132. ->whereIn('uid', $channelId)
  133. ->orWhere('owner_uid', $user['user_uid']);
  134. break;
  135. case 'user-in-chapter':
  136. #获取user 在某章节 所有有权限的channel列表
  137. $user = AuthApi::current($request);
  138. if (!$user) {
  139. return $this->error(__('auth.failed'));
  140. }
  141. $channelById = [];
  142. $channelId = [];
  143. //获取共享channel
  144. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  145. foreach ($allSharedChannels as $key => $value) {
  146. # code...
  147. $channelId[] = $value['res_id'];
  148. $channelById[$value['res_id']] = $value;
  149. }
  150. //获取全网公开channel
  151. $chapter = PaliTextApi::getChapterStartEnd($request->input('book'), $request->input('para'));
  152. $publicChannelsWithContent = Sentence::where('book_id', $request->input('book'))
  153. ->whereBetween('paragraph', $chapter)
  154. ->where('strlen', '>', 0)
  155. ->where('status', 30)
  156. ->groupBy('channel_uid')
  157. ->select('channel_uid')
  158. ->get();
  159. foreach ($publicChannelsWithContent as $key => $value) {
  160. # code...
  161. $value['res_id'] = $value->channel_uid;
  162. $value['power'] = 10;
  163. $value['type'] = 2;
  164. if (!isset($channelById[$value['res_id']])) {
  165. $channelId[] = $value['res_id'];
  166. $channelById[$value['res_id']] = $value;
  167. }
  168. }
  169. $table = Channel::select($indexCol)
  170. ->whereIn('uid', $channelId)
  171. ->orWhere('owner_uid', $user['user_uid']);
  172. break;
  173. case 'system':
  174. $table = Channel::select($indexCol)
  175. ->where('owner_uid', config("mint.admin.root_uuid"));
  176. break;
  177. case 'id':
  178. $table = Channel::select($indexCol)
  179. ->whereIn('uid', explode(',', $request->input("id")));
  180. }
  181. if ($request->has("book")) {
  182. if ($request->input("view") === "public") {
  183. $table = $table->leftJoin('progress_chapters', 'channels.uid', '=', 'progress_chapters.channel_id',)
  184. ->where('progress_chapters.book', $request->input("book"))
  185. ->where('progress_chapters.para', $request->input("paragraph"));
  186. } else {
  187. $table = $table->leftJoin('progress_chapters', function ($join) use ($request) {
  188. $join->on('channels.uid', '=', 'progress_chapters.channel_id')
  189. ->where('progress_chapters.book', $request->input("book"))
  190. ->where('progress_chapters.para', $request->input("paragraph")); // 条件写在这里!
  191. });
  192. }
  193. /* leftJoin('progress_chapters', '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. if (!empty($request->input("search"))) {
  199. $table = $table->where('name', 'like', "%" . $request->input("search") . "%");
  200. }
  201. if ($request->has("type")) {
  202. $table = $table->where('type', $request->input("type"));
  203. }
  204. if ($request->has("updated_at")) {
  205. $table = $table->where('updated_at', '>', $request->input("updated_at"));
  206. }
  207. if ($request->has("created_at")) {
  208. $table = $table->where('created_at', '>', $request->input("created_at"));
  209. }
  210. //获取记录总条数
  211. $count = $table->count();
  212. //处理排序
  213. $table = $table->orderBy(
  214. $request->input("order", 'created_at'),
  215. $request->input("dir", 'desc')
  216. );
  217. //处理分页
  218. $table = $table->skip($request->input("offset", 0))
  219. ->take($request->input("limit", 200));
  220. Log::debug('channel sql ' . $table->toSql());
  221. //获取数据
  222. $result = $table->get();
  223. //TODO 将下面代码转移到resource
  224. if ($result) {
  225. if ($request->has('progress')) {
  226. //获取进度
  227. //获取单句长度
  228. $sentLen = PaliSentence::where('book', $request->input('book'))
  229. ->whereBetween('paragraph', $chapter)
  230. ->orderBy('word_begin')
  231. ->select(['book', 'paragraph', 'word_begin', 'word_end', 'length'])
  232. ->get();
  233. }
  234. foreach ($result as $key => $value) {
  235. if ($request->has('progress')) {
  236. //获取进度
  237. $finalTable = Sentence::where('book_id', $request->input('book'))
  238. ->whereBetween('paragraph', $chapter)
  239. ->where('channel_uid', $value->uid)
  240. ->where('strlen', '>', 0)
  241. ->select(['strlen', 'book_id', 'paragraph', 'word_start', 'word_end']);
  242. if ($finalTable->count() > 0) {
  243. $finished = $finalTable->get();
  244. $final = [];
  245. foreach ($sentLen as $sent) {
  246. # code...
  247. $first = Arr::first($finished, function ($value, $key) use ($sent) {
  248. return ($value->book_id == $sent->book &&
  249. $value->paragraph == $sent->paragraph &&
  250. $value->word_start == $sent->word_begin &&
  251. $value->word_end == $sent->word_end);
  252. });
  253. $final[] = [$sent->length, $first ? true : false];
  254. }
  255. $value['final'] = $final;
  256. }
  257. }
  258. //角色
  259. if (isset($user['user_uid'])) {
  260. if ($value->owner_uid === $user['user_uid']) {
  261. $value['role'] = 'owner';
  262. } else {
  263. if (isset($channelById[$value->uid])) {
  264. switch ($channelById[$value->uid]['power']) {
  265. case 10:
  266. # code...
  267. $value['role'] = 'member';
  268. break;
  269. case 20:
  270. $value['role'] = 'editor';
  271. break;
  272. case 30:
  273. $value['role'] = 'owner';
  274. break;
  275. default:
  276. # code...
  277. $value['role'] = $channelById[$value->uid]['power'];
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. # 获取studio信息
  284. $value->studio = StudioApi::getById($value->owner_uid);
  285. }
  286. return $this->ok(["rows" => $result, "count" => $count]);
  287. } else {
  288. return $this->ok(["rows" => [], "count" => 0]);
  289. }
  290. }
  291. /**
  292. * 获取我的,和协作channel数量
  293. *
  294. * @return \Illuminate\Http\Response
  295. */
  296. public function showMyNumber(Request $request)
  297. {
  298. $user = AuthApi::current($request);
  299. if (!$user) {
  300. return $this->error(__('auth.failed'));
  301. }
  302. //判断当前用户是否有指定的studio的权限
  303. $studioId = StudioApi::getIdByName($request->input('studio'));
  304. if ($user['user_uid'] !== $studioId) {
  305. return $this->error(__('auth.failed'));
  306. }
  307. //我的
  308. $my = Channel::where('owner_uid', $studioId)->count();
  309. //协作
  310. $resList = ShareApi::getResList($studioId, 2);
  311. $resId = [];
  312. foreach ($resList as $res) {
  313. $resId[] = $res['res_id'];
  314. }
  315. $collaboration = Channel::whereIn('uid', $resId)->where('owner_uid', '<>', $studioId)->count();
  316. return $this->ok(['my' => $my, 'collaboration' => $collaboration]);
  317. }
  318. /**
  319. * 获取章节的进度
  320. *
  321. * @param \Illuminate\Http\Request $request
  322. * @return \Illuminate\Http\Response
  323. */
  324. public function progress(Request $request)
  325. {
  326. $indexCol = ['uid', 'name', 'summary', 'type', 'owner_uid', 'lang', 'status', 'updated_at', 'created_at'];
  327. $sent = $request->input('sentence');
  328. $query = [];
  329. $queryWithChannel = [];
  330. $sentContainer = [];
  331. $sentLenContainer = [];
  332. $paliChannel = ChannelApi::getSysChannel('_System_Pali_VRI_');
  333. $customBookChannel = array();
  334. foreach ($sent as $value) {
  335. $ids = explode('-', $value);
  336. $idWithChannel = $ids;
  337. if (count($ids) === 4) {
  338. if ($ids[0] < 1000) {
  339. $idWithChannel[] = $paliChannel;
  340. } else {
  341. if (!isset($customBookChannel[$ids[0]])) {
  342. $cbChannel = CustomBook::where('book_id', $ids[0])->value('channel_id');
  343. if ($cbChannel) {
  344. $customBookChannel[$ids[0]] = $cbChannel;
  345. } else {
  346. $customBookChannel[$ids[0]] = $paliChannel;
  347. }
  348. }
  349. $idWithChannel[] = $customBookChannel[$ids[0]];
  350. }
  351. $sentContainer[$value] = false;
  352. $query[] = $ids;
  353. $queryWithChannel[] = $idWithChannel;
  354. }
  355. }
  356. //获取单句长度
  357. if (count($query) > 0) {
  358. $table = Sentence::whereIns([
  359. 'book_id',
  360. 'paragraph',
  361. 'word_start',
  362. 'word_end',
  363. 'channel_uid'
  364. ], $queryWithChannel)
  365. ->select(['book_id', 'paragraph', 'word_start', 'word_end', 'strlen']);
  366. $sentLen = $table->get();
  367. foreach ($sentLen as $value) {
  368. $strlen = $value->strlen;
  369. if (empty($strlen)) {
  370. $strlen = 0;
  371. }
  372. $sentId = "{$value->book_id}-{$value->paragraph}-{$value->word_start}-{$value->word_end}";
  373. $sentLenContainer[$sentId] = $strlen;
  374. }
  375. }
  376. $channelById = [];
  377. $channelId = [];
  378. //获取全网公开的有译文的channel
  379. if ($request->input('owner') === 'all' || $request->input('owner') === 'public') {
  380. if (count($query) > 0) {
  381. $fields = ['book_id', 'paragraph', 'word_start', 'word_end'];
  382. $publicChannelsWithContent = Sentence::whereIns($fields, $query)
  383. ->where('strlen', '>', 0)
  384. ->where('status', 30)
  385. ->groupBy('channel_uid')
  386. ->select('channel_uid')
  387. ->get();
  388. foreach ($publicChannelsWithContent as $key => $value) {
  389. # code...
  390. $value['res_id'] = $value->channel_uid;
  391. $value['power'] = 10;
  392. $value['type'] = 2;
  393. if (!isset($channelById[$value['res_id']])) {
  394. $channelId[] = $value['res_id'];
  395. $channelById[$value['res_id']] = $value;
  396. }
  397. }
  398. }
  399. }
  400. #获取 user 在某章节 所有有权限的 channel 列表
  401. $user = AuthApi::current($request);
  402. if ($user !== false) {
  403. //我自己的
  404. if ($request->input('owner') === 'all' || $request->input('owner') === 'my') {
  405. $my = Channel::select($indexCol)->where('owner_uid', $user['user_uid'])->get();
  406. foreach ($my as $key => $value) {
  407. $channelId[] = $value->uid;
  408. $channelById[$value->uid] = [
  409. 'res_id' => $value->uid,
  410. 'power' => 30,
  411. 'type' => 2,
  412. ];
  413. }
  414. }
  415. //获取共享channel
  416. if ($request->input('owner') === 'all' || $request->input('owner') === 'collaborator') {
  417. $allSharedChannels = ShareApi::getResList($user['user_uid'], 2);
  418. foreach ($allSharedChannels as $key => $value) {
  419. # code...
  420. if (!in_array($value['res_id'], $channelId)) {
  421. $channelId[] = $value['res_id'];
  422. $channelById[$value['res_id']] = $value;
  423. }
  424. }
  425. }
  426. }
  427. //所有有这些句子译文的channel
  428. if (count($query) > 0) {
  429. $allChannels = Sentence::whereIns(['book_id', 'paragraph', 'word_start', 'word_end'], $query)
  430. ->where('strlen', '>', 0)
  431. ->groupBy('channel_uid')
  432. ->select('channel_uid')
  433. ->get();
  434. }
  435. //所有需要查询的channel
  436. $table = Channel::select(['uid', 'name', 'summary', 'type', 'owner_uid', 'lang', 'status', 'updated_at', 'created_at'])
  437. ->whereIn('uid', $channelId);
  438. if ($user !== false) {
  439. $table->orWhere('owner_uid', $user['user_uid']);
  440. }
  441. $result = $table->get();
  442. foreach ($result as $key => $value) {
  443. //角色
  444. if ($user !== false && $value->owner_uid === $user['user_uid']) {
  445. $value['role'] = 'owner';
  446. } else {
  447. if (isset($channelById[$value->uid])) {
  448. switch ($channelById[$value->uid]['power']) {
  449. case 10:
  450. # code...
  451. $value['role'] = 'member';
  452. break;
  453. case 20:
  454. $value['role'] = 'editor';
  455. break;
  456. case 30:
  457. $value['role'] = 'owner';
  458. break;
  459. default:
  460. # code...
  461. $value['role'] = $channelById[$value->uid]['power'];
  462. break;
  463. }
  464. }
  465. }
  466. # 获取studio信息
  467. $result[$key]["studio"] = \App\Http\Api\StudioApi::getById($value->owner_uid);
  468. //获取进度
  469. if (count($query) > 0) {
  470. $currChannelId = $value->uid;
  471. $hasContent = Arr::first($allChannels, function ($value, $key) use ($currChannelId) {
  472. return ($value->channel_uid === $currChannelId);
  473. });
  474. if ($hasContent && count($query) > 0) {
  475. $finalTable = Sentence::whereIns(['book_id', 'paragraph', 'word_start', 'word_end'], $query)
  476. ->where('channel_uid', $currChannelId)
  477. ->where('strlen', '>', 0)
  478. ->select(['strlen', 'book_id', 'paragraph', 'word_start', 'word_end', 'created_at', 'updated_at']);
  479. $created_at = time();
  480. $edit_at = 0;
  481. if ($finalTable->count() > 0) {
  482. $finished = $finalTable->get();
  483. $currChannel = [];
  484. foreach ($finished as $rowFinish) {
  485. $createTime = strtotime($rowFinish->created_at);
  486. $updateTime = strtotime($rowFinish->updated_at);
  487. if ($createTime < $created_at) {
  488. $created_at = $createTime;
  489. }
  490. if ($updateTime > $edit_at) {
  491. $edit_at = $updateTime;
  492. }
  493. $currChannel["{$rowFinish->book_id}-{$rowFinish->paragraph}-{$rowFinish->word_start}-{$rowFinish->word_end}"] = 1;
  494. }
  495. $final = [];
  496. foreach ($sentContainer as $sentId => $rowSent) {
  497. # code...
  498. if (isset($currChannel[$sentId])) {
  499. $final[] = [$sentLenContainer[$sentId], true];
  500. } else {
  501. $final[] = [$sentLenContainer[$sentId], false];
  502. }
  503. }
  504. $result[$key]['final'] = $final;
  505. $result[$key]['content_created_at'] = date('Y-m-d H:i:s', $created_at);
  506. $result[$key]['content_updated_at'] = date('Y-m-d H:i:s', $edit_at);
  507. }
  508. }
  509. }
  510. }
  511. return $this->ok(["rows" => $result, count($result)]);
  512. }
  513. /**
  514. * Store a newly created resource in storage.
  515. *
  516. * @param \Illuminate\Http\Request $request
  517. * @return \Illuminate\Http\Response
  518. */
  519. public function store(Request $request)
  520. {
  521. //
  522. $user = AuthApi::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 \Illuminate\Http\Response
  561. */
  562. public function show($id)
  563. {
  564. //
  565. $indexCol = ['uid', 'name', 'summary', 'type', 'owner_uid', 'lang', 'is_system', 'status', 'updated_at', 'created_at'];
  566. $channel = Channel::where("uid", $id)->select($indexCol)->first();
  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 = AuthApi::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. $channel->save();
  622. return $this->ok($channel);
  623. }
  624. /**
  625. * patch the specified resource in storage.
  626. *
  627. * @param \Illuminate\Http\Request $request
  628. * @param \App\Models\Channel $channel
  629. * @return \Illuminate\Http\Response
  630. */
  631. public function patch(Request $request, Channel $channel)
  632. {
  633. //鉴权
  634. $user = AuthApi::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. * @param \Illuminate\Http\Request $request
  672. * @param \App\Models\Channel $channel
  673. * @return \Illuminate\Http\Response
  674. */
  675. public function destroy(Request $request, Channel $channel)
  676. {
  677. //
  678. $user = AuthApi::current($request);
  679. if (!$user) {
  680. return $this->error(__('auth.failed'));
  681. }
  682. //判断当前用户是否有指定的studio的权限
  683. if ($user['user_uid'] !== $channel->owner_uid) {
  684. return $this->error(__('auth.failed'));
  685. }
  686. //查询其他资源
  687. if (Sentence::where("channel_uid", $channel->uid)->exists()) {
  688. return $this->error("译文有数据无法删除");
  689. }
  690. if (DhammaTerm::where("channal", $channel->uid)->exists()) {
  691. return $this->error("术语有数据无法删除");
  692. }
  693. if (WbwBlock::where("channel_uid", $channel->uid)->exists()) {
  694. return $this->error("逐词解析有数据无法删除");
  695. }
  696. $delete = 0;
  697. DB::transaction(function () use ($channel, $delete) {
  698. //TODO 删除相关资源
  699. $delete = $channel->delete();
  700. });
  701. return $this->ok($delete);
  702. }
  703. }