DynamicController.php 7.0 KB

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