HomeController.php 9.1 KB

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