DynamicController.php 6.9 KB

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