Usercenter.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\model\wallet;
  6. use Redis;
  7. /**
  8. * 会员中心,不是个人中心
  9. */
  10. class Usercenter extends Api
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = '*';
  14. protected $allowFields = [
  15. 'id',
  16. 'username',
  17. 'nickname',
  18. // 'truename',
  19. 'mobile',
  20. 'avatar',
  21. // 'real_status',
  22. 'gender',
  23. 'height',
  24. 'weight',
  25. 'birthday',
  26. 'bio',
  27. 'idcard_status',
  28. 'cityname',
  29. 'photo_images',
  30. 'tag_ids',
  31. 'attribute',
  32. 'shoesize',
  33. 'wechat_account',
  34. ];
  35. //获取他人用户信息,留下足迹
  36. public function getuserinfo(){
  37. $uid = input_post('uid',0);
  38. $userinfo = Db::name('user')->field($this->allowFields)->where('id|username',$uid)->find();
  39. if(!$userinfo){
  40. $this->error('不存在的用户');
  41. }
  42. //用户数据
  43. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  44. $idcard_confirm = Db::name('user_idconfirm')->where('user_id',$uid)->find();
  45. $new_data = [
  46. 'age' => birthtime_to_age($userinfo['birthday']),
  47. 'truename' => ($userinfo['idcard_status'] == 1 && isset($idcard_confirm['truename'])) ? $idcard_confirm['truename'] : '',
  48. ];
  49. //合并
  50. $userinfo = array_merge($userinfo,$new_data);
  51. //枚举
  52. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name'])->select();
  53. //vip
  54. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  55. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  56. //是否喜欢和关注
  57. $is_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>$uid])->find();
  58. $userinfo['is_follow'] = $is_follow ? 1 : 0;
  59. $is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  60. $userinfo['is_like'] = $is_like ? 1 : 0;
  61. //是否拉黑
  62. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  63. $userinfo['is_black'] = $is_black ? 1 : 0;
  64. //关注人数,粉丝人数
  65. $follow_num = Db::name('user_follow')->where(['uid'=>$this->auth->id])->count('id');
  66. $fans_num = Db::name('user_follow')->where(['follow_uid'=>$this->auth->id])->count('id');
  67. $userinfo['follow_num'] = $follow_num;
  68. $userinfo['fans_num'] = $fans_num;
  69. //查看别人信息,就要留下痕迹
  70. if($this->user_power($this->auth->id,'wuhen') != 1){
  71. $data = [
  72. 'uid' => $this->auth->id,
  73. 'to_uid' => $uid,
  74. ];
  75. $check = Db::name('user_visit')->where($data)->find();
  76. if($check){
  77. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  78. }else{
  79. $data['number'] = 1;
  80. $data['updatetime'] = time();
  81. Db::name('user_visit')->insertGetId($data);
  82. }
  83. }
  84. //活跃,在线
  85. $userinfo['is_active'] = $this->user_isactive($uid);
  86. //用户权限
  87. //$userinfo['power'] = Db::name('user_power')->where('user_id',$uid)->find();
  88. $this->success('success',$userinfo);
  89. }
  90. /////////////////////////////////////////////////////////////////////////////////////
  91. //这里不用连user_active表,完全使用user表的active_time,user_active表只做离线用
  92. //同城
  93. public function samecity(){
  94. $gender = input_post('gender','all');
  95. $agemin = input_post('agemin',0);
  96. $agemax = input_post('agemax',100);
  97. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  98. // $this->success('success',[]);
  99. }
  100. $map = [
  101. 'user.status' => 1,
  102. // 'user.cityname' => $this->auth->cityname,
  103. 'user.id' => ['neq',$this->auth->id],
  104. // 'user.longitude' => ['neq',''],
  105. // 'user.latitude' => ['neq',''],
  106. // 'user.is_online|user.is_livebc' => 1, //完全不考虑直播与语聊的权重,只用活跃做排序
  107. // 'user.active_time' => ['gt',time()-86400],
  108. ];
  109. if($gender != 'all'){
  110. $map['user.gender'] = $gender;
  111. }
  112. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  113. //dump($map);
  114. $field = [
  115. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender','user.active_time'
  116. ];
  117. $list = Db::name('user')->alias('user')->field($field)->where($map)->order('user.active_time desc')->autopage()->select();
  118. //dump($list);
  119. $list = list_domain_image($list,['avatar']);
  120. foreach($list as $key => $one){
  121. $one['age'] = birthtime_to_age($one['birthday']);
  122. // $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  123. $one['distance'] = rand(0,10).'km';
  124. //状态
  125. $one['active_text'] = '刚刚离线';
  126. if($one['active_time'] > time()-21600){
  127. $one['active_text'] = '1小时内在线';
  128. }
  129. if($one['active_time'] > time()-3600){
  130. $one['active_text'] = '在线';
  131. }
  132. $list[$key] = $one;
  133. }
  134. $this->success('success',$list);
  135. }
  136. //附近
  137. public function nearuser(){
  138. $gender = input_post('gender','all');
  139. $agemin = input_post('agemin',0);
  140. $agemax = input_post('agemax',100);
  141. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  142. // $this->success('success',[]);
  143. }
  144. //经过地图测算和公式推算,经度纬度 0.1即为11公里
  145. $map = [
  146. 'user.status' => 1,
  147. ////'user.cityname' => $this->auth->cityname,
  148. 'user.id' => ['neq',$this->auth->id],
  149. // 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  150. // 'user.latitude' => ['between',[$this->auth->latitude - 0.1,$this->auth->latitude + 0.1]],
  151. // 'user.is_online|user.is_livebc' => 1, //完全不考虑直播与语聊的权重,只用活跃做排序
  152. // 'user.active_time' => ['gt',time()-86400],
  153. ];
  154. if($gender != 'all'){
  155. $map['user.gender'] = $gender;
  156. }
  157. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  158. //dump($map);
  159. $field = [
  160. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender','user.active_time'
  161. ];
  162. $list = Db::name('user')->alias('user')->field($field)->where($map)->order('user.active_time desc')->autopage()->select();
  163. //dump($list);exit;
  164. $list = list_domain_image($list,['avatar']);
  165. foreach($list as $key => $one){
  166. $one['age'] = birthtime_to_age($one['birthday']);
  167. //$one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  168. $one['distance'] = rand(0,10).'km';
  169. //状态
  170. $one['active_text'] = '刚刚离线';
  171. if($one['active_time'] > time()-21600){
  172. $one['active_text'] = '1小时内在线';
  173. }
  174. if($one['active_time'] > time()-3600){
  175. $one['active_text'] = '在线';
  176. }
  177. $list[$key] = $one;
  178. }
  179. $this->success('success',$list);
  180. }
  181. //打字聊天每句话调用一次
  182. public function typing_once(){
  183. //检测用户
  184. $to_user_id = input_post('to_user_id');
  185. $to_user_info = Db::name('user')->field('id,real_status,gender,free_video,free_audio,free_typing')->where('id',$to_user_id)->find();
  186. if(!$to_user_info){
  187. $this->error('不存在的用户');
  188. }
  189. //正常价格
  190. $price = config('site.typing_min_price'); //扣费金币
  191. $bili = config('site.money_to_gold'); //兑换比例
  192. $gift_plat_scale = config('site.gift_plat_scale'); //抽成比例
  193. $money = bcdiv($price,$bili,2); //对应人民币
  194. $money = bcdiv(bcmul($money,100 - $gift_plat_scale,2),100,2); //抽成后收益
  195. //发起用户的分数,被发起用户的分数。按性别给分
  196. $auth_level = 0;
  197. $tous_level = 0;
  198. //打分
  199. if($this->auth->gender == 0 && $this->auth->real_status == 1){
  200. $auth_level = 30;//实名女最高
  201. }
  202. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  203. $auth_level = 20;//未实名女次之
  204. }
  205. if($this->auth->gender == 1){
  206. $auth_level = 10;//男性最低
  207. }
  208. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] == 1){
  209. $tous_level = 30;
  210. }
  211. if($to_user_info['gender'] == 0 && $to_user_info['real_status'] != 1){
  212. $tous_level = 20;
  213. }
  214. if($to_user_info['gender'] == 1){
  215. $tous_level = 10;
  216. }
  217. //同性不收钱
  218. //都是男的,不扣钱
  219. //都是实名认证的女性,不扣钱
  220. //都是未实名认证的女性,不扣钱
  221. if($auth_level == $tous_level){
  222. $price = 0;$money = 0;
  223. }
  224. //性别优势的人发起,免费
  225. if($auth_level > $tous_level){
  226. $price = 0;$money = 0;
  227. }
  228. Db::startTrans();
  229. //记录日志
  230. $data = [
  231. 'user_id' => $this->auth->id,
  232. 'price' => $price,
  233. 'createtime' => time(),
  234. 'to_user_id' => $to_user_id,
  235. 'money' => $money,
  236. ];
  237. $log_id = Db::name('user_match_typing_log')->insertGetId($data);
  238. if(!$log_id){
  239. Db::rollback();
  240. $this->error('扣费失败');
  241. }
  242. //同性别,提前结束
  243. if($auth_level == $tous_level){
  244. Db::commit();
  245. $this->success('success');
  246. }
  247. //零消费,零收益消费,提前结束,其实这条没必要,下面金钱操作还会过滤一次
  248. if($price == 0 && $money == 0){
  249. Db::commit();
  250. $this->success('success');
  251. }
  252. //扣钱uid,收钱uid,收钱free_video
  253. //分数少扣钱,分数多收益
  254. if($auth_level < $tous_level){
  255. $kou_user = $this->auth->id;
  256. $get_user = $to_user_info['id'];
  257. $get_user_free = $to_user_info['free_typing'];
  258. }else{
  259. //这种已经没有了
  260. $kou_user = $to_user_info['id'];
  261. $get_user = $this->auth->id;
  262. $get_user_free = $this->auth->free_typing;
  263. }
  264. //有性别差,扣费
  265. if($price > 0){
  266. $rs = model('wallet')->lockChangeAccountRemain($kou_user,'gold',-$price,13,'','user_match_typing_log',$log_id);
  267. if($rs['status'] === false){
  268. Db::rollback();
  269. $this->error($rs['msg']);
  270. }
  271. }
  272. //另一方加钱,0收费
  273. if($money > 0 && $get_user_free == 0){
  274. $rs = model('wallet')->lockChangeAccountRemain($get_user,'money',$money,23,'','user_match_typing_log',$log_id);
  275. if($rs['status'] === false){
  276. Db::rollback();
  277. $this->error($rs['msg']);
  278. }
  279. }
  280. //tag任务赠送金币
  281. //搭讪奖励
  282. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  283. if($task_rs === false){
  284. Db::rollback();
  285. $this->error('完成任务赠送奖励失败');
  286. }
  287. Db::commit();
  288. $this->success('success');
  289. }
  290. //语音匹配
  291. public function getaudiouser(){
  292. //判断资格
  293. /*$start = strtotime(date('Y-m-d'));
  294. $end = $start + 86399;
  295. $map = [
  296. 'user_id' => $this->auth->id,
  297. 'createtime' => ['between',[$start,$end]],
  298. 'price' => 0,
  299. ];
  300. $check = Db::name('user_match_audio_log')->where($map)->find();*/
  301. $check = true;
  302. //已经用掉免费的了,判断金额
  303. if($check){
  304. $price = config('site.audio_min_price');
  305. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  306. if($gold < $price){
  307. $this->error('您的金币已经不足,请充值');
  308. }
  309. }
  310. //找到互关的人,排除
  311. //$follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  312. //dump($follow_me);
  313. //$my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  314. //dump($my_follow);exit;
  315. //给出备选用户
  316. $map = [
  317. 'status' =>1, //未封禁用户
  318. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  319. 'is_online' => 0, //不在语聊间的
  320. 'is_livebc' => 0, //不在直播的
  321. 'is_active' => 1, //在线的
  322. //'real_status' => 1, //真人认证
  323. //'idcard_status' => 1, //实名认证
  324. 'open_match_audio' => 1, //打开语聊开关
  325. //'id' => ['NOT IN',$my_follow] //不是好友的
  326. 'id' => ['BETWEEN',[368,382]]
  327. ];
  328. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  329. $lists = $this->fliter_user($lists,10);
  330. $result = [];
  331. if(!empty($lists)){
  332. foreach($lists as $key => $val){
  333. $result[] = ['id'=>$val];
  334. }
  335. }
  336. //tag任务赠送金币
  337. //语音匹配奖励 +5金币
  338. if(!empty($result)){
  339. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,11);
  340. if($task_rs === false){
  341. $this->error('完成任务赠送奖励失败');
  342. }
  343. }
  344. $this->success('success',$result);
  345. }
  346. //视频匹配
  347. public function getvideouser(){
  348. //判断资格
  349. /*$start = strtotime(date('Y-m-d'));
  350. $end = $start + 86399;
  351. $map = [
  352. 'user_id' => $this->auth->id,
  353. 'createtime' => ['between',[$start,$end]],
  354. 'price' => 0,
  355. ];
  356. $check = Db::name('user_match_video_log')->where($map)->find();*/
  357. $check = true;
  358. //已经用掉免费的了,判断金额
  359. if($check){
  360. $price = config('site.video_min_price');
  361. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  362. if($gold < $price){
  363. $this->error('您的金币已经不足,请充值');
  364. }
  365. }
  366. //找到互关的人,排除
  367. //$follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  368. //dump($follow_me);
  369. //$my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  370. //dump($my_follow);exit;
  371. //给出备选用户
  372. $map = [
  373. 'status' =>1, //未封禁用户
  374. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  375. 'is_online' => 0, //不在语聊间的
  376. 'is_livebc' => 0, //不在直播的
  377. 'is_active' => 1, //在线的
  378. //'real_status' => 1, //真人认证
  379. //'idcard_status' => 1, //实名认证
  380. 'open_match_video' => 1, //打开视频开关的
  381. // 'id' => ['NOT IN',$my_follow] //不是好友的
  382. 'id' => ['BETWEEN',[368,382]]
  383. ];
  384. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  385. $lists = $this->fliter_user($lists,10);
  386. $result = [];
  387. if(!empty($lists)){
  388. foreach($lists as $key => $val){
  389. $result[] = ['id'=>$val];
  390. }
  391. }
  392. //tag任务赠送金币
  393. //视频匹配奖励 +5金币
  394. if(!empty($result)){
  395. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,12);
  396. if($task_rs === false){
  397. $this->error('完成任务赠送奖励失败');
  398. }
  399. }
  400. $this->success('success',$result);
  401. }
  402. //聊天匹配
  403. public function gettypinguser(){
  404. //找到互关的人,排除
  405. //$follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  406. //dump($follow_me);
  407. //$my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  408. //dump($my_follow);exit;
  409. //给出备选用户
  410. $map = [
  411. 'status' =>1, //未封禁用户
  412. 'gender' => $this->auth->gender == 1 ? 0 : 1, //异性
  413. //'real_status' => 1, //真人认证
  414. //'idcard_status' => 1, //实名认证
  415. //'is_active' => 1, //在线的
  416. //打开聊天开关的
  417. 'open_match_typing' => 1, //打开文字聊天开关的
  418. //'id' => ['NOT IN',$my_follow] //不是好友的
  419. 'id' => ['BETWEEN',[368,382]]
  420. ];
  421. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  422. //$lists = $this->fliter_user($lists,100);
  423. $lists = array_column($lists,'id');
  424. $result = [];
  425. if(!empty($lists)){
  426. /*foreach($lists as $key => $val){
  427. $result[] = ['id'=>$val];
  428. }*/
  429. $result = Db::name('user')->field('id,nickname,username,avatar,audio_bio')->where(['id'=>['IN',$lists]])->select();
  430. $result = list_domain_image($result,['avatar,audio_bio']);
  431. }
  432. //tag任务赠送金币
  433. //缘分匹配奖励 +5金币
  434. if(!empty($result)){
  435. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,10);
  436. if($task_rs === false){
  437. $this->error('完成任务赠送奖励失败');
  438. }
  439. }
  440. $this->success('success',$result);
  441. }
  442. //过滤规则
  443. private function fliter_user($lists,$number = 1){
  444. if(empty($lists)){
  445. return $lists;
  446. }
  447. //dump($lists);
  448. //过滤掉通话中的
  449. foreach($lists as $key => $val){
  450. if(redis_matching_get($val['id']) == 1){
  451. unset($lists[$key]);
  452. }
  453. }
  454. //预留全部
  455. $all_result = array_column($lists,'id');
  456. //dump($all_result);
  457. //提取同城的
  458. $citydata = [];
  459. foreach($lists as $key => $val){
  460. if( !empty($this->auth->cityname) && $this->auth->cityname == $val['cityname'] ){
  461. $citydata[] = $val['id'];
  462. }
  463. }
  464. //dump($citydata);
  465. //有标签交集的
  466. $tagdata = [];
  467. foreach($lists as $key => $val){
  468. if( !empty($this->auth->tag_ids) && !empty($val['tag_ids']) ){
  469. $auth_tag_ids = explode(',',$this->auth->tag_ids);
  470. $val_tag_ids = explode(',',$val['tag_ids']);
  471. if(count(array_intersect($auth_tag_ids,$val_tag_ids)) > 0){
  472. $tagdata[] = $val['id'];
  473. }
  474. }
  475. }
  476. //dump($tagdata);
  477. //双条件都满足
  478. $double_data = [];
  479. if(!empty($citydata) && !empty($tagdata)){
  480. $double_data = array_intersect($citydata,$tagdata);
  481. }
  482. //dump($double_data);
  483. if(count($double_data) >= $number){
  484. return $double_data;
  485. }
  486. //两种条件合并,去重。空数组合并没影响
  487. $merge_data = array_merge($citydata,$tagdata);
  488. $merge_data = array_flip(array_flip($merge_data));
  489. //dump($merge_data);
  490. if(count($merge_data) >= $number){
  491. return $merge_data;
  492. }
  493. return $all_result;
  494. }
  495. /**
  496. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  497. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  498. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  499. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  500. * @return float | false | string
  501. */
  502. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  503. if( empty( $point_1 ) || empty( $point_2 ) ){
  504. return false;
  505. }
  506. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  507. $p1_x = $point_1[0];
  508. $p1_y = $point_1[1];
  509. $p2_x = $point_2[0];
  510. $p2_y = $point_2[1];
  511. if(
  512. $p1_x < -180 || $p1_x > 180
  513. || $p2_x < -180 || $p2_x > 180
  514. || $p1_y < -90 || $p1_y > 90
  515. || $p2_y < -90 || $p2_y > 90
  516. ){
  517. return '0公里';
  518. }
  519. // 根据2点各自的坐标,计算2点之间直线距离的公式
  520. $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);
  521. // 是否计算为字符串公里距离
  522. if( !$calc_as_string ){
  523. return (string)round( $distance / 1000 , 1 ) . '公里';
  524. }
  525. // 如果计算为字符串公里距离
  526. if( $distance / 1000 > 1 ){
  527. $k = (string)round( $distance / 1000 , 1 );
  528. $m = (string)$distance % 1000 ;
  529. $distance = "{$k}公里{$m}米";
  530. }
  531. else{
  532. $distance = "{$distance}米";
  533. }
  534. return $distance;
  535. }
  536. //地图api,根据两地坐标,获得两地距离,打卡用的
  537. //type=0直线,type=1开车
  538. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  539. $result = 0;
  540. $apiurl = 'https://restapi.amap.com/v3/distance?';
  541. $param = [
  542. 'key' => '398c424811d1a59beac2f915323d334e',
  543. 'origins' => $start_lon.','.$start_lat,
  544. 'destination' => $end_lon.','.$end_lat,
  545. 'type' => $type,
  546. 'output' => 'json',
  547. ];
  548. $apiurl .= http_build_query($param);
  549. $request_rs = json_decode(curl_get($apiurl),true);
  550. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  551. if(isset($request_rs['results'][0]['distance']))
  552. {
  553. $result = $request_rs['results'][0]['distance'];
  554. }
  555. }
  556. //dump($result);
  557. return $result;
  558. }
  559. public function distance()
  560. {
  561. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  562. dump($a);
  563. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  564. dump($a);
  565. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  566. dump($b);
  567. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  568. dump($b);
  569. }
  570. }