ChannelController.php 25 KB

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