HomeController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Enums\ApiEnum;
  4. use App\Exceptions\AuthException;
  5. use App\Exceptions\VipException;
  6. use App\Http\Params\UserCommentParam;
  7. use App\Http\Params\UserLikeParam;
  8. use App\Models\UserPhotoDestroy;
  9. use App\Services\DynamicService;
  10. use App\Services\HomeService;
  11. use App\Services\RedisService;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\Redis;
  16. use PHPUnit\Util\Exception;
  17. class HomeController extends Controller
  18. {
  19. public $homeService;
  20. public function __construct()
  21. {
  22. $this->homeService = new HomeService();
  23. }
  24. /**
  25. * 获取位置信息,经纬度
  26. */
  27. public function get_location(Request $request){
  28. try {
  29. if(!isset($request->latitude) || $request->latitude==""){
  30. throw new Exception("参数错误");
  31. }
  32. if(!isset($request->longitude) || $request->longitude==""){
  33. throw new Exception("参数错误");
  34. }
  35. // if(!isset($request->uniqueID) || $request->uniqueID==""){
  36. // throw new Exception("参数错误");
  37. // }
  38. $user = auth('api')->user();
  39. if(!$user){
  40. RedisService::redis()->SETEX('user_location_' . $request->uniqueID, 600, json_encode(['latitude' => $request->latitude, 'longitude' => $request->longitude]));
  41. }else{
  42. $user->latitude = $request->latitude;
  43. $user->longitude = $request->longitude;
  44. $user->save();
  45. }
  46. }catch (\Exception $exception){
  47. return $this->response->errorForbidden($exception->getMessage());
  48. }
  49. return response()->json(['message'=>"请求成功"]);
  50. }
  51. /**
  52. * 返回首页信息
  53. * @param Request $request
  54. * @return \Illuminate\Http\JsonResponse|void
  55. */
  56. public function home(Request $request){
  57. try {
  58. $param['keyword'] = $request->post('keyword','');//关键词,昵称,标签
  59. $param['nearby'] = $request->post('nearby',1);//附近
  60. $param['online'] = $request->post('online',1);//在线
  61. $param['new'] = $request->post('new',1);//新人
  62. $param['latitude'] = $request->post('latitude');//纬度
  63. $param['longitude'] = $request->post('longitude');//经度
  64. $user = auth('api')->user();
  65. if($user){
  66. $user->latitude = $request->latitude;
  67. $user->longitude = $request->longitude;
  68. $user->save();
  69. }else{
  70. RedisService::redis()->SETEX('user_location_' . $request->uniqueID, 600, json_encode(['latitude' => $request->latitude, 'longitude' => $request->longitude]));
  71. }
  72. $res = $this->homeService->get_list($param);
  73. }catch (\Exception $exception){
  74. return $this->response->errorForbidden($exception->getMessage());
  75. }
  76. return response()->json($res);
  77. }
  78. /**
  79. * 用户详情
  80. * @param Request $request
  81. * @return \Illuminate\Http\JsonResponse|void
  82. */
  83. public function user_detail(Request $request){
  84. try {
  85. $param['user_id'] = $request->user_id;
  86. $param['tencent_im_user_id'] = $request->tencent_im_user_id;
  87. $res = $this->homeService->user_detail($param);
  88. }catch (VipException $exception){
  89. return response()->json(['message'=>$exception->getMessage()])->setStatusCode(410);
  90. }catch (AuthException $exception){
  91. return $this->response->errorUnauthorized($exception->getMessage());
  92. }catch (\Exception $exception){
  93. return $this->response->errorForbidden($exception->getMessage());
  94. }
  95. return response()->json($res);
  96. }
  97. /**
  98. * 用户动态
  99. * @param Request $request
  100. * @return \Illuminate\Http\JsonResponse|void
  101. */
  102. public function user_dynamic(Request $request){
  103. try {
  104. $where = array();
  105. $where['type'] = $request->post('type',1); //类型 type 1全部 2关注 3附近
  106. $where['look_type'] =$request->post('look_type',3);//查看类型 type 1只看男士 2只看女士 3全部
  107. $where['tag_id'] =$request->post('tag_id',0); //话题标签
  108. $where['user_id'] =$request->user_id;
  109. $dynamicService = new DynamicService();
  110. $res = $dynamicService->dynamic_list($where);
  111. }catch (\Exception $exception){
  112. return $this->response->errorForbidden($exception->getMessage());
  113. }
  114. return response()->json($res);
  115. }
  116. /**
  117. * 用户喜欢
  118. */
  119. public function do_like(Request $request){
  120. DB::beginTransaction();
  121. try {
  122. $user = auth('api')->user();
  123. $UserLikeParam = new UserLikeParam();
  124. $UserLikeParam->user_id = $user->id;
  125. $UserLikeParam->like_id = $request->user_id;
  126. $is_like = $this->homeService->do_like($UserLikeParam);
  127. DB::commit();
  128. }catch (\Exception $exception){
  129. DB::rollBack();
  130. return $this->response->errorForbidden($exception->getMessage());
  131. }
  132. return response()->json(['message'=>$is_like==1?"已喜欢":"取消喜欢",'is_like'=>$is_like]);
  133. }
  134. /**
  135. * 用户评价
  136. * @param Request $request
  137. * @return \Illuminate\Http\JsonResponse|void
  138. */
  139. public function do_comment(Request $request){
  140. try {
  141. $user = auth('api')->user();
  142. $UserComment = new UserCommentParam();
  143. $UserComment->user_id = $user->id;
  144. $UserComment->comment_id = $request->user_id;
  145. $UserComment->con1 = $request->con1;
  146. $UserComment->con2 = $request->con2;
  147. $UserComment->con3 = $request->con3;
  148. $UserComment->con4 = $request->con4;
  149. $is_like = $this->homeService->do_comment($UserComment);
  150. }catch (\Exception $exception){
  151. return $this->response->errorForbidden($exception->getMessage());
  152. }
  153. return response()->json(['message'=>'评价成功']);
  154. }
  155. /**
  156. * 解锁微信
  157. * @param Request $request
  158. * @return \Illuminate\Http\JsonResponse|void
  159. */
  160. public function get_weixin(Request $request){
  161. try {
  162. $res = $this->homeService->get_weixin($request->user_id);
  163. }catch (VipException $exception){
  164. return response()->json(['message'=>$exception->getMessage()])->setStatusCode(410);
  165. }catch (\Exception $exception){
  166. return $this->response->errorForbidden($exception->getMessage());
  167. }
  168. return response()->json(['weixin'=>$res]);
  169. }
  170. /**
  171. * 阅后即焚
  172. */
  173. public function photo_destroy(Request $request){
  174. try {
  175. if(empty($request->url)){
  176. throw new Exception("参数错误");
  177. }
  178. $user = auth('api')->user();
  179. $cache_eq_id = $request->header('uniqueID','');//获取设备码
  180. $lock = Cache::lock(ApiEnum::PHOTO_DESTROY_LOCK.$cache_eq_id, 1);
  181. if ($lock->get()) {
  182. if(!$user){
  183. if(!Redis::get(ApiEnum::PHOTO_DESTROY_URL.$cache_eq_id.md5($request->url))){
  184. Redis::setex(ApiEnum::PHOTO_DESTROY_URL.$cache_eq_id.md5($request->url),86400*365,$request->url);
  185. }
  186. }else{
  187. if(!UserPhotoDestroy::query()->where(['user_id'=>$user->id,'url'=>$request->url])->first()){
  188. UserPhotoDestroy::query()->create(['user_id'=>$user->id,'url'=>$request->url,'atime'=>date("Y-m-d H:i:s")]);
  189. }
  190. }
  191. $lock->release();
  192. }else{
  193. throw new Exception("请求太频繁");
  194. }
  195. }catch (\Exception $exception){
  196. return $this->response->errorForbidden($exception->getMessage());
  197. }
  198. return response()->json(['message'=>'已销毁']);
  199. }
  200. //拉黑用户
  201. public function lahei(Request $request){
  202. try {
  203. $param['user_id'] = $request->user_id;
  204. $param['tencent_im_user_id'] = $request->tencent_im_user_id;
  205. $this->homeService->lahei($param);
  206. }catch (\Exception $exception){
  207. return $this->response->errorForbidden($exception->getMessage());
  208. }
  209. return response()->json(['message'=>'已拉黑']);
  210. }
  211. }