Usercenter.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 = ['test'];
  11. protected $noNeedRight = '*';
  12. public function test(){
  13. $a = [1,4,10,22,66,36,102,45,23,52,35,76,7];
  14. $b = [1,10,7,102];
  15. $c = [10,102];
  16. //dump(array_intersect($a,$b));
  17. $data = array_merge($c,$b);
  18. dump($data);
  19. dump(array_flip(array_flip($data)));
  20. dump(array_flip([]));
  21. }
  22. //同城
  23. public function samecity(){
  24. $gender = input_post('gender','all');
  25. $agemin = input_post('agemin',0);
  26. $agemax = input_post('agemax',100);
  27. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  28. $this->success('success',[]);
  29. }
  30. $map = [
  31. 'user.status' => 1,
  32. 'user.cityname' => $this->auth->cityname,
  33. 'user.id' => ['neq',$this->auth->id],
  34. 'user.longitude' => ['neq',''],
  35. 'user.latitude' => ['neq',''],
  36. ];
  37. if($gender != 'all'){
  38. $map['user.gender'] = $gender;
  39. }
  40. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  41. //dump($map);
  42. $field = [
  43. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  44. ];
  45. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  46. //dump($list);
  47. $list = list_domain_image($list,['avatar']);
  48. foreach($list as $key => $one){
  49. $one['age'] = birthtime_to_age($one['birthday']);
  50. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  51. $list[$key] = $one;
  52. }
  53. $this->success('success',$list);
  54. }
  55. //附近
  56. public function nearuser(){
  57. $gender = input_post('gender','all');
  58. $agemin = input_post('agemin',0);
  59. $agemax = input_post('agemax',100);
  60. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  61. $this->success('success',[]);
  62. }
  63. //经过地图测算和公式推算,经度纬度 0.1即为11公里
  64. $map = [
  65. 'user.status' => 1,
  66. //'user.cityname' => $this->auth->cityname,
  67. 'user.id' => ['neq',$this->auth->id],
  68. 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  69. 'user.latitude' => ['between',[$this->auth->latitude - 0.1,$this->auth->latitude + 0.1]],
  70. ];
  71. if($gender != 'all'){
  72. $map['user.gender'] = $gender;
  73. }
  74. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  75. //dump($map);
  76. $field = [
  77. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  78. ];
  79. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  80. //dump($list);exit;
  81. $list = list_domain_image($list,['avatar']);
  82. foreach($list as $key => $one){
  83. $one['age'] = birthtime_to_age($one['birthday']);
  84. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  85. $list[$key] = $one;
  86. }
  87. $this->success('success',$list);
  88. }
  89. //视频匹配
  90. public function getvideouser(){
  91. $map = [
  92. 'status' =>1,
  93. //'gender' => $this->auth->gender == 1 ? 0 : 1,
  94. //'real_status' => 1,
  95. //打开视频开关的
  96. 'id' => ['neq',$this->auth->id]
  97. ];
  98. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  99. $lists = $this->fliter_user($lists);
  100. $this->success('success',$lists);
  101. }
  102. private function fliter_user($lists){
  103. //预留全部
  104. $result = array_column($lists,'id');
  105. //提取同城的
  106. $citydata = [];
  107. foreach($lists as $key => $val){
  108. if( !empty($this->auth->cityname) && $this->auth->cityname == $val['cityname'] ){
  109. $citydata[] = $val['id'];
  110. }
  111. }
  112. //有标签交集的
  113. $tagdata = [];
  114. foreach($lists as $key => $val){
  115. if( !empty($this->auth->tag_ids) && !empty($val['tag_ids']) ){
  116. $auth_tag_ids = explode(',',$this->auth->tag_ids);
  117. $val_tag_ids = explode(',',$val['tag_ids']);
  118. if(count(array_intersect($auth_tag_ids,$val_tag_ids)) > 0){
  119. $tagdata[] = $val['id'];
  120. }
  121. }
  122. }
  123. //两个都满足
  124. $double_data = [];
  125. if(!empty($citydata) && !empty($tagdata)){
  126. $double_data = array_intersect($citydata,$tagdata);
  127. }
  128. //两种条件合并,去重。空数组合并没影响
  129. $merge_data = array_merge($citydata,$tagdata);
  130. $merge_data = array_flip(array_flip($merge_data));
  131. //最终结果
  132. //双条件数量足够就return
  133. if(count($double_data) >= 1){
  134. //echo __LINE__;
  135. return $double_data;
  136. }
  137. //不够就合并
  138. $result_data = array_merge($double_data,$merge_data);
  139. $result_data = array_flip(array_flip($result_data));
  140. if(count($result_data) >= 1){
  141. //echo __LINE__;
  142. return $result_data;
  143. }
  144. //仍然不够,全合并
  145. $all_data = array_merge($result_data,$result);
  146. $all_data = array_flip(array_flip($all_data));
  147. return $all_data;
  148. }
  149. /**
  150. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  151. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  152. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  153. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  154. * @return float | false | string
  155. */
  156. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  157. if( empty( $point_1 ) || empty( $point_2 ) ){
  158. return false;
  159. }
  160. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  161. $p1_x = $point_1[0];
  162. $p1_y = $point_1[1];
  163. $p2_x = $point_2[0];
  164. $p2_y = $point_2[1];
  165. if(
  166. $p1_x < -180 || $p1_x > 180
  167. || $p2_x < -180 || $p2_x > 180
  168. || $p1_y < -90 || $p1_y > 90
  169. || $p2_y < -90 || $p2_y > 90
  170. ){
  171. return '0公里';
  172. }
  173. // 根据2点各自的坐标,计算2点之间直线距离的公式
  174. $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);
  175. // 是否计算为字符串公里距离
  176. if( !$calc_as_string ){
  177. return (string)round( $distance / 1000 , 1 ) . '公里';
  178. }
  179. // 如果计算为字符串公里距离
  180. if( $distance / 1000 > 1 ){
  181. $k = (string)round( $distance / 1000 , 1 );
  182. $m = (string)$distance % 1000 ;
  183. $distance = "{$k}公里{$m}米";
  184. }
  185. else{
  186. $distance = "{$distance}米";
  187. }
  188. return $distance;
  189. }
  190. //地图api,根据两地坐标,获得两地距离,打卡用的
  191. //type=0直线,type=1开车
  192. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  193. $result = 0;
  194. $apiurl = 'https://restapi.amap.com/v3/distance?';
  195. $param = [
  196. 'key' => '398c424811d1a59beac2f915323d334e',
  197. 'origins' => $start_lon.','.$start_lat,
  198. 'destination' => $end_lon.','.$end_lat,
  199. 'type' => $type,
  200. 'output' => 'json',
  201. ];
  202. $apiurl .= http_build_query($param);
  203. $request_rs = json_decode(curl_get($apiurl),true);
  204. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  205. if(isset($request_rs['results'][0]['distance']))
  206. {
  207. $result = $request_rs['results'][0]['distance'];
  208. }
  209. }
  210. //dump($result);
  211. return $result;
  212. }
  213. public function distance()
  214. {
  215. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  216. dump($a);
  217. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  218. dump($a);
  219. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  220. dump($b);
  221. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  222. dump($b);
  223. }
  224. }