ChannelController.php 26 KB

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