ChannelController.php 22 KB

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