HomeController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. $user = auth('api')->user();
  56. if($user){
  57. $user_info['sex'] = $user->sex;
  58. $user_info['password'] = $user->password;
  59. }else{
  60. $user_info = [];
  61. }
  62. return response()->json(['data'=>$res,'setting'=>$set,'user_info'=>$user_info]);
  63. }
  64. /**
  65. * 返回首页信息
  66. * @param Request $request
  67. * @return \Illuminate\Http\JsonResponse|void
  68. */
  69. public function home(Request $request){
  70. try {
  71. $param['keyword'] = $request->post('keyword','');//关键词,昵称,标签
  72. $param['nearby'] = $request->post('nearby',1);//附近
  73. $param['online'] = $request->post('online',1);//在线
  74. $param['new'] = $request->post('new',1);//新人
  75. $param['latitude'] = $request->post('latitude');//纬度
  76. $param['longitude'] = $request->post('longitude');//经度
  77. $user = auth('api')->user();
  78. if($user){
  79. $user->latitude = $request->latitude;
  80. $user->longitude = $request->longitude;
  81. $user->save();
  82. }else{
  83. RedisService::redis()->SETEX('user_location_' . $request->uniqueID, 600, json_encode(['latitude' => $request->latitude, 'longitude' => $request->longitude]));
  84. }
  85. $res = $this->homeService->get_list($param);
  86. }catch (\Exception $exception){
  87. return $this->response->errorForbidden($exception->getMessage());
  88. }
  89. return response()->json($res);
  90. }
  91. /**
  92. * 用户详情
  93. * @param Request $request
  94. * @return \Illuminate\Http\JsonResponse|void
  95. */
  96. public function user_detail(Request $request){
  97. try {
  98. $param['user_id'] = $request->user_id;
  99. $param['tencent_im_user_id'] = $request->tencent_im_user_id;
  100. $res = $this->homeService->user_detail($param);
  101. }catch (VipException $exception){
  102. return response()->json(['message'=>$exception->getMessage()])->setStatusCode(410);
  103. }catch (AuthException $exception){
  104. return $this->response->errorUnauthorized($exception->getMessage());
  105. }catch (\Exception $exception){
  106. return $this->response->errorForbidden($exception->getMessage());
  107. }
  108. return response()->json($res);
  109. }
  110. /**
  111. * 用户动态
  112. * @param Request $request
  113. * @return \Illuminate\Http\JsonResponse|void
  114. */
  115. public function user_dynamic(Request $request){
  116. try {
  117. $where = array();
  118. $where['type'] = $request->post('type',1); //类型 type 1全部 2关注 3附近
  119. $where['look_type'] =$request->post('look_type',3);//查看类型 type 1只看男士 2只看女士 3全部
  120. $where['tag_id'] =$request->post('tag_id',0); //话题标签
  121. $where['user_id'] =$request->user_id;
  122. $dynamicService = new DynamicService();
  123. $res = $dynamicService->dynamic_list($where);
  124. }catch (\Exception $exception){
  125. return $this->response->errorForbidden($exception->getMessage());
  126. }
  127. return response()->json($res);
  128. }
  129. /**
  130. * 用户喜欢
  131. */
  132. public function do_like(Request $request){
  133. DB::beginTransaction();
  134. try {
  135. $user = auth('api')->user();
  136. $UserLikeParam = new UserLikeParam();
  137. $UserLikeParam->user_id = $user->id;
  138. $UserLikeParam->like_id = $request->user_id;
  139. $is_like = $this->homeService->do_like($UserLikeParam);
  140. DB::commit();
  141. }catch (\Exception $exception){
  142. DB::rollBack();
  143. return $this->response->errorForbidden($exception->getMessage());
  144. }
  145. return response()->json(['message'=>$is_like==1?"已喜欢":"取消喜欢",'is_like'=>$is_like]);
  146. }
  147. /**
  148. * 用户评价
  149. * @param Request $request
  150. * @return \Illuminate\Http\JsonResponse|void
  151. */
  152. public function do_comment(Request $request){
  153. try {
  154. $user = auth('api')->user();
  155. $UserComment = new UserCommentParam();
  156. $UserComment->user_id = $user->id;
  157. $UserComment->comment_id = $request->user_id;
  158. $UserComment->con1 = $request->con1;
  159. $UserComment->con2 = $request->con2;
  160. $UserComment->con3 = $request->con3;
  161. $UserComment->con4 = $request->con4;
  162. $is_like = $this->homeService->do_comment($UserComment);
  163. }catch (\Exception $exception){
  164. return $this->response->errorForbidden($exception->getMessage());
  165. }
  166. return response()->json(['message'=>'评价成功']);
  167. }
  168. /**
  169. * 解锁微信
  170. * @param Request $request
  171. * @return \Illuminate\Http\JsonResponse|void
  172. */
  173. public function get_weixin(Request $request){
  174. try {
  175. $res = $this->homeService->get_weixin($request->user_id);
  176. }catch (VipException $exception){
  177. return response()->json(['message'=>$exception->getMessage()])->setStatusCode(410);
  178. }catch (\Exception $exception){
  179. return $this->response->errorForbidden($exception->getMessage());
  180. }
  181. return response()->json(['weixin'=>$res]);
  182. }
  183. /**
  184. * 阅后即焚
  185. */
  186. public function photo_destroy(Request $request){
  187. try {
  188. if(empty($request->url)){
  189. throw new Exception("参数错误");
  190. }
  191. $user = auth('api')->user();
  192. $cache_eq_id = $request->header('uniqueID','');//获取设备码
  193. $lock = Cache::lock(ApiEnum::PHOTO_DESTROY_LOCK.$cache_eq_id, 1);
  194. if ($lock->get()) {
  195. if(!$user){
  196. if(!Redis::get(ApiEnum::PHOTO_DESTROY_URL.$cache_eq_id.md5($request->url))){
  197. Redis::setex(ApiEnum::PHOTO_DESTROY_URL.$cache_eq_id.md5($request->url),86400*365,$request->url);
  198. }
  199. }else{
  200. if(!UserPhotoDestroy::query()->where(['user_id'=>$user->id,'url'=>$request->url])->first()){
  201. UserPhotoDestroy::query()->create(['user_id'=>$user->id,'url'=>$request->url,'atime'=>date("Y-m-d H:i:s")]);
  202. }
  203. }
  204. $lock->release();
  205. }else{
  206. throw new Exception("请求太频繁");
  207. }
  208. }catch (\Exception $exception){
  209. return $this->response->errorForbidden($exception->getMessage());
  210. }
  211. return response()->json(['message'=>'已销毁']);
  212. }
  213. //拉黑用户
  214. public function lahei(Request $request){
  215. try {
  216. $param['user_id'] = $request->user_id;
  217. $param['tencent_im_user_id'] = $request->tencent_im_user_id;
  218. $this->homeService->lahei($param);
  219. }catch (\Exception $exception){
  220. return $this->response->errorForbidden($exception->getMessage());
  221. }
  222. return response()->json(['message'=>'已拉黑']);
  223. }
  224. /**
  225. * 用户详情
  226. * @param Request $request
  227. * @return \Illuminate\Http\JsonResponse|void
  228. */
  229. public function user_detail_h5(Request $request){
  230. try {
  231. $param['user_id'] = $request->user_id;
  232. $res = $this->homeService->user_detail_h5($param);
  233. }catch (\Exception $exception){
  234. return $this->response->errorForbidden($exception->getMessage());
  235. }
  236. return response()->json($res);
  237. }
  238. }