DynamicController.php 6.6 KB

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