ChannelController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <?php
  2. namespace App\Http\Controllers;
  3. require_once __DIR__.'/../../../public/app/ucenter/function.php';
  4. use App\Models\Channel;
  5. use App\Models\Sentence;
  6. use App\Models\DhammaTerm;
  7. use App\Models\WbwBlock;
  8. use App\Models\PaliSentence;
  9. use App\Http\Controllers\AuthController;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Log;
  12. use App\Http\Api\AuthApi;
  13. use App\Http\Api\StudioApi;
  14. use App\Http\Api\ShareApi;
  15. use App\Http\Api\PaliTextApi;
  16. use Illuminate\Support\Arr;
  17. use Illuminate\Support\Facades\DB;
  18. class ChannelController extends Controller
  19. {
  20. /**
  21. * Display a listing of the resource.
  22. *
  23. * @return \Illuminate\Http\Response
  24. */
  25. public function index(Request $request)
  26. {
  27. //
  28. $userinfo = new \UserInfo();
  29. $result=false;
  30. $indexCol = ['uid','name','summary',
  31. 'type','owner_uid','lang',
  32. 'status','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. $sentContainer = [];
  278. $sentLenContainer = [];
  279. foreach ($sent as $value) {
  280. $ids = explode('-',$value);
  281. if(count($ids)===4){
  282. $sentContainer[$value] = false;
  283. $query[] = $ids;
  284. }
  285. }
  286. //获取单句长度
  287. if(count($query)>0){
  288. $table = PaliSentence::whereIns(['book','paragraph','word_begin','word_end'],$query)
  289. ->select(['book','paragraph','word_begin','word_end','length']);
  290. $sentLen = $table->get();
  291. foreach ($sentLen as $value) {
  292. $sentLenContainer["{$value->book}-{$value->paragraph}-{$value->word_begin}-{$value->word_end}"] = $value->length;
  293. }
  294. }
  295. #获取 user 在某章节 所有有权限的 channel 列表
  296. $user = AuthApi::current($request);
  297. if(!$user){
  298. return $this->error(__('auth.failed'));
  299. }
  300. $channelById = [];
  301. $channelId = [];
  302. //获取共享channel
  303. $allSharedChannels = ShareApi::getResList($user['user_uid'],2);
  304. foreach ($allSharedChannels as $key => $value) {
  305. # code...
  306. $channelId[] = $value['res_id'];
  307. $channelById[$value['res_id']] = $value;
  308. }
  309. //获取全网公开的有译文的channel
  310. if(count($query)>0){
  311. $publicChannelsWithContent = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  312. ->where('strlen','>',0)
  313. ->where('status',30)
  314. ->groupBy('channel_uid')
  315. ->select('channel_uid')
  316. ->get();
  317. foreach ($publicChannelsWithContent as $key => $value) {
  318. # code...
  319. $value['res_id']=$value->channel_uid;
  320. $value['power'] = 10;
  321. $value['type'] = 2;
  322. if(!isset($channelById[$value['res_id']])){
  323. $channelId[] = $value['res_id'];
  324. $channelById[$value['res_id']] = $value;
  325. }
  326. }
  327. }
  328. //所有有这些句子译文的channel
  329. if(count($query) > 0){
  330. $allChannels = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  331. ->where('strlen','>',0)
  332. ->groupBy('channel_uid')
  333. ->select('channel_uid')
  334. ->get();
  335. }
  336. //所有需要查询的channel
  337. $result = Channel::select(['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'])
  338. ->whereIn('uid', $channelId)
  339. ->orWhere('owner_uid',$user['user_uid'])
  340. ->get();
  341. foreach ($result as $key => $value) {
  342. //角色
  343. if($value->owner_uid===$user['user_uid']){
  344. $value['role'] = 'owner';
  345. }else{
  346. if(isset($channelById[$value->uid])){
  347. switch ($channelById[$value->uid]['power']) {
  348. case 10:
  349. # code...
  350. $value['role'] = 'member';
  351. break;
  352. case 20:
  353. $value['role'] = 'editor';
  354. break;
  355. case 30:
  356. $value['role'] = 'owner';
  357. break;
  358. default:
  359. # code...
  360. $value['role'] = $channelById[$value->uid]['power'];
  361. break;
  362. }
  363. }
  364. }
  365. # 获取studio信息
  366. $result[$key]["studio"] = \App\Http\Api\StudioApi::getById($value->owner_uid);
  367. //获取进度
  368. if(count($query) > 0){
  369. $currChannelId = $value->uid;
  370. $hasContent = Arr::first($allChannels, function ($value, $key) use($currChannelId) {
  371. return ($value->channel_uid===$currChannelId);
  372. });
  373. if($hasContent && count($query)>0){
  374. $finalTable = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  375. ->where('channel_uid',$currChannelId)
  376. ->where('strlen','>',0)
  377. ->select(['strlen','book_id','paragraph','word_start','word_end']);
  378. if($finalTable->count()>0){
  379. $finished = $finalTable->get();
  380. $currChannel = [];
  381. foreach ($finished as $rowFinish) {
  382. $currChannel["{$rowFinish->book_id}-{$rowFinish->paragraph}-{$rowFinish->word_start}-{$rowFinish->word_end}"] = 1;
  383. }
  384. $final=[];
  385. foreach ($sentContainer as $sentId=>$rowSent) {
  386. # code...
  387. if(isset($currChannel[$sentId])){
  388. $final[] = [$sentLenContainer[$sentId],true];
  389. }else{
  390. $final[] = [$sentLenContainer[$sentId],false];
  391. }
  392. }
  393. $result[$key]['final'] = $final;
  394. }
  395. }
  396. }
  397. }
  398. return $this->ok(["rows"=>$result,count($result)]);
  399. }
  400. /**
  401. * Store a newly created resource in storage.
  402. *
  403. * @param \Illuminate\Http\Request $request
  404. * @return \Illuminate\Http\Response
  405. */
  406. public function store(Request $request)
  407. {
  408. //
  409. $user = AuthApi::current($request);
  410. if($user){
  411. //判断当前用户是否有指定的studio的权限
  412. if($user['user_uid'] === StudioApi::getIdByName($request->get('studio'))){
  413. //查询是否重复
  414. if(Channel::where('name',$request->get('name'))->where('owner_uid',$user['user_uid'])->exists()){
  415. return $this->error(__('validation.exists',['name']));
  416. }else{
  417. $channel = new Channel;
  418. $channel->id = app('snowflake')->id();
  419. $channel->name = $request->get('name');
  420. $channel->owner_uid = $user['user_uid'];
  421. $channel->type = $request->get('type');
  422. $channel->lang = $request->get('lang');
  423. $channel->editor_id = $user['user_id'];
  424. $channel->create_time = time()*1000;
  425. $channel->modify_time = time()*1000;
  426. $channel->save();
  427. return $this->ok($channel);
  428. }
  429. }else{
  430. return $this->error(__('auth.failed'));
  431. }
  432. }else{
  433. return $this->error(__('auth.failed'));
  434. }
  435. }
  436. /**
  437. * Display the specified resource.
  438. *
  439. * @param int $id
  440. * @return \Illuminate\Http\Response
  441. */
  442. public function show($id)
  443. {
  444. //
  445. $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
  446. $channel = Channel::where("uid",$id)->select($indexCol)->first();
  447. $userinfo = new \UserInfo();
  448. $studio = $userinfo->getName($channel->owner_uid);
  449. $channel->owner_info = $studio;
  450. $channel->studio = [
  451. 'id'=>$channel->owner_uid,
  452. 'nickName'=>$studio['nickname'],
  453. 'studioName'=>$studio['username'],
  454. 'avastar'=>'',
  455. 'owner' => [
  456. 'id'=>$channel->owner_uid,
  457. 'nickName'=>$studio['nickname'],
  458. 'userName'=>$studio['username'],
  459. 'avastar'=>'',
  460. ]
  461. ];
  462. return $this->ok($channel);
  463. }
  464. /**
  465. * Update the specified resource in storage.
  466. *
  467. * @param \Illuminate\Http\Request $request
  468. * @param \App\Models\Channel $channel
  469. * @return \Illuminate\Http\Response
  470. */
  471. public function update(Request $request, Channel $channel)
  472. {
  473. //鉴权
  474. $user = AuthApi::current($request);
  475. if(!$user){
  476. return $this->error(__('auth.failed'),[],401);
  477. }
  478. if($channel->owner_uid !== $user["user_uid"]){
  479. //判断是否为协作
  480. $power = ShareApi::getResPower($user["user_uid"],$request->get('id'));
  481. if($power < 30){
  482. return $this->error(__('auth.failed'),[],403);
  483. }
  484. }
  485. $channel->name = $request->get('name');
  486. $channel->type = $request->get('type');
  487. $channel->summary = $request->get('summary');
  488. $channel->lang = $request->get('lang');
  489. $channel->status = $request->get('status');
  490. $channel->save();
  491. return $this->ok($channel);
  492. }
  493. /**
  494. * patch 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 patch(Request $request, Channel $channel)
  501. {
  502. //鉴权
  503. $user = AuthApi::current($request);
  504. if(!$user){
  505. return $this->error(__('auth.failed'),[],401);
  506. }
  507. if($channel->owner_uid !== $user["user_uid"]){
  508. //判断是否为协作
  509. $power = ShareApi::getResPower($user["user_uid"],$request->get('id'));
  510. if($power < 30){
  511. return $this->error(__('auth.failed'),[],403);
  512. }
  513. }
  514. if($request->has('name')){$channel->name = $request->get('name');}
  515. if($request->has('type')){$channel->type = $request->get('type');}
  516. if($request->has('summary')){$channel->summary = $request->get('summary');}
  517. if($request->has('lang')){$channel->lang = $request->get('lang');}
  518. if($request->has('status')){$channel->status = $request->get('status');}
  519. if($request->has('config')){$channel->status = $request->get('config');}
  520. $channel->save();
  521. return $this->ok($channel);
  522. }
  523. /**
  524. * Remove the specified resource from storage.
  525. * @param \Illuminate\Http\Request $request
  526. * @param \App\Models\Channel $channel
  527. * @return \Illuminate\Http\Response
  528. */
  529. public function destroy(Request $request,Channel $channel)
  530. {
  531. //
  532. $user = AuthApi::current($request);
  533. if(!$user){
  534. return $this->error(__('auth.failed'));
  535. }
  536. //判断当前用户是否有指定的studio的权限
  537. if($user['user_uid'] !== $channel->owner_uid){
  538. return $this->error(__('auth.failed'));
  539. }
  540. //查询其他资源
  541. if(Sentence::where("channel_uid",$channel->uid)->exists()){
  542. return $this->error("译文有数据无法删除");
  543. }
  544. if(DhammaTerm::where("channal",$channel->uid)->exists()){
  545. return $this->error("术语有数据无法删除");
  546. }
  547. if(WbwBlock::where("channel_uid",$channel->uid)->exists()){
  548. return $this->error("逐词解析有数据无法删除");
  549. }
  550. $delete = 0;
  551. DB::transaction(function() use($channel,$delete){
  552. //TODO 删除相关资源
  553. $delete = $channel->delete();
  554. });
  555. return $this->ok($delete);
  556. }
  557. }