Aliapi.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 阿里云API市场接口集合
  7. */
  8. namespace app\common\facade\library;
  9. use app\common\model\SystemApis;
  10. use app\common\model\SystemMemberBank;
  11. use app\common\model\SystemMemberBankBill;
  12. use filter\Filter;
  13. use GuzzleHttp\Client;
  14. class AliApi{
  15. public function __construct() {
  16. $this->config = SystemApis::config('aliapi');
  17. }
  18. /**
  19. * 根据IP地址定位
  20. */
  21. public function ip($ip,$member_id = 0){
  22. if($member_id > 0){
  23. $rel = self::moneyUpate($member_id);
  24. if(!$rel){
  25. return $rel['error_code'] == 0;
  26. }
  27. }
  28. $ip = trim(Filter::filter_escape($ip));
  29. $ip = $ip == '127.0.0.1' ? '222.88.142.161' : $ip;
  30. $client = new Client([
  31. 'base_uri' => 'http://ipquery.market.alicloudapi.com',
  32. 'timeout' => 2.0,
  33. ]);
  34. $rel = SystemApis::config('aliapi');
  35. $response = $client->request('GET','/query?ip='.$ip,[
  36. 'headers' => [
  37. 'Authorization' => 'APPCODE '.$rel['appcode'],
  38. 'Accept' => 'application/json',
  39. ]
  40. ]);
  41. return json_decode($response->getBody(),true);
  42. }
  43. /**
  44. * 根据经纬度定位所在城市
  45. */
  46. public function gps($latitude,$longitude,$member_id = 0){
  47. if($member_id > 0){
  48. $rel = self::moneyUpate($member_id);
  49. if(!$rel){
  50. return;
  51. }
  52. }
  53. $latitude = floatval(Filter::filter_escape($latitude));
  54. $longitude = floatval(Filter::filter_escape($longitude));
  55. $client = new Client([
  56. 'base_uri' => 'http://getlocat.market.alicloudapi.com/api/getLocationinfor',
  57. 'timeout' => 2.0,
  58. ]);
  59. $response = $client->request('POST',"?latlng={$latitude}%2C{$longitude}&type=2",[
  60. 'headers' => [
  61. 'Authorization' => 'APPCODE '.$this->config['appcode'],
  62. 'Accept' => 'application/json',
  63. ]
  64. ]);
  65. $rel = json_decode($response->getBody(),true);
  66. if($rel['error_code'] == 0){
  67. return $rel;
  68. }
  69. return;
  70. }
  71. /**
  72. * 根据地址位置转换经纬度
  73. */
  74. public function address($address,$miniapp_id = 0){
  75. if($miniapp_id > 0){
  76. $rel = self::moneyUpate($miniapp_id);
  77. if(!$rel){
  78. return;
  79. }
  80. }
  81. $address = Filter::filter_escape($address);
  82. $client = new Client([
  83. 'base_uri' => 'http://geo.market.alicloudapi.com/v3/geocode/geo',
  84. 'timeout' => 2.0,
  85. ]);
  86. $response = $client->request('GET',"?address={$address}&batch=false&output=JSON",[
  87. 'headers' => [
  88. 'Authorization' => 'APPCODE '.$this->config['appcode'],
  89. 'Accept' => 'application/json',
  90. ]
  91. ]);
  92. $rel = json_decode($response->getBody(),true);
  93. if($rel['status'] == 1){
  94. return $rel;
  95. }
  96. return;
  97. }
  98. /**
  99. * 定位费用扣除
  100. * @param [type] $id
  101. * @return void
  102. */
  103. protected function moneyUpate($member_id){
  104. if($member_id > 0 && !empty($this->config['price'])){
  105. $rel = SystemMemberBank::moneyJudge($member_id,$this->config['price']); //判断余额
  106. if($rel){
  107. return;
  108. }
  109. SystemMemberBankBill::create(['state' => 1,'money' => $this->config['price'],'member_id' => $member_id,'message' => '调用第三方服务收费','update_time' => time()]);
  110. SystemMemberBank::moneyUpdate($member_id, -$this->config['price']);
  111. return true;
  112. }
  113. return;
  114. }
  115. }