Usercenter.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. }
  56. /**
  57. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  58. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  59. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  60. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  61. * @return float | false | string
  62. */
  63. public function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  64. if( empty( $point_1 ) || empty( $point_2 ) ){
  65. return false;
  66. }
  67. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  68. $p1_x = $point_1[0];
  69. $p1_y = $point_1[1];
  70. $p2_x = $point_2[0];
  71. $p2_y = $point_2[1];
  72. if(
  73. $p1_x < -180 || $p1_x > 180
  74. || $p2_x < -180 || $p2_x > 180
  75. || $p1_y < -90 || $p1_y > 90
  76. || $p2_y < -90 || $p2_y > 90
  77. ){
  78. return '0公里';
  79. }
  80. // 根据2点各自的坐标,计算2点之间直线距离的公式
  81. $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);
  82. // 是否计算为字符串公里距离
  83. if( !$calc_as_string ){
  84. return (string)round( $distance / 1000 , 1 ) . '公里';
  85. }
  86. // 如果计算为字符串公里距离
  87. if( $distance / 1000 > 1 ){
  88. $k = (string)round( $distance / 1000 , 1 );
  89. $m = (string)$distance % 1000 ;
  90. $distance = "{$k}公里{$m}米";
  91. }
  92. else{
  93. $distance = "{$distance}米";
  94. }
  95. return $distance;
  96. }
  97. //地图api,根据两地坐标,获得两地距离,打卡用的
  98. //type=0直线,type=1开车
  99. public function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  100. $result = 0;
  101. $apiurl = 'https://restapi.amap.com/v3/distance?';
  102. $param = [
  103. 'key' => '398c424811d1a59beac2f915323d334e',
  104. 'origins' => $start_lon.','.$start_lat,
  105. 'destination' => $end_lon.','.$end_lat,
  106. 'type' => $type,
  107. 'output' => 'json',
  108. ];
  109. $apiurl .= http_build_query($param);
  110. $request_rs = json_decode(curl_get($apiurl),true);
  111. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  112. if(isset($request_rs['results'][0]['distance']))
  113. {
  114. $result = $request_rs['results'][0]['distance'];
  115. }
  116. }
  117. //dump($result);
  118. return $result;
  119. }
  120. }