123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 会员中心
- */
- class Usercenter extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- public function test()
- {
- $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
- dump($a);
- $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
- dump($a);
- $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
- dump($b);
- $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
- dump($b);
- }
- //同城
- public function samecity(){
- $gender = input_post('gender','all');
- $agemin = input_post('agemin',0);
- $agemax = input_post('agemax',100);
- $map = [
- 'user.status' => 1,
- 'user.cityname' => $this->auth->cityname,
- 'user.id' => ['neq',$this->auth->id],
- 'user.longitude' => ['neq',''],
- 'user.latitude' => ['neq',''],
- ];
- if($gender != 'all'){
- $map['user.gender'] = $gender;
- }
- $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
- //dump($map);
- $field = [
- 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
- ];
- $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
- //dump($list);
- $list = list_domain_image($list,['avatar']);
- foreach($list as $key => $one){
- $one['age'] = birthtime_to_age($one['birthday']);
- $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
- $list[$key] = $one;
- }
- $this->success('success',$list);
- }
- //附近
- public function nearuser(){
- $gender = input_post('gender','all');
- $agemin = input_post('agemin',0);
- $agemax = input_post('agemax',100);
- $map = [
- 'user.status' => 1,
- //'user.cityname' => $this->auth->cityname,
- 'user.id' => ['neq',$this->auth->id],
- 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
- 'user.latitude' => ['between',[$this->auth->latitude - 0.01,$this->auth->latitude + 0.01]],
- ];
- if($gender != 'all'){
- $map['user.gender'] = $gender;
- }
- $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
- //dump($map);
- $field = [
- 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
- ];
- $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
- //dump($list);exit;
- $list = list_domain_image($list,['avatar']);
- foreach($list as $key => $one){
- $one['age'] = birthtime_to_age($one['birthday']);
- $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
- $list[$key] = $one;
- }
- $this->success('success',$list);
- }
- /**
- * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
- * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
- * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
- * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
- * @return float | false | string
- */
- public function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
- if( empty( $point_1 ) || empty( $point_2 ) ){
- return false;
- }
- // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
- $p1_x = $point_1[0];
- $p1_y = $point_1[1];
- $p2_x = $point_2[0];
- $p2_y = $point_2[1];
- if(
- $p1_x < -180 || $p1_x > 180
- || $p2_x < -180 || $p2_x > 180
- || $p1_y < -90 || $p1_y > 90
- || $p2_y < -90 || $p2_y > 90
- ){
- return '0公里';
- }
- // 根据2点各自的坐标,计算2点之间直线距离的公式
- $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);
- // 是否计算为字符串公里距离
- if( !$calc_as_string ){
- return (string)round( $distance / 1000 , 1 ) . '公里';
- }
- // 如果计算为字符串公里距离
- if( $distance / 1000 > 1 ){
- $k = (string)round( $distance / 1000 , 1 );
- $m = (string)$distance % 1000 ;
- $distance = "{$k}公里{$m}米";
- }
- else{
- $distance = "{$distance}米";
- }
- return $distance;
- }
- //地图api,根据两地坐标,获得两地距离,打卡用的
- //type=0直线,type=1开车
- public function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
- $result = 0;
- $apiurl = 'https://restapi.amap.com/v3/distance?';
- $param = [
- 'key' => '398c424811d1a59beac2f915323d334e',
- 'origins' => $start_lon.','.$start_lat,
- 'destination' => $end_lon.','.$end_lat,
- 'type' => $type,
- 'output' => 'json',
- ];
- $apiurl .= http_build_query($param);
- $request_rs = json_decode(curl_get($apiurl),true);
- if(isset($request_rs['status']) && $request_rs['status'] == 1){
- if(isset($request_rs['results'][0]['distance']))
- {
- $result = $request_rs['results'][0]['distance'];
- }
- }
- //dump($result);
- return $result;
- }
- }
|