ChannelController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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)->where('owner_uid','<>', $studioId);
  54. }
  55. break;
  56. case 'studio-all':
  57. /**
  58. * studio 的和协作的
  59. */
  60. #获取user所有有权限的channel列表
  61. $user = AuthApi::current($request);
  62. if(!$user){
  63. return $this->error(__('auth.failed'));
  64. }
  65. //判断当前用户是否有指定的studio的权限
  66. if($user['user_uid'] !== \App\Http\Api\StudioApi::getIdByName($request->get('name'))){
  67. return $this->error(__('auth.failed'));
  68. }
  69. $channelById = [];
  70. $channelId = [];
  71. //获取共享channel
  72. $allSharedChannels = ShareApi::getResList($user['user_uid'],2);
  73. foreach ($allSharedChannels as $key => $value) {
  74. # code...
  75. $channelId[] = $value['res_id'];
  76. $channelById[$value['res_id']] = $value;
  77. }
  78. $table = Channel::select($indexCol)
  79. ->whereIn('uid', $channelId)
  80. ->orWhere('owner_uid',$user['user_uid']);
  81. break;
  82. case 'user-edit':
  83. /**
  84. * 某用户有编辑权限的
  85. */
  86. #获取user所有有权限的channel列表
  87. $user = AuthApi::current($request);
  88. if(!$user){
  89. return $this->error(__('auth.failed'));
  90. }
  91. $channelById = [];
  92. $channelId = [];
  93. //获取共享channel
  94. $allSharedChannels = ShareApi::getResList($user['user_uid'],2);
  95. foreach ($allSharedChannels as $key => $value) {
  96. # code...
  97. if($value['power']>=20){
  98. $channelId[] = $value['res_id'];
  99. $channelById[$value['res_id']] = $value;
  100. }
  101. }
  102. $table = Channel::select($indexCol)
  103. ->whereIn('uid', $channelId)
  104. ->orWhere('owner_uid',$user['user_uid']);
  105. break;
  106. case 'user-in-chapter':
  107. #获取user 在某章节 所有有权限的channel列表
  108. $user = AuthApi::current($request);
  109. if($user){
  110. $channelById = [];
  111. $channelId = [];
  112. //获取共享channel
  113. $allSharedChannels = ShareApi::getResList($user['user_uid'],2);
  114. foreach ($allSharedChannels as $key => $value) {
  115. # code...
  116. $channelId[] = $value['res_id'];
  117. $channelById[$value['res_id']] = $value;
  118. }
  119. //获取全网公开channel
  120. $chapter = PaliTextApi::getChapterStartEnd($request->get('book'),$request->get('para'));
  121. $publicChannelsWithContent = Sentence::where('book_id',$request->get('book'))
  122. ->whereBetween('paragraph',$chapter)
  123. ->where('strlen','>',0)
  124. ->where('status',30)
  125. ->groupBy('channel_uid')
  126. ->select('channel_uid')
  127. ->get();
  128. foreach ($publicChannelsWithContent as $key => $value) {
  129. # code...
  130. $value['res_id']=$value->channel_uid;
  131. $value['power'] = 10;
  132. $value['type'] = 2;
  133. if(!isset($channelById[$value['res_id']])){
  134. $channelId[] = $value['res_id'];
  135. $channelById[$value['res_id']] = $value;
  136. }
  137. }
  138. $table = Channel::select($indexCol)
  139. ->whereIn('uid', $channelId)
  140. ->orWhere('owner_uid',$user['user_uid']);
  141. }else{
  142. return $this->error(__('auth.failed'));
  143. }
  144. break;
  145. }
  146. //处理搜索
  147. if(isset($_GET["search"])){
  148. $table = $table->where('title', 'like', $_GET["search"]."%");
  149. }
  150. //获取记录总条数
  151. $count = $table->count();
  152. //处理排序
  153. if(isset($_GET["order"]) && isset($_GET["dir"])){
  154. $table = $table->orderBy($_GET["order"],$_GET["dir"]);
  155. }else{
  156. //默认排序
  157. $table = $table->orderBy('updated_at','desc');
  158. }
  159. //处理分页
  160. if($request->has("limit")){
  161. if($request->has("offset")){
  162. $offset = $request->get("offset");
  163. }else{
  164. $offset = 0;
  165. }
  166. $table = $table->skip($offset)->take($request->get("limit"));
  167. }
  168. //获取数据
  169. $result = $table->get();
  170. //TODO 将下面代码转移到resource
  171. if($result){
  172. if($request->has('progress')){
  173. //获取进度
  174. //获取单句长度
  175. $sentLen = PaliSentence::where('book',$request->get('book'))
  176. ->whereBetween('paragraph',$chapter)
  177. ->orderBy('word_begin')
  178. ->select(['book','paragraph','word_begin','word_end','length'])
  179. ->get();
  180. }
  181. foreach ($result as $key => $value) {
  182. if($request->has('progress')){
  183. //获取进度
  184. $finalTable = Sentence::where('book_id',$request->get('book'))
  185. ->whereBetween('paragraph',$chapter)
  186. ->where('channel_uid',$value->uid)
  187. ->where('strlen','>',0)
  188. ->select(['strlen','book_id','paragraph','word_start','word_end']);
  189. if($finalTable->count()>0){
  190. $finished = $finalTable->get();
  191. $final=[];
  192. foreach ($sentLen as $sent) {
  193. # code...
  194. $first = Arr::first($finished, function ($value, $key) use($sent) {
  195. return ($value->book_id==$sent->book &&
  196. $value->paragraph==$sent->paragraph &&
  197. $value->word_start==$sent->word_begin &&
  198. $value->word_end==$sent->word_end);
  199. });
  200. $final[] = [$sent->length,$first?true:false];
  201. }
  202. $value['final'] = $final;
  203. }
  204. }
  205. //角色
  206. if($value->owner_uid===$user['user_uid']){
  207. $value['role'] = 'owner';
  208. }else{
  209. if(isset($channelById[$value->uid])){
  210. switch ($channelById[$value->uid]['power']) {
  211. case 10:
  212. # code...
  213. $value['role'] = 'member';
  214. break;
  215. case 20:
  216. $value['role'] = 'editor';
  217. break;
  218. case 30:
  219. $value['role'] = 'owner';
  220. break;
  221. default:
  222. # code...
  223. $value['role'] = $channelById[$value->uid]['power'];
  224. break;
  225. }
  226. }
  227. }
  228. # 获取studio信息
  229. $value->studio = \App\Http\Api\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. $publicChannelsWithContent = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  306. ->where('strlen','>',0)
  307. ->where('status',30)
  308. ->groupBy('channel_uid')
  309. ->select('channel_uid')
  310. ->get();
  311. foreach ($publicChannelsWithContent as $key => $value) {
  312. # code...
  313. $value['res_id']=$value->channel_uid;
  314. $value['power'] = 10;
  315. $value['type'] = 2;
  316. if(!isset($channelById[$value['res_id']])){
  317. $channelId[] = $value['res_id'];
  318. $channelById[$value['res_id']] = $value;
  319. }
  320. }
  321. //所有有这些句子译文的channel
  322. $allChannels = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  323. ->where('strlen','>',0)
  324. ->groupBy('channel_uid')
  325. ->select('channel_uid')
  326. ->get();
  327. //所有需要查询的channel
  328. $result = Channel::select(['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'])
  329. ->whereIn('uid', $channelId)
  330. ->orWhere('owner_uid',$user['user_uid'])
  331. ->get();
  332. foreach ($result as $key => $value) {
  333. //角色
  334. if($value->owner_uid===$user['user_uid']){
  335. $value['role'] = 'owner';
  336. }else{
  337. if(isset($channelById[$value->uid])){
  338. switch ($channelById[$value->uid]['power']) {
  339. case 10:
  340. # code...
  341. $value['role'] = 'member';
  342. break;
  343. case 20:
  344. $value['role'] = 'editor';
  345. break;
  346. case 30:
  347. $value['role'] = 'owner';
  348. break;
  349. default:
  350. # code...
  351. $value['role'] = $channelById[$value->uid]['power'];
  352. break;
  353. }
  354. }
  355. }
  356. # 获取studio信息
  357. $result[$key]["studio"] = \App\Http\Api\StudioApi::getById($value->owner_uid);
  358. //获取进度
  359. $currChannelId = $value->uid;
  360. $hasContent = Arr::first($allChannels, function ($value, $key) use($currChannelId) {
  361. return ($value->channel_uid===$currChannelId);
  362. });
  363. if($hasContent && count($query)>0){
  364. $finalTable = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  365. ->where('channel_uid',$currChannelId)
  366. ->where('strlen','>',0)
  367. ->select(['strlen','book_id','paragraph','word_start','word_end']);
  368. if($finalTable->count()>0){
  369. $finished = $finalTable->get();
  370. $currChannel = [];
  371. foreach ($finished as $rowFinish) {
  372. $currChannel["{$rowFinish->book_id}-{$rowFinish->paragraph}-{$rowFinish->word_start}-{$rowFinish->word_end}"] = 1;
  373. }
  374. $final=[];
  375. foreach ($sentContainer as $sentId=>$rowSent) {
  376. # code...
  377. if(isset($currChannel[$sentId])){
  378. $final[] = [$sentLenContainer[$sentId],true];
  379. }else{
  380. $final[] = [$sentLenContainer[$sentId],false];
  381. }
  382. }
  383. $result[$key]['final'] = $final;
  384. }
  385. }
  386. }
  387. return $this->ok(["rows"=>$result,count($result)]);
  388. }
  389. /**
  390. * Store a newly created resource in storage.
  391. *
  392. * @param \Illuminate\Http\Request $request
  393. * @return \Illuminate\Http\Response
  394. */
  395. public function store(Request $request)
  396. {
  397. //
  398. $user = AuthApi::current($request);
  399. if($user){
  400. //判断当前用户是否有指定的studio的权限
  401. if($user['user_uid'] === StudioApi::getIdByName($request->get('studio'))){
  402. //查询是否重复
  403. if(Channel::where('name',$request->get('name'))->where('owner_uid',$user['user_uid'])->exists()){
  404. return $this->error(__('validation.exists',['name']));
  405. }else{
  406. $channel = new Channel;
  407. $channel->id = app('snowflake')->id();
  408. $channel->name = $request->get('name');
  409. $channel->owner_uid = $user['user_uid'];
  410. $channel->type = $request->get('type');
  411. $channel->lang = $request->get('lang');
  412. $channel->editor_id = $user['user_id'];
  413. $channel->create_time = time()*1000;
  414. $channel->modify_time = time()*1000;
  415. $channel->save();
  416. return $this->ok($channel);
  417. }
  418. }else{
  419. return $this->error(__('auth.failed'));
  420. }
  421. }else{
  422. return $this->error(__('auth.failed'));
  423. }
  424. }
  425. /**
  426. * Display the specified resource.
  427. *
  428. * @param int $id
  429. * @return \Illuminate\Http\Response
  430. */
  431. public function show($id)
  432. {
  433. //
  434. $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
  435. $channel = Channel::where("uid",$id)->select($indexCol)->first();
  436. $userinfo = new \UserInfo();
  437. $studio = $userinfo->getName($channel->owner_uid);
  438. $channel->owner_info = $studio;
  439. $channel->studio = [
  440. 'id'=>$channel->owner_uid,
  441. 'nickName'=>$studio['nickname'],
  442. 'studioName'=>$studio['username'],
  443. 'avastar'=>'',
  444. 'owner' => [
  445. 'id'=>$channel->owner_uid,
  446. 'nickName'=>$studio['nickname'],
  447. 'userName'=>$studio['username'],
  448. 'avastar'=>'',
  449. ]
  450. ];
  451. return $this->ok($channel);
  452. }
  453. /**
  454. * Update the specified resource in storage.
  455. *
  456. * @param \Illuminate\Http\Request $request
  457. * @param \App\Models\Channel $channel
  458. * @return \Illuminate\Http\Response
  459. */
  460. public function update(Request $request, Channel $channel)
  461. {
  462. //鉴权
  463. $user = AuthApi::current($request);
  464. if($user && $channel->owner_uid === $user["user_uid"]){
  465. $channel->name = $request->get('name');
  466. $channel->type = $request->get('type');
  467. $channel->summary = $request->get('summary');
  468. $channel->lang = $request->get('lang');
  469. $channel->status = $request->get('status');
  470. $channel->save();
  471. return $this->ok($channel);
  472. }else{
  473. //非所有者鉴权失败
  474. //TODO 判断是否为协作
  475. return $this->error(__('auth.failed'));
  476. }
  477. }
  478. /**
  479. * Remove the specified resource from storage.
  480. * @param \Illuminate\Http\Request $request
  481. * @param \App\Models\Channel $channel
  482. * @return \Illuminate\Http\Response
  483. */
  484. public function destroy(Request $request,Channel $channel)
  485. {
  486. //
  487. $user = AuthApi::current($request);
  488. if(!$user){
  489. return $this->error(__('auth.failed'));
  490. }
  491. //判断当前用户是否有指定的studio的权限
  492. if($user['user_uid'] !== $channel->owner_uid){
  493. return $this->error(__('auth.failed'));
  494. }
  495. //查询其他资源
  496. if(Sentence::where("channel_uid",$channel->uid)->exists()){
  497. return $this->error("译文有数据无法删除");
  498. }
  499. if(DhammaTerm::where("channal",$channel->uid)->exists()){
  500. return $this->error("术语有数据无法删除");
  501. }
  502. if(WbwBlock::where("channel_uid",$channel->uid)->exists()){
  503. return $this->error("逐词解析有数据无法删除");
  504. }
  505. $delete = 0;
  506. DB::transaction(function() use($channel,$delete){
  507. //TODO 删除相关资源
  508. $delete = $channel->delete();
  509. });
  510. return $this->ok($delete);
  511. }
  512. }