DynamicController.php 6.5 KB

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