2
0

ChannelController.php 24 KB

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