2
0

ChannelController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Http\Controllers;
  3. require_once __DIR__.'/../../../public/app/ucenter/function.php';
  4. use App\Models\Channel;
  5. use App\Http\Controllers\AuthController;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Log;
  8. use App\Http\Api;
  9. class ChannelController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index(Request $request)
  17. {
  18. //
  19. $userinfo = new \UserInfo();
  20. $result=false;
  21. $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
  22. switch ($request->get('view')) {
  23. case 'studio':
  24. # 获取studio内所有channel
  25. $user = \App\Http\Api\AuthApi::current($request);
  26. if($user){
  27. //判断当前用户是否有指定的studio的权限
  28. if($user['user_uid'] === \App\Http\Api\StudioApi::getIdByName($request->get('name'))){
  29. $table = Channel::select($indexCol)->where('owner_uid', $user["user_uid"]);
  30. }else{
  31. return $this->error(__('auth.failed'));
  32. }
  33. }else{
  34. return $this->error(__('auth.failed'));
  35. }
  36. break;
  37. }
  38. //处理搜索
  39. if(isset($_GET["search"])){
  40. $table = $table->where('title', 'like', $_GET["search"]."%");
  41. }
  42. //获取记录总条数
  43. $count = $table->count();
  44. //处理排序
  45. if(isset($_GET["order"]) && isset($_GET["dir"])){
  46. $table = $table->orderBy($_GET["order"],$_GET["dir"]);
  47. }else{
  48. //默认排序
  49. $table = $table->orderBy('updated_at','desc');
  50. }
  51. //处理分页
  52. if($request->has("limit")){
  53. if($request->has("offset")){
  54. $offset = $request->get("offset");
  55. }else{
  56. $offset = 0;
  57. }
  58. $table = $table->skip($offset)->take($request->get("limit"));
  59. }
  60. //获取数据
  61. $result = $table->get();
  62. if($result){
  63. foreach ($result as $key => $value) {
  64. # 获取studio信息
  65. $studio = $userinfo->getName($value->owner_uid);
  66. $value->studio = [
  67. 'id'=>$value->owner_uid,
  68. 'nickName'=>$studio['nickname'],
  69. 'studioName'=>$studio['username'],
  70. 'avastar'=>'',
  71. 'owner' => [
  72. 'id'=>$value->owner_uid,
  73. 'nickName'=>$studio['nickname'],
  74. 'userName'=>$studio['username'],
  75. 'avastar'=>'',
  76. ]
  77. ];
  78. }
  79. return $this->ok(["rows"=>$result,"count"=>$count]);
  80. }else{
  81. return $this->error("没有查询到数据");
  82. }
  83. }
  84. /**
  85. * Store a newly created resource in storage.
  86. *
  87. * @param \Illuminate\Http\Request $request
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function store(Request $request)
  91. {
  92. //
  93. $user = \App\Http\Api\AuthApi::current($request);
  94. if($user){
  95. //判断当前用户是否有指定的studio的权限
  96. if($user['user_uid'] === \App\Http\Api\StudioApi::getIdByName($request->get('studio'))){
  97. //查询是否重复
  98. if(Channel::where('name',$request->get('name'))->where('owner_uid',$user['user_uid'])->exists()){
  99. return $this->error(__('validation.exists',['name']));
  100. }else{
  101. $channel = new Channel;
  102. $channel->id = app('snowflake')->id();
  103. $channel->name = $request->get('name');
  104. $channel->owner_uid = $user['user_uid'];
  105. $channel->type = $request->get('type');
  106. $channel->lang = $request->get('lang');
  107. $channel->editor_id = $user['user_id'];
  108. $channel->create_time = time()*1000;
  109. $channel->modify_time = time()*1000;
  110. $channel->save();
  111. return $this->ok($channel);
  112. }
  113. }else{
  114. return $this->error(__('auth.failed'));
  115. }
  116. }else{
  117. return $this->error(__('auth.failed'));
  118. }
  119. }
  120. /**
  121. * Display the specified resource.
  122. *
  123. * @param int $id
  124. * @return \Illuminate\Http\Response
  125. */
  126. public function show($id)
  127. {
  128. //
  129. $indexCol = ['uid','name','summary','type','owner_uid','lang','status','updated_at','created_at'];
  130. $channel = Channel::where("uid",$id)->select($indexCol)->first();
  131. $userinfo = new \UserInfo();
  132. $studio = $userinfo->getName($channel->owner_uid);
  133. $channel->owner_info = $studio;
  134. $channel->studio = [
  135. 'id'=>$channel->owner_uid,
  136. 'nickName'=>$studio['nickname'],
  137. 'studioName'=>$studio['username'],
  138. 'avastar'=>'',
  139. 'owner' => [
  140. 'id'=>$channel->owner_uid,
  141. 'nickName'=>$studio['nickname'],
  142. 'userName'=>$studio['username'],
  143. 'avastar'=>'',
  144. ]
  145. ];
  146. return $this->ok($channel);
  147. }
  148. /**
  149. * Update the specified resource in storage.
  150. *
  151. * @param \Illuminate\Http\Request $request
  152. * @param \App\Models\Channel $channel
  153. * @return \Illuminate\Http\Response
  154. */
  155. public function update(Request $request, Channel $channel)
  156. {
  157. //鉴权
  158. $user = AuthApi::current($request);
  159. if($user && $channel->owner_uid === $user["user_uid"]){
  160. $channel->name = $request->get('name');
  161. $channel->type = $request->get('type');
  162. $channel->summary = $request->get('summary');
  163. $channel->lang = $request->get('lang');
  164. $channel->status = $request->get('status');
  165. $channel->save();
  166. return $this->ok($channel);
  167. }else{
  168. //非所有者鉴权失败
  169. //TODO 判断是否为协作
  170. return $this->error(__('auth.failed'));
  171. }
  172. }
  173. /**
  174. * Remove the specified resource from storage.
  175. *
  176. * @param \App\Models\Channel $channel
  177. * @return \Illuminate\Http\Response
  178. */
  179. public function destroy(Channel $channel)
  180. {
  181. //
  182. }
  183. }