ChannelController.php 27 KB

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