DynamicController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Params\DynamicParam;
  4. use App\Http\Params\DynamicZanParam;
  5. use App\Http\Params\UserReportParam;
  6. use App\Models\Banner;
  7. use App\Services\DynamicService;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Validator;
  11. class DynamicController extends Controller
  12. {
  13. protected $dynamicService;
  14. public function __construct(DynamicService $dynamicService)
  15. {
  16. $this->dynamicService = $dynamicService;
  17. }
  18. /**
  19. * 获取话题列表
  20. */
  21. public function get_tag_list(Request $request){
  22. try {
  23. $data = $this->dynamicService->tag_list($request->keyword);
  24. }catch (\Exception $exception){
  25. return $this->response->errorForbidden($exception->getMessage());
  26. }
  27. return response()->json($data);
  28. }
  29. /**
  30. * 发布动态
  31. */
  32. public function release(Request $request){
  33. $validator = Validator::make($request->all(), [
  34. 'content' => 'required',
  35. ], [
  36. 'content.required'=>"内容必须",
  37. ]);
  38. if ($validator->fails()) {
  39. return $this->response()->errorForbidden($validator->messages()->first());
  40. }
  41. DB::beginTransaction();
  42. try {
  43. $user = auth('api')->user();
  44. $dynamicParam = new DynamicParam();
  45. $dynamicParam->user_id = $user->id;
  46. $dynamicParam->content = htmlspecialchars($request->post('content'));
  47. $dynamicParam->img_url = $request->post('img_url');
  48. $dynamicParam->type = $request->post('type');
  49. $dynamicParam->status = 1;
  50. $dynamicParam->zan_num = 0;
  51. $dynamicParam->site = $request->post('site');
  52. $dynamicParam->tag = $request->post('tag');
  53. $this->dynamicService->release($dynamicParam);
  54. DB::commit();
  55. }catch (\Exception $exception){
  56. DB::rollBack();
  57. return $this->response->errorForbidden($exception->getMessage());
  58. }
  59. return $this->response->noContent();
  60. }
  61. /**
  62. * 动态banner,话题,风险提示
  63. */
  64. public function dynamic_info(){
  65. try {
  66. $oss_config = config("filesystems.disks.oss");
  67. $banner = Banner::where(["site"=>1,"status"=>1])->orderBy("sort",'asc')->limit(3)->get(['id','img_url']);
  68. foreach ($banner as $k=>$v){
  69. $banner[$k]['img_url'] = "https://".$oss_config['bucket'].'.'.$oss_config['endpoint'].'/'.$v['img_url'];
  70. }
  71. $res['banner'] = $banner;
  72. $res['tag'] = DB::table("dynamic_tag")->orderBy('hot','desc')->limit(10)->get();
  73. }catch (\Exception $exception){
  74. return $this->response->errorForbidden($exception->getMessage());
  75. }
  76. return response()->json($res);
  77. }
  78. /**
  79. * 动态列表
  80. */
  81. public function get_list(Request $request){
  82. $where = array();
  83. $where['type'] = $request->post('type',1); //类型 type 1全部 2关注 3附近
  84. $where['look_type'] =$request->post('look_type',3);//查看类型 type 1只看男士 2只看女士 3全部
  85. $where['tag_id'] =$request->post('tag_id',0); //话题标签
  86. $where['user_id'] =0;
  87. try {
  88. $list = $this->dynamicService->dynamic_list($where);
  89. }catch (\Exception $exception){
  90. return $this->response->errorForbidden($exception->getMessage());
  91. }
  92. return response()->json($list);
  93. }
  94. /**
  95. * 我的动态
  96. */
  97. public function my_list(Request $request){
  98. $user = auth('api')->user();
  99. $where = array();
  100. $where['type'] = $request->post('type',1); //类型 type 1全部 2关注 3附近
  101. $where['look_type'] =$request->post('look_type',3);//查看类型 type 1只看男士 2只看女士 3全部
  102. $where['tag_id'] =$request->post('tag_id',0); //话题标签
  103. $where['user_id'] =$user->id;
  104. try {
  105. $list = $this->dynamicService->dynamic_list($where);
  106. }catch (\Exception $exception){
  107. return $this->response->errorForbidden($exception->getMessage());
  108. }
  109. return response()->json($list);
  110. }
  111. /**
  112. * 动态点赞
  113. */
  114. public function zan(Request $request){
  115. DB::beginTransaction();
  116. try {
  117. $user = auth('api')->user();
  118. $dynamic_zan_param = new DynamicZanParam();
  119. $dynamic_zan_param->user_id = $user->id;
  120. $dynamic_zan_param->dynamic_id = $request->id;
  121. $this->dynamicService->zan($dynamic_zan_param);
  122. DB::commit();
  123. }catch (\Exception $exception){
  124. DB::rollBack();
  125. return $this->response->errorForbidden($exception->getMessage());
  126. }
  127. return $this->response->noContent();
  128. }
  129. /**
  130. * 删除动态
  131. */
  132. public function del(Request $request){
  133. try {
  134. $DynamicParam = new DynamicParam();
  135. $DynamicParam->id = $request->id;
  136. $this->dynamicService->del($DynamicParam);
  137. }catch (\Exception $exception){
  138. return $this->response->errorForbidden($exception->getMessage());
  139. }
  140. return $this->response->noContent();
  141. }
  142. /**
  143. * 举报动态
  144. */
  145. public function report(Request $request){
  146. try {
  147. $user = auth('api')->user();
  148. $UserReportParam = new UserReportParam();
  149. $UserReportParam->type = 2;
  150. $UserReportParam->user_id = $user->id;
  151. $UserReportParam->content = $request->post('content');
  152. $UserReportParam->report_id = $request->post('id');
  153. $UserReportParam->status = 0;
  154. $UserReportParam->img_url = $request->post('img_url');
  155. $UserReportParam->info = $request->post('info',"");
  156. $this->dynamicService->report($UserReportParam);
  157. }catch (\Exception $exception){
  158. return $this->response->errorForbidden($exception->getMessage());
  159. }
  160. return $this->response->noContent();
  161. }
  162. }