ChannelController.php 29 KB

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