DynamicController.php 7.7 KB

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