ChannelController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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\PaliSentence;
  7. use App\Http\Controllers\AuthController;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Http\Api\AuthApi;
  11. use App\Http\Api\StudioApi;
  12. use App\Http\Api\ShareApi;
  13. use App\Http\Api\PaliTextApi;
  14. use Illuminate\Support\Arr;
  15. class ChannelController extends Controller
  16. {
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return \Illuminate\Http\Response
  21. */
  22. public function index(Request $request)
  23. {
  24. //
  25. $userinfo = new \UserInfo();
  26. $result=false;
  27. $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
  28. switch ($request->get('view')) {
  29. case 'studio':
  30. # 获取studio内所有channel
  31. $user = AuthApi::current($request);
  32. if($user){
  33. //判断当前用户是否有指定的studio的权限
  34. if($user['user_uid'] === \App\Http\Api\StudioApi::getIdByName($request->get('name'))){
  35. $table = Channel::select($indexCol)->where('owner_uid', $user["user_uid"]);
  36. }else{
  37. return $this->error(__('auth.failed'));
  38. }
  39. }else{
  40. return $this->error(__('auth.failed'));
  41. }
  42. break;
  43. case 'user-edit':
  44. /**
  45. * 某用户有编辑权限的
  46. */
  47. #获取user所有有权限的channel列表
  48. $user = AuthApi::current($request);
  49. if(!$user){
  50. return $this->error(__('auth.failed'));
  51. }
  52. $channelById = [];
  53. $channelId = [];
  54. //获取共享channel
  55. $allSharedChannels = ShareApi::getResList($user['user_uid'],2);
  56. foreach ($allSharedChannels as $key => $value) {
  57. # code...
  58. if($value['power']>=20){
  59. $channelId[] = $value['res_id'];
  60. $channelById[$value['res_id']] = $value;
  61. }
  62. }
  63. $table = Channel::select($indexCol)
  64. ->whereIn('uid', $channelId)
  65. ->orWhere('owner_uid',$user['user_uid']);
  66. break;
  67. case 'user-in-chapter':
  68. #获取user 在某章节 所有有权限的channel列表
  69. $user = AuthApi::current($request);
  70. if($user){
  71. $channelById = [];
  72. $channelId = [];
  73. //获取共享channel
  74. $allSharedChannels = ShareApi::getResList($user['user_uid'],2);
  75. foreach ($allSharedChannels as $key => $value) {
  76. # code...
  77. $channelId[] = $value['res_id'];
  78. $channelById[$value['res_id']] = $value;
  79. }
  80. //获取全网公开channel
  81. $chapter = PaliTextApi::getChapterStartEnd($request->get('book'),$request->get('para'));
  82. $publicChannelsWithContent = Sentence::where('book_id',$request->get('book'))
  83. ->whereBetween('paragraph',$chapter)
  84. ->where('strlen','>',0)
  85. ->where('status',30)
  86. ->groupBy('channel_uid')
  87. ->select('channel_uid')
  88. ->get();
  89. foreach ($publicChannelsWithContent as $key => $value) {
  90. # code...
  91. $value['res_id']=$value->channel_uid;
  92. $value['power'] = 10;
  93. $value['type'] = 2;
  94. if(!isset($channelById[$value['res_id']])){
  95. $channelId[] = $value['res_id'];
  96. $channelById[$value['res_id']] = $value;
  97. }
  98. }
  99. $table = Channel::select($indexCol)
  100. ->whereIn('uid', $channelId)
  101. ->orWhere('owner_uid',$user['user_uid']);
  102. }else{
  103. return $this->error(__('auth.failed'));
  104. }
  105. break;
  106. }
  107. //处理搜索
  108. if(isset($_GET["search"])){
  109. $table = $table->where('title', 'like', $_GET["search"]."%");
  110. }
  111. //获取记录总条数
  112. $count = $table->count();
  113. //处理排序
  114. if(isset($_GET["order"]) && isset($_GET["dir"])){
  115. $table = $table->orderBy($_GET["order"],$_GET["dir"]);
  116. }else{
  117. //默认排序
  118. $table = $table->orderBy('updated_at','desc');
  119. }
  120. //处理分页
  121. if($request->has("limit")){
  122. if($request->has("offset")){
  123. $offset = $request->get("offset");
  124. }else{
  125. $offset = 0;
  126. }
  127. $table = $table->skip($offset)->take($request->get("limit"));
  128. }
  129. //获取数据
  130. $result = $table->get();
  131. //TODO 将下面代码转移到resource
  132. if($result){
  133. if($request->has('progress')){
  134. //获取进度
  135. //获取单句长度
  136. $sentLen = PaliSentence::where('book',$request->get('book'))
  137. ->whereBetween('paragraph',$chapter)
  138. ->orderBy('word_begin')
  139. ->select(['book','paragraph','word_begin','word_end','length'])
  140. ->get();
  141. }
  142. foreach ($result as $key => $value) {
  143. if($request->has('progress')){
  144. //获取进度
  145. $finalTable = Sentence::where('book_id',$request->get('book'))
  146. ->whereBetween('paragraph',$chapter)
  147. ->where('channel_uid',$value->uid)
  148. ->where('strlen','>',0)
  149. ->select(['strlen','book_id','paragraph','word_start','word_end']);
  150. if($finalTable->count()>0){
  151. $finished = $finalTable->get();
  152. $final=[];
  153. foreach ($sentLen as $sent) {
  154. # code...
  155. $first = Arr::first($finished, function ($value, $key) use($sent) {
  156. return ($value->book_id==$sent->book &&
  157. $value->paragraph==$sent->paragraph &&
  158. $value->word_start==$sent->word_begin &&
  159. $value->word_end==$sent->word_end);
  160. });
  161. $final[] = [$sent->length,$first?true:false];
  162. }
  163. $value['final'] = $final;
  164. }
  165. }
  166. if($value->owner_uid===$user['user_uid']){
  167. $value['role'] = 'owner';
  168. }else{
  169. if(isset($channelById[$value->uid])){
  170. switch ($channelById[$value->uid]['power']) {
  171. case 10:
  172. # code...
  173. $value['role'] = 'member';
  174. break;
  175. case 20:
  176. $value['role'] = 'editor';
  177. break;
  178. case 30:
  179. $value['role'] = 'owner';
  180. break;
  181. default:
  182. # code...
  183. $value['role'] = $channelById[$value->uid]['power'];
  184. break;
  185. }
  186. }
  187. }
  188. # 获取studio信息
  189. $studio = $userinfo->getName($value->owner_uid);
  190. $value->studio = [
  191. 'id'=>$value->owner_uid,
  192. 'nickName'=>$studio['nickname'],
  193. 'studioName'=>$studio['username'],
  194. 'avatar'=>'',
  195. 'owner' => [
  196. 'id'=>$value->owner_uid,
  197. 'nickName'=>$studio['nickname'],
  198. 'userName'=>$studio['username'],
  199. 'avatar'=>'',
  200. ]
  201. ];
  202. }
  203. return $this->ok(["rows"=>$result,"count"=>$count]);
  204. }else{
  205. return $this->error("没有查询到数据");
  206. }
  207. }
  208. /**
  209. * Store a newly created resource in storage.
  210. *
  211. * @param \Illuminate\Http\Request $request
  212. * @return \Illuminate\Http\Response
  213. */
  214. public function store(Request $request)
  215. {
  216. //
  217. $user = AuthApi::current($request);
  218. if($user){
  219. //判断当前用户是否有指定的studio的权限
  220. if($user['user_uid'] === StudioApi::getIdByName($request->get('studio'))){
  221. //查询是否重复
  222. if(Channel::where('name',$request->get('name'))->where('owner_uid',$user['user_uid'])->exists()){
  223. return $this->error(__('validation.exists',['name']));
  224. }else{
  225. $channel = new Channel;
  226. $channel->id = app('snowflake')->id();
  227. $channel->name = $request->get('name');
  228. $channel->owner_uid = $user['user_uid'];
  229. $channel->type = $request->get('type');
  230. $channel->lang = $request->get('lang');
  231. $channel->editor_id = $user['user_id'];
  232. $channel->create_time = time()*1000;
  233. $channel->modify_time = time()*1000;
  234. $channel->save();
  235. return $this->ok($channel);
  236. }
  237. }else{
  238. return $this->error(__('auth.failed'));
  239. }
  240. }else{
  241. return $this->error(__('auth.failed'));
  242. }
  243. }
  244. /**
  245. * Display the specified resource.
  246. *
  247. * @param int $id
  248. * @return \Illuminate\Http\Response
  249. */
  250. public function show($id)
  251. {
  252. //
  253. $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
  254. $channel = Channel::where("uid",$id)->select($indexCol)->first();
  255. $userinfo = new \UserInfo();
  256. $studio = $userinfo->getName($channel->owner_uid);
  257. $channel->owner_info = $studio;
  258. $channel->studio = [
  259. 'id'=>$channel->owner_uid,
  260. 'nickName'=>$studio['nickname'],
  261. 'studioName'=>$studio['username'],
  262. 'avastar'=>'',
  263. 'owner' => [
  264. 'id'=>$channel->owner_uid,
  265. 'nickName'=>$studio['nickname'],
  266. 'userName'=>$studio['username'],
  267. 'avastar'=>'',
  268. ]
  269. ];
  270. return $this->ok($channel);
  271. }
  272. /**
  273. * Update the specified resource in storage.
  274. *
  275. * @param \Illuminate\Http\Request $request
  276. * @param \App\Models\Channel $channel
  277. * @return \Illuminate\Http\Response
  278. */
  279. public function update(Request $request, Channel $channel)
  280. {
  281. //鉴权
  282. $user = AuthApi::current($request);
  283. if($user && $channel->owner_uid === $user["user_uid"]){
  284. $channel->name = $request->get('name');
  285. $channel->type = $request->get('type');
  286. $channel->summary = $request->get('summary');
  287. $channel->lang = $request->get('lang');
  288. $channel->status = $request->get('status');
  289. $channel->save();
  290. return $this->ok($channel);
  291. }else{
  292. //非所有者鉴权失败
  293. //TODO 判断是否为协作
  294. return $this->error(__('auth.failed'));
  295. }
  296. }
  297. /**
  298. * Remove the specified resource from storage.
  299. *
  300. * @param \App\Models\Channel $channel
  301. * @return \Illuminate\Http\Response
  302. */
  303. public function destroy(Channel $channel)
  304. {
  305. //
  306. }
  307. }