Usercenter.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 会员中心
  7. */
  8. class Usercenter extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. public function test()
  13. {
  14. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  15. dump($a);
  16. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  17. dump($a);
  18. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  19. dump($b);
  20. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  21. dump($b);
  22. }
  23. //同城
  24. public function samecity(){
  25. $gender = input_post('gender','all');
  26. $agemin = input_post('agemin',0);
  27. $agemax = input_post('agemax',100);
  28. $map = [
  29. 'user.status' => 1,
  30. 'user.cityname' => $this->auth->cityname,
  31. 'user.id' => ['neq',$this->auth->id],
  32. 'user.longitude' => ['neq',''],
  33. 'user.latitude' => ['neq',''],
  34. ];
  35. if($gender != 'all'){
  36. $map['user.gender'] = $gender;
  37. }
  38. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  39. //dump($map);
  40. $field = [
  41. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  42. ];
  43. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  44. //dump($list);
  45. $list = list_domain_image($list,['avatar']);
  46. foreach($list as $key => $one){
  47. $one['age'] = birthtime_to_age($one['birthday']);
  48. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  49. $list[$key] = $one;
  50. }
  51. $this->success('success',$list);
  52. }
  53. //附近
  54. public function nearuser(){
  55. $gender = input_post('gender','all');
  56. $agemin = input_post('agemin',0);
  57. $agemax = input_post('agemax',100);
  58. $map = [
  59. 'user.status' => 1,
  60. //'user.cityname' => $this->auth->cityname,
  61. 'user.id' => ['neq',$this->auth->id],
  62. 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  63. 'user.latitude' => ['between',[$this->auth->latitude - 0.01,$this->auth->latitude + 0.01]],
  64. ];
  65. if($gender != 'all'){
  66. $map['user.gender'] = $gender;
  67. }
  68. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  69. //dump($map);
  70. $field = [
  71. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  72. ];
  73. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  74. //dump($list);exit;
  75. $list = list_domain_image($list,['avatar']);
  76. foreach($list as $key => $one){
  77. $one['age'] = birthtime_to_age($one['birthday']);
  78. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  79. $list[$key] = $one;
  80. }
  81. $this->success('success',$list);
  82. }
  83. /**
  84. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  85. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  86. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  87. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  88. * @return float | false | string
  89. */
  90. public function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  91. if( empty( $point_1 ) || empty( $point_2 ) ){
  92. return false;
  93. }
  94. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  95. $p1_x = $point_1[0];
  96. $p1_y = $point_1[1];
  97. $p2_x = $point_2[0];
  98. $p2_y = $point_2[1];
  99. if(
  100. $p1_x < -180 || $p1_x > 180
  101. || $p2_x < -180 || $p2_x > 180
  102. || $p1_y < -90 || $p1_y > 90
  103. || $p2_y < -90 || $p2_y > 90
  104. ){
  105. return '0公里';
  106. }
  107. // 根据2点各自的坐标,计算2点之间直线距离的公式
  108. $distance = round(6378.138*2*asin(sqrt(pow(sin(( $p1_x *pi()/180-$p2_x*pi()/180)/2),2)+cos( $p1_x *pi()/180)*cos($p2_x*pi()/180)* pow(sin(( $p1_y *pi()/180-$p2_y*pi()/180)/2),2)))*1000);
  109. // 是否计算为字符串公里距离
  110. if( !$calc_as_string ){
  111. return (string)round( $distance / 1000 , 1 ) . '公里';
  112. }
  113. // 如果计算为字符串公里距离
  114. if( $distance / 1000 > 1 ){
  115. $k = (string)round( $distance / 1000 , 1 );
  116. $m = (string)$distance % 1000 ;
  117. $distance = "{$k}公里{$m}米";
  118. }
  119. else{
  120. $distance = "{$distance}米";
  121. }
  122. return $distance;
  123. }
  124. //地图api,根据两地坐标,获得两地距离,打卡用的
  125. //type=0直线,type=1开车
  126. public function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  127. $result = 0;
  128. $apiurl = 'https://restapi.amap.com/v3/distance?';
  129. $param = [
  130. 'key' => '398c424811d1a59beac2f915323d334e',
  131. 'origins' => $start_lon.','.$start_lat,
  132. 'destination' => $end_lon.','.$end_lat,
  133. 'type' => $type,
  134. 'output' => 'json',
  135. ];
  136. $apiurl .= http_build_query($param);
  137. $request_rs = json_decode(curl_get($apiurl),true);
  138. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  139. if(isset($request_rs['results'][0]['distance']))
  140. {
  141. $result = $request_rs['results'][0]['distance'];
  142. }
  143. }
  144. //dump($result);
  145. return $result;
  146. }
  147. }