HomeController.php 8.0 KB

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