DynamicController.php 7.3 KB

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