DynamicController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 response()->json(['message'=>"发布成功"]);
  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. $res['fx'] = "这是风险提示";
  74. }catch (\Exception $exception){
  75. return $this->response->errorForbidden($exception->getMessage());
  76. }
  77. return response()->json($res);
  78. }
  79. /**
  80. * 动态列表
  81. */
  82. public function get_list(Request $request){
  83. $where = array();
  84. $where['type'] = $request->post('type',1); //类型 type 1全部 2关注 3附近
  85. $where['look_type'] =$request->post('look_type',3);//查看类型 type 1只看男士 2只看女士 3全部
  86. $where['tag_id'] =$request->post('tag_id',0); //话题标签
  87. $where['user_id'] =0;
  88. try {
  89. $list = $this->dynamicService->dynamic_list($where);
  90. }catch (\Exception $exception){
  91. return $this->response->errorForbidden($exception->getMessage());
  92. }
  93. return response()->json($list);
  94. }
  95. /**
  96. * 我的动态
  97. */
  98. public function my_list(Request $request){
  99. $user = auth('api')->user();
  100. $where = array();
  101. $where['type'] = $request->post('type',1); //类型 type 1全部 2关注 3附近
  102. $where['look_type'] =$request->post('look_type',3);//查看类型 type 1只看男士 2只看女士 3全部
  103. $where['tag_id'] =$request->post('tag_id',0); //话题标签
  104. $where['user_id'] =$user->id;
  105. try {
  106. $list = $this->dynamicService->dynamic_list($where);
  107. }catch (\Exception $exception){
  108. return $this->response->errorForbidden($exception->getMessage());
  109. }
  110. return response()->json($list);
  111. }
  112. /**
  113. * 动态点赞
  114. */
  115. public function zan(Request $request){
  116. DB::beginTransaction();
  117. try {
  118. $user = auth('api')->user();
  119. $dynamic_zan_param = new DynamicZanParam();
  120. $dynamic_zan_param->user_id = $user->id;
  121. $dynamic_zan_param->dynamic_id = $request->id;
  122. $this->dynamicService->zan($dynamic_zan_param);
  123. DB::commit();
  124. }catch (\Exception $exception){
  125. DB::rollBack();
  126. return $this->response->errorForbidden($exception->getMessage());
  127. }
  128. return response()->json(['message'=>"点赞成功"]);
  129. }
  130. /**
  131. * 删除动态
  132. */
  133. public function del(Request $request){
  134. try {
  135. $DynamicParam = new DynamicParam();
  136. $DynamicParam->id = $request->id;
  137. $this->dynamicService->del($DynamicParam);
  138. }catch (\Exception $exception){
  139. return $this->response->errorForbidden($exception->getMessage());
  140. }
  141. return response()->json(['message'=>"删除成功"]);
  142. }
  143. /**
  144. * 举报动态
  145. */
  146. public function report(Request $request){
  147. try {
  148. $user = auth('api')->user();
  149. $UserReportParam = new UserReportParam();
  150. $UserReportParam->type = 2;
  151. $UserReportParam->user_id = $user->id;
  152. $UserReportParam->content = $request->post('content');
  153. $UserReportParam->report_id = $request->post('id');
  154. $UserReportParam->status = 0;
  155. $UserReportParam->img_url = $request->post('img_url');
  156. $UserReportParam->info = $request->post('info',"");
  157. $this->dynamicService->report($UserReportParam);
  158. }catch (\Exception $exception){
  159. return $this->response->errorForbidden($exception->getMessage());
  160. }
  161. return response()->json(['message'=>"举报成功"]);
  162. }
  163. }