ChannelController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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','type','owner_uid','lang','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. //获取记录总条数
  159. $count = $table->count();
  160. //处理排序
  161. $table = $table->orderBy($request->get("order",'updated_at'),
  162. $request->get("dir",'desc'));
  163. //处理分页
  164. $table = $table->skip($request->get("offset",0))
  165. ->take($request->get("limit",200));
  166. //获取数据
  167. $result = $table->get();
  168. //TODO 将下面代码转移到resource
  169. if($result){
  170. if($request->has('progress')){
  171. //获取进度
  172. //获取单句长度
  173. $sentLen = PaliSentence::where('book',$request->get('book'))
  174. ->whereBetween('paragraph',$chapter)
  175. ->orderBy('word_begin')
  176. ->select(['book','paragraph','word_begin','word_end','length'])
  177. ->get();
  178. }
  179. foreach ($result as $key => $value) {
  180. if($request->has('progress')){
  181. //获取进度
  182. $finalTable = Sentence::where('book_id',$request->get('book'))
  183. ->whereBetween('paragraph',$chapter)
  184. ->where('channel_uid',$value->uid)
  185. ->where('strlen','>',0)
  186. ->select(['strlen','book_id','paragraph','word_start','word_end']);
  187. if($finalTable->count()>0){
  188. $finished = $finalTable->get();
  189. $final=[];
  190. foreach ($sentLen as $sent) {
  191. # code...
  192. $first = Arr::first($finished, function ($value, $key) use($sent) {
  193. return ($value->book_id==$sent->book &&
  194. $value->paragraph==$sent->paragraph &&
  195. $value->word_start==$sent->word_begin &&
  196. $value->word_end==$sent->word_end);
  197. });
  198. $final[] = [$sent->length,$first?true:false];
  199. }
  200. $value['final'] = $final;
  201. }
  202. }
  203. //角色
  204. if(isset($user['user_uid'])){
  205. if($value->owner_uid===$user['user_uid']){
  206. $value['role'] = 'owner';
  207. }else{
  208. if(isset($channelById[$value->uid])){
  209. switch ($channelById[$value->uid]['power']) {
  210. case 10:
  211. # code...
  212. $value['role'] = 'member';
  213. break;
  214. case 20:
  215. $value['role'] = 'editor';
  216. break;
  217. case 30:
  218. $value['role'] = 'owner';
  219. break;
  220. default:
  221. # code...
  222. $value['role'] = $channelById[$value->uid]['power'];
  223. break;
  224. }
  225. }
  226. }
  227. }
  228. # 获取studio信息
  229. $value->studio = StudioApi::getById($value->owner_uid);
  230. }
  231. return $this->ok(["rows"=>$result,"count"=>$count]);
  232. }else{
  233. return $this->error("没有查询到数据");
  234. }
  235. }
  236. /**
  237. * 获取我的,和协作channel数量
  238. *
  239. * @return \Illuminate\Http\Response
  240. */
  241. public function showMyNumber(Request $request){
  242. $user = AuthApi::current($request);
  243. if(!$user){
  244. return $this->error(__('auth.failed'));
  245. }
  246. //判断当前用户是否有指定的studio的权限
  247. $studioId = StudioApi::getIdByName($request->get('studio'));
  248. if($user['user_uid'] !== $studioId){
  249. return $this->error(__('auth.failed'));
  250. }
  251. //我的
  252. $my = Channel::where('owner_uid', $studioId)->count();
  253. //协作
  254. $resList = ShareApi::getResList($studioId,2);
  255. $resId=[];
  256. foreach ($resList as $res) {
  257. $resId[] = $res['res_id'];
  258. }
  259. $collaboration = Channel::whereIn('uid', $resId)->where('owner_uid','<>', $studioId)->count();
  260. return $this->ok(['my'=>$my,'collaboration'=>$collaboration]);
  261. }
  262. /**
  263. * 获取章节的进度
  264. *
  265. * @param \Illuminate\Http\Request $request
  266. * @return \Illuminate\Http\Response
  267. */
  268. public function progress(Request $request){
  269. $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
  270. $sent = $request->get('sentence') ;
  271. $query = [];
  272. $sentContainer = [];
  273. $sentLenContainer = [];
  274. foreach ($sent as $value) {
  275. $ids = explode('-',$value);
  276. if(count($ids)===4){
  277. $sentContainer[$value] = false;
  278. $query[] = $ids;
  279. }
  280. }
  281. //获取单句长度
  282. if(count($query)>0){
  283. $table = PaliSentence::whereIns(['book','paragraph','word_begin','word_end'],$query)
  284. ->select(['book','paragraph','word_begin','word_end','length']);
  285. $sentLen = $table->get();
  286. foreach ($sentLen as $value) {
  287. $sentLenContainer["{$value->book}-{$value->paragraph}-{$value->word_begin}-{$value->word_end}"] = $value->length;
  288. }
  289. }
  290. #获取 user 在某章节 所有有权限的 channel 列表
  291. $user = AuthApi::current($request);
  292. if(!$user){
  293. return $this->error(__('auth.failed'));
  294. }
  295. $channelById = [];
  296. $channelId = [];
  297. //获取共享channel
  298. $allSharedChannels = ShareApi::getResList($user['user_uid'],2);
  299. foreach ($allSharedChannels as $key => $value) {
  300. # code...
  301. $channelId[] = $value['res_id'];
  302. $channelById[$value['res_id']] = $value;
  303. }
  304. //获取全网公开的有译文的channel
  305. if(count($query)>0){
  306. $publicChannelsWithContent = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  307. ->where('strlen','>',0)
  308. ->where('status',30)
  309. ->groupBy('channel_uid')
  310. ->select('channel_uid')
  311. ->get();
  312. foreach ($publicChannelsWithContent as $key => $value) {
  313. # code...
  314. $value['res_id']=$value->channel_uid;
  315. $value['power'] = 10;
  316. $value['type'] = 2;
  317. if(!isset($channelById[$value['res_id']])){
  318. $channelId[] = $value['res_id'];
  319. $channelById[$value['res_id']] = $value;
  320. }
  321. }
  322. }
  323. //所有有这些句子译文的channel
  324. if(count($query) > 0){
  325. $allChannels = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  326. ->where('strlen','>',0)
  327. ->groupBy('channel_uid')
  328. ->select('channel_uid')
  329. ->get();
  330. }
  331. //所有需要查询的channel
  332. $result = Channel::select(['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'])
  333. ->whereIn('uid', $channelId)
  334. ->orWhere('owner_uid',$user['user_uid'])
  335. ->get();
  336. foreach ($result as $key => $value) {
  337. //角色
  338. if($value->owner_uid===$user['user_uid']){
  339. $value['role'] = 'owner';
  340. }else{
  341. if(isset($channelById[$value->uid])){
  342. switch ($channelById[$value->uid]['power']) {
  343. case 10:
  344. # code...
  345. $value['role'] = 'member';
  346. break;
  347. case 20:
  348. $value['role'] = 'editor';
  349. break;
  350. case 30:
  351. $value['role'] = 'owner';
  352. break;
  353. default:
  354. # code...
  355. $value['role'] = $channelById[$value->uid]['power'];
  356. break;
  357. }
  358. }
  359. }
  360. # 获取studio信息
  361. $result[$key]["studio"] = \App\Http\Api\StudioApi::getById($value->owner_uid);
  362. //获取进度
  363. if(count($query) > 0){
  364. $currChannelId = $value->uid;
  365. $hasContent = Arr::first($allChannels, function ($value, $key) use($currChannelId) {
  366. return ($value->channel_uid===$currChannelId);
  367. });
  368. if($hasContent && count($query)>0){
  369. $finalTable = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  370. ->where('channel_uid',$currChannelId)
  371. ->where('strlen','>',0)
  372. ->select(['strlen','book_id','paragraph','word_start','word_end']);
  373. if($finalTable->count()>0){
  374. $finished = $finalTable->get();
  375. $currChannel = [];
  376. foreach ($finished as $rowFinish) {
  377. $currChannel["{$rowFinish->book_id}-{$rowFinish->paragraph}-{$rowFinish->word_start}-{$rowFinish->word_end}"] = 1;
  378. }
  379. $final=[];
  380. foreach ($sentContainer as $sentId=>$rowSent) {
  381. # code...
  382. if(isset($currChannel[$sentId])){
  383. $final[] = [$sentLenContainer[$sentId],true];
  384. }else{
  385. $final[] = [$sentLenContainer[$sentId],false];
  386. }
  387. }
  388. $result[$key]['final'] = $final;
  389. }
  390. }
  391. }
  392. }
  393. return $this->ok(["rows"=>$result,count($result)]);
  394. }
  395. /**
  396. * Store a newly created resource in storage.
  397. *
  398. * @param \Illuminate\Http\Request $request
  399. * @return \Illuminate\Http\Response
  400. */
  401. public function store(Request $request)
  402. {
  403. //
  404. $user = AuthApi::current($request);
  405. if($user){
  406. //判断当前用户是否有指定的studio的权限
  407. if($user['user_uid'] === StudioApi::getIdByName($request->get('studio'))){
  408. //查询是否重复
  409. if(Channel::where('name',$request->get('name'))->where('owner_uid',$user['user_uid'])->exists()){
  410. return $this->error(__('validation.exists',['name']));
  411. }else{
  412. $channel = new Channel;
  413. $channel->id = app('snowflake')->id();
  414. $channel->name = $request->get('name');
  415. $channel->owner_uid = $user['user_uid'];
  416. $channel->type = $request->get('type');
  417. $channel->lang = $request->get('lang');
  418. $channel->editor_id = $user['user_id'];
  419. $channel->create_time = time()*1000;
  420. $channel->modify_time = time()*1000;
  421. $channel->save();
  422. return $this->ok($channel);
  423. }
  424. }else{
  425. return $this->error(__('auth.failed'));
  426. }
  427. }else{
  428. return $this->error(__('auth.failed'));
  429. }
  430. }
  431. /**
  432. * Display the specified resource.
  433. *
  434. * @param int $id
  435. * @return \Illuminate\Http\Response
  436. */
  437. public function show($id)
  438. {
  439. //
  440. $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
  441. $channel = Channel::where("uid",$id)->select($indexCol)->first();
  442. $userinfo = new \UserInfo();
  443. $studio = $userinfo->getName($channel->owner_uid);
  444. $channel->owner_info = $studio;
  445. $channel->studio = [
  446. 'id'=>$channel->owner_uid,
  447. 'nickName'=>$studio['nickname'],
  448. 'studioName'=>$studio['username'],
  449. 'avastar'=>'',
  450. 'owner' => [
  451. 'id'=>$channel->owner_uid,
  452. 'nickName'=>$studio['nickname'],
  453. 'userName'=>$studio['username'],
  454. 'avastar'=>'',
  455. ]
  456. ];
  457. return $this->ok($channel);
  458. }
  459. /**
  460. * Update the specified resource in storage.
  461. *
  462. * @param \Illuminate\Http\Request $request
  463. * @param \App\Models\Channel $channel
  464. * @return \Illuminate\Http\Response
  465. */
  466. public function update(Request $request, Channel $channel)
  467. {
  468. //鉴权
  469. $user = AuthApi::current($request);
  470. if(!$user){
  471. return $this->error(__('auth.failed'),[],401);
  472. }
  473. if($channel->owner_uid !== $user["user_uid"]){
  474. //判断是否为协作
  475. $power = ShareApi::getResPower($user["user_uid"],$request->get('id'));
  476. if($power < 30){
  477. return $this->error(__('auth.failed'),[],403);
  478. }
  479. }
  480. $channel->name = $request->get('name');
  481. $channel->type = $request->get('type');
  482. $channel->summary = $request->get('summary');
  483. $channel->lang = $request->get('lang');
  484. $channel->status = $request->get('status');
  485. $channel->save();
  486. return $this->ok($channel);
  487. }
  488. /**
  489. * patch the specified resource in storage.
  490. *
  491. * @param \Illuminate\Http\Request $request
  492. * @param \App\Models\Channel $channel
  493. * @return \Illuminate\Http\Response
  494. */
  495. public function patch(Request $request, Channel $channel)
  496. {
  497. //鉴权
  498. $user = AuthApi::current($request);
  499. if(!$user){
  500. return $this->error(__('auth.failed'),[],401);
  501. }
  502. if($channel->owner_uid !== $user["user_uid"]){
  503. //判断是否为协作
  504. $power = ShareApi::getResPower($user["user_uid"],$request->get('id'));
  505. if($power < 30){
  506. return $this->error(__('auth.failed'),[],403);
  507. }
  508. }
  509. if($request->has('name')){$channel->name = $request->get('name');}
  510. if($request->has('type')){$channel->type = $request->get('type');}
  511. if($request->has('summary')){$channel->summary = $request->get('summary');}
  512. if($request->has('lang')){$channel->lang = $request->get('lang');}
  513. if($request->has('status')){$channel->status = $request->get('status');}
  514. if($request->has('config')){$channel->status = $request->get('config');}
  515. $channel->save();
  516. return $this->ok($channel);
  517. }
  518. /**
  519. * Remove the specified resource from storage.
  520. * @param \Illuminate\Http\Request $request
  521. * @param \App\Models\Channel $channel
  522. * @return \Illuminate\Http\Response
  523. */
  524. public function destroy(Request $request,Channel $channel)
  525. {
  526. //
  527. $user = AuthApi::current($request);
  528. if(!$user){
  529. return $this->error(__('auth.failed'));
  530. }
  531. //判断当前用户是否有指定的studio的权限
  532. if($user['user_uid'] !== $channel->owner_uid){
  533. return $this->error(__('auth.failed'));
  534. }
  535. //查询其他资源
  536. if(Sentence::where("channel_uid",$channel->uid)->exists()){
  537. return $this->error("译文有数据无法删除");
  538. }
  539. if(DhammaTerm::where("channal",$channel->uid)->exists()){
  540. return $this->error("术语有数据无法删除");
  541. }
  542. if(WbwBlock::where("channel_uid",$channel->uid)->exists()){
  543. return $this->error("逐词解析有数据无法删除");
  544. }
  545. $delete = 0;
  546. DB::transaction(function() use($channel,$delete){
  547. //TODO 删除相关资源
  548. $delete = $channel->delete();
  549. });
  550. return $this->ok($delete);
  551. }
  552. }