HomeController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. $res = $this->homeService->user_detail($request->user_id);
  85. }catch (AuthException $exception){
  86. return $this->response->errorUnauthorized($exception->getMessage());
  87. }catch (\Exception $exception){
  88. return $this->response->errorForbidden($exception->getMessage());
  89. }
  90. return response()->json($res);
  91. }
  92. /**
  93. * 用户动态
  94. * @param Request $request
  95. * @return \Illuminate\Http\JsonResponse|void
  96. */
  97. public function user_dynamic(Request $request){
  98. try {
  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'] =$request->user_id;
  104. $dynamicService = new DynamicService();
  105. $res = $dynamicService->dynamic_list($where);
  106. }catch (\Exception $exception){
  107. return $this->response->errorForbidden($exception->getMessage());
  108. }
  109. return response()->json($res);
  110. }
  111. /**
  112. * 用户喜欢
  113. */
  114. public function do_like(Request $request){
  115. DB::beginTransaction();
  116. try {
  117. $user = auth('api')->user();
  118. $UserLikeParam = new UserLikeParam();
  119. $UserLikeParam->user_id = $user->id;
  120. $UserLikeParam->like_id = $request->user_id;
  121. $is_like = $this->homeService->do_like($UserLikeParam);
  122. DB::commit();
  123. }catch (\Exception $exception){
  124. DB::rollBack();
  125. return $this->response->errorForbidden($exception->getMessage());
  126. }
  127. return response()->json(['message'=>$is_like==1?"已喜欢":"取消喜欢",'is_like'=>$is_like]);
  128. }
  129. /**
  130. * 用户评价
  131. * @param Request $request
  132. * @return \Illuminate\Http\JsonResponse|void
  133. */
  134. public function do_comment(Request $request){
  135. try {
  136. $user = auth('api')->user();
  137. $UserComment = new UserCommentParam();
  138. $UserComment->user_id = $user->id;
  139. $UserComment->comment_id = $request->user_id;
  140. $UserComment->con1 = $request->con1;
  141. $UserComment->con2 = $request->con2;
  142. $UserComment->con3 = $request->con3;
  143. $UserComment->con4 = $request->con4;
  144. $is_like = $this->homeService->do_comment($UserComment);
  145. }catch (\Exception $exception){
  146. return $this->response->errorForbidden($exception->getMessage());
  147. }
  148. return response()->json(['message'=>'评价成功']);
  149. }
  150. /**
  151. * 解锁微信
  152. * @param Request $request
  153. * @return \Illuminate\Http\JsonResponse|void
  154. */
  155. public function get_weixin(Request $request){
  156. try {
  157. $res = $this->homeService->get_weixin($request->user_id);
  158. }catch (\Exception $exception){
  159. return $this->response->errorForbidden($exception->getMessage());
  160. }
  161. return response()->json(['weixin'=>$res]);
  162. }
  163. /**
  164. * 阅后即焚
  165. */
  166. public function photo_destroy(Request $request){
  167. try {
  168. if(empty($request->url)){
  169. throw new Exception("参数错误");
  170. }
  171. $user = auth('api')->user();
  172. $cache_eq_id = $request->header('uniqueID','');//获取设备码
  173. $lock = Cache::lock(ApiEnum::PHOTO_DESTROY_LOCK.$cache_eq_id, 1);
  174. if ($lock->get()) {
  175. if(!$user){
  176. if(!Redis::get(ApiEnum::PHOTO_DESTROY_URL.$cache_eq_id.md5($request->url))){
  177. Redis::setex(ApiEnum::PHOTO_DESTROY_URL.$cache_eq_id.md5($request->url),86400*365,$request->url);
  178. }
  179. }else{
  180. if(!UserPhotoDestroy::query()->where(['user_id'=>$user->id,'url'=>$request->url])->first()){
  181. UserPhotoDestroy::query()->create(['user_id'=>$user->id,'url'=>$request->url,'atime'=>date("Y-m-d H:i:s")]);
  182. }
  183. }
  184. $lock->release();
  185. }else{
  186. throw new Exception("请求太频繁");
  187. }
  188. }catch (\Exception $exception){
  189. return $this->response->errorForbidden($exception->getMessage());
  190. }
  191. return response()->json(['message'=>'已销毁']);
  192. }
  193. }