ChannelController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. $channelById = [];
  116. $channelId = [];
  117. //获取共享channel
  118. $allSharedChannels = ShareApi::getResList($user['user_uid'],2);
  119. foreach ($allSharedChannels as $key => $value) {
  120. # code...
  121. $channelId[] = $value['res_id'];
  122. $channelById[$value['res_id']] = $value;
  123. }
  124. //获取全网公开channel
  125. $chapter = PaliTextApi::getChapterStartEnd($request->get('book'),$request->get('para'));
  126. $publicChannelsWithContent = Sentence::where('book_id',$request->get('book'))
  127. ->whereBetween('paragraph',$chapter)
  128. ->where('strlen','>',0)
  129. ->where('status',30)
  130. ->groupBy('channel_uid')
  131. ->select('channel_uid')
  132. ->get();
  133. foreach ($publicChannelsWithContent as $key => $value) {
  134. # code...
  135. $value['res_id']=$value->channel_uid;
  136. $value['power'] = 10;
  137. $value['type'] = 2;
  138. if(!isset($channelById[$value['res_id']])){
  139. $channelId[] = $value['res_id'];
  140. $channelById[$value['res_id']] = $value;
  141. }
  142. }
  143. $table = Channel::select($indexCol)
  144. ->whereIn('uid', $channelId)
  145. ->orWhere('owner_uid',$user['user_uid']);
  146. }else{
  147. return $this->error(__('auth.failed'));
  148. }
  149. break;
  150. }
  151. //处理搜索
  152. if(isset($_GET["search"])){
  153. $table = $table->where('title', 'like', $_GET["search"]."%");
  154. }
  155. //获取记录总条数
  156. $count = $table->count();
  157. //处理排序
  158. if(isset($_GET["order"]) && isset($_GET["dir"])){
  159. $table = $table->orderBy($_GET["order"],$_GET["dir"]);
  160. }else{
  161. //默认排序
  162. $table = $table->orderBy('updated_at','desc');
  163. }
  164. //处理分页
  165. if($request->has("limit")){
  166. if($request->has("offset")){
  167. $offset = $request->get("offset");
  168. }else{
  169. $offset = 0;
  170. }
  171. $table = $table->skip($offset)->take($request->get("limit"));
  172. }
  173. //获取数据
  174. $result = $table->get();
  175. //TODO 将下面代码转移到resource
  176. if($result){
  177. if($request->has('progress')){
  178. //获取进度
  179. //获取单句长度
  180. $sentLen = PaliSentence::where('book',$request->get('book'))
  181. ->whereBetween('paragraph',$chapter)
  182. ->orderBy('word_begin')
  183. ->select(['book','paragraph','word_begin','word_end','length'])
  184. ->get();
  185. }
  186. foreach ($result as $key => $value) {
  187. if($request->has('progress')){
  188. //获取进度
  189. $finalTable = Sentence::where('book_id',$request->get('book'))
  190. ->whereBetween('paragraph',$chapter)
  191. ->where('channel_uid',$value->uid)
  192. ->where('strlen','>',0)
  193. ->select(['strlen','book_id','paragraph','word_start','word_end']);
  194. if($finalTable->count()>0){
  195. $finished = $finalTable->get();
  196. $final=[];
  197. foreach ($sentLen as $sent) {
  198. # code...
  199. $first = Arr::first($finished, function ($value, $key) use($sent) {
  200. return ($value->book_id==$sent->book &&
  201. $value->paragraph==$sent->paragraph &&
  202. $value->word_start==$sent->word_begin &&
  203. $value->word_end==$sent->word_end);
  204. });
  205. $final[] = [$sent->length,$first?true:false];
  206. }
  207. $value['final'] = $final;
  208. }
  209. }
  210. //角色
  211. if($value->owner_uid===$user['user_uid']){
  212. $value['role'] = 'owner';
  213. }else{
  214. if(isset($channelById[$value->uid])){
  215. switch ($channelById[$value->uid]['power']) {
  216. case 10:
  217. # code...
  218. $value['role'] = 'member';
  219. break;
  220. case 20:
  221. $value['role'] = 'editor';
  222. break;
  223. case 30:
  224. $value['role'] = 'owner';
  225. break;
  226. default:
  227. # code...
  228. $value['role'] = $channelById[$value->uid]['power'];
  229. break;
  230. }
  231. }
  232. }
  233. # 获取studio信息
  234. $value->studio = \App\Http\Api\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. $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. //所有有这些句子译文的channel
  327. $allChannels = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$query)
  328. ->where('strlen','>',0)
  329. ->groupBy('channel_uid')
  330. ->select('channel_uid')
  331. ->get();
  332. //所有需要查询的channel
  333. $result = Channel::select(['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'])
  334. ->whereIn('uid', $channelId)
  335. ->orWhere('owner_uid',$user['user_uid'])
  336. ->get();
  337. foreach ($result as $key => $value) {
  338. //角色
  339. if($value->owner_uid===$user['user_uid']){
  340. $value['role'] = 'owner';
  341. }else{
  342. if(isset($channelById[$value->uid])){
  343. switch ($channelById[$value->uid]['power']) {
  344. case 10:
  345. # code...
  346. $value['role'] = 'member';
  347. break;
  348. case 20:
  349. $value['role'] = 'editor';
  350. break;
  351. case 30:
  352. $value['role'] = 'owner';
  353. break;
  354. default:
  355. # code...
  356. $value['role'] = $channelById[$value->uid]['power'];
  357. break;
  358. }
  359. }
  360. }
  361. # 获取studio信息
  362. $result[$key]["studio"] = \App\Http\Api\StudioApi::getById($value->owner_uid);
  363. //获取进度
  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. return $this->ok(["rows"=>$result,count($result)]);
  393. }
  394. /**
  395. * Store a newly created resource in storage.
  396. *
  397. * @param \Illuminate\Http\Request $request
  398. * @return \Illuminate\Http\Response
  399. */
  400. public function store(Request $request)
  401. {
  402. //
  403. $user = AuthApi::current($request);
  404. if($user){
  405. //判断当前用户是否有指定的studio的权限
  406. if($user['user_uid'] === StudioApi::getIdByName($request->get('studio'))){
  407. //查询是否重复
  408. if(Channel::where('name',$request->get('name'))->where('owner_uid',$user['user_uid'])->exists()){
  409. return $this->error(__('validation.exists',['name']));
  410. }else{
  411. $channel = new Channel;
  412. $channel->id = app('snowflake')->id();
  413. $channel->name = $request->get('name');
  414. $channel->owner_uid = $user['user_uid'];
  415. $channel->type = $request->get('type');
  416. $channel->lang = $request->get('lang');
  417. $channel->editor_id = $user['user_id'];
  418. $channel->create_time = time()*1000;
  419. $channel->modify_time = time()*1000;
  420. $channel->save();
  421. return $this->ok($channel);
  422. }
  423. }else{
  424. return $this->error(__('auth.failed'));
  425. }
  426. }else{
  427. return $this->error(__('auth.failed'));
  428. }
  429. }
  430. /**
  431. * Display the specified resource.
  432. *
  433. * @param int $id
  434. * @return \Illuminate\Http\Response
  435. */
  436. public function show($id)
  437. {
  438. //
  439. $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
  440. $channel = Channel::where("uid",$id)->select($indexCol)->first();
  441. $userinfo = new \UserInfo();
  442. $studio = $userinfo->getName($channel->owner_uid);
  443. $channel->owner_info = $studio;
  444. $channel->studio = [
  445. 'id'=>$channel->owner_uid,
  446. 'nickName'=>$studio['nickname'],
  447. 'studioName'=>$studio['username'],
  448. 'avastar'=>'',
  449. 'owner' => [
  450. 'id'=>$channel->owner_uid,
  451. 'nickName'=>$studio['nickname'],
  452. 'userName'=>$studio['username'],
  453. 'avastar'=>'',
  454. ]
  455. ];
  456. return $this->ok($channel);
  457. }
  458. /**
  459. * Update the specified resource in storage.
  460. *
  461. * @param \Illuminate\Http\Request $request
  462. * @param \App\Models\Channel $channel
  463. * @return \Illuminate\Http\Response
  464. */
  465. public function update(Request $request, Channel $channel)
  466. {
  467. //鉴权
  468. $user = AuthApi::current($request);
  469. if($user && $channel->owner_uid === $user["user_uid"]){
  470. $channel->name = $request->get('name');
  471. $channel->type = $request->get('type');
  472. $channel->summary = $request->get('summary');
  473. $channel->lang = $request->get('lang');
  474. $channel->status = $request->get('status');
  475. $channel->save();
  476. return $this->ok($channel);
  477. }else{
  478. //非所有者鉴权失败
  479. //TODO 判断是否为协作
  480. return $this->error(__('auth.failed'));
  481. }
  482. }
  483. /**
  484. * Remove the specified resource from storage.
  485. * @param \Illuminate\Http\Request $request
  486. * @param \App\Models\Channel $channel
  487. * @return \Illuminate\Http\Response
  488. */
  489. public function destroy(Request $request,Channel $channel)
  490. {
  491. //
  492. $user = AuthApi::current($request);
  493. if(!$user){
  494. return $this->error(__('auth.failed'));
  495. }
  496. //判断当前用户是否有指定的studio的权限
  497. if($user['user_uid'] !== $channel->owner_uid){
  498. return $this->error(__('auth.failed'));
  499. }
  500. //查询其他资源
  501. if(Sentence::where("channel_uid",$channel->uid)->exists()){
  502. return $this->error("译文有数据无法删除");
  503. }
  504. if(DhammaTerm::where("channal",$channel->uid)->exists()){
  505. return $this->error("术语有数据无法删除");
  506. }
  507. if(WbwBlock::where("channel_uid",$channel->uid)->exists()){
  508. return $this->error("逐词解析有数据无法删除");
  509. }
  510. $delete = 0;
  511. DB::transaction(function() use($channel,$delete){
  512. //TODO 删除相关资源
  513. $delete = $channel->delete();
  514. });
  515. return $this->ok($delete);
  516. }
  517. }