DynamicController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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->tag = $request->post('tag');
  52. $dynamicParam->city = $request->post('city');
  53. $dynamicParam->latitude = $request->post('latitude');
  54. $dynamicParam->longitude = $request->post('longitude');
  55. $this->dynamicService->release($dynamicParam);
  56. DB::commit();
  57. }catch (\Exception $exception){
  58. DB::rollBack();
  59. return $this->response->errorForbidden($exception->getMessage());
  60. }
  61. return response()->json(['message'=>"发布成功"]);
  62. }
  63. /**
  64. * 动态banner,话题,风险提示
  65. */
  66. public function dynamic_info(){
  67. try {
  68. $oss_config = config("filesystems.disks.oss");
  69. $banner = Banner::where(["site"=>1,"status"=>1])->orderBy("sort",'asc')->limit(3)->get(['id','img_url']);
  70. foreach ($banner as $k=>$v){
  71. $banner[$k]['img_url'] = "https://".$oss_config['bucket'].'.'.$oss_config['endpoint'].'/'.$v['img_url'];
  72. }
  73. $res['banner'] = $banner;
  74. $res['tag'] = DB::table("dynamic_tag")->orderBy('hot','desc')->limit(10)->get();
  75. $res['fx'] = "这是风险提示";
  76. }catch (\Exception $exception){
  77. return $this->response->errorForbidden($exception->getMessage());
  78. }
  79. return response()->json($res);
  80. }
  81. /**
  82. * 动态列表
  83. */
  84. public function get_list(Request $request){
  85. $where = array();
  86. $where['type'] = $request->post('type',1); //类型 type 1全部 2关注 3附近
  87. $where['look_type'] =$request->post('look_type',3);//查看类型 type 1只看男士 2只看女士 3全部
  88. $where['tag_id'] =$request->post('tag_id',0); //话题标签
  89. $where['user_id'] =0;
  90. try {
  91. $list = $this->dynamicService->dynamic_list($where);
  92. }catch (\Exception $exception){
  93. return $this->response->errorForbidden($exception->getMessage());
  94. }
  95. return response()->json($list);
  96. }
  97. /**
  98. * 我的动态
  99. */
  100. public function my_list(Request $request){
  101. $user = auth('api')->user();
  102. $where = array();
  103. $where['type'] = $request->post('type',1); //类型 type 1全部 2关注 3附近
  104. $where['look_type'] =$request->post('look_type',3);//查看类型 type 1只看男士 2只看女士 3全部
  105. $where['tag_id'] =$request->post('tag_id',0); //话题标签
  106. $where['user_id'] =$user->id;
  107. try {
  108. $list = $this->dynamicService->dynamic_list($where);
  109. }catch (\Exception $exception){
  110. return $this->response->errorForbidden($exception->getMessage());
  111. }
  112. return response()->json($list);
  113. }
  114. /**
  115. * 动态点赞
  116. */
  117. public function zan(Request $request){
  118. DB::beginTransaction();
  119. try {
  120. $user = auth('api')->user();
  121. $dynamic_zan_param = new DynamicZanParam();
  122. $dynamic_zan_param->user_id = $user->id;
  123. $dynamic_zan_param->dynamic_id = $request->id;
  124. $zan = $this->dynamicService->zan($dynamic_zan_param);
  125. DB::commit();
  126. }catch (\Exception $exception){
  127. DB::rollBack();
  128. return $this->response->errorForbidden($exception->getMessage());
  129. }
  130. return response()->json(['message'=>$zan==1?"点赞成功":"取消点赞",'zan'=>$zan]);
  131. }
  132. /**
  133. * 删除动态
  134. */
  135. public function del(Request $request){
  136. try {
  137. $DynamicParam = new DynamicParam();
  138. $DynamicParam->id = $request->id;
  139. $this->dynamicService->del($DynamicParam);
  140. }catch (\Exception $exception){
  141. return $this->response->errorForbidden($exception->getMessage());
  142. }
  143. return response()->json(['message'=>"删除成功"]);
  144. }
  145. /**
  146. * 举报动态
  147. */
  148. public function report(Request $request){
  149. try {
  150. $user = auth('api')->user();
  151. $UserReportParam = new UserReportParam();
  152. $UserReportParam->type = 2;
  153. $UserReportParam->user_id = $user->id;
  154. $UserReportParam->content = $request->post('content');
  155. $UserReportParam->report_id = $request->post('id');
  156. $UserReportParam->status = 0;
  157. $UserReportParam->img_url = $request->post('img_url');
  158. $UserReportParam->info = $request->post('info',"");
  159. $this->dynamicService->report($UserReportParam);
  160. }catch (\Exception $exception){
  161. return $this->response->errorForbidden($exception->getMessage());
  162. }
  163. return response()->json(['message'=>"举报成功"]);
  164. }
  165. }