DynamicController.php 7.3 KB

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