ChannelController.php 11 KB

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