123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace app\common;
- use app\Request;
- use Joypack\Tencent\Map\Bundle\Location;
- use Joypack\Tencent\Map\Bundle\LocationOption;
- use qiniu\basic\BaseController;
- abstract class ApiBaseController extends BaseController
- {
-
- protected $userInfo;
-
- protected $uid;
- protected $service = null;
-
- public function __construct(Request $request)
- {
- parent::__construct($request);
- $this->initialize();
- }
- protected function initialize()
- {
- $this->uid = $this->request->uid();
- $this->userInfo = $this->request->user();
- }
-
- public function getAddress(): array
- {
- list($latitude, $longitude) = $this->request->location();
- $address = $this->geoLbscoder($latitude, $longitude);
- if ($address) {
- return [
- 'address' => $address['address_component'],
- 'formatted_addresses' => $address['formatted_addresses'],
- 'info' => $address['ad_info'],
- ];
- }
- return [];
- }
-
- private function geoLbscoder(string $latitude, string $longitude)
- {
- if (!$latitude || !$longitude) {
- return null;
- }
- $mapKey = sys_config('tengxun_map_key');
- $mapKSecret = sys_config('tengxun_map_secret', '');
- if (!$mapKSecret) $mapKSecret = null;
- if (!$mapKey) {
- return null;
- }
- try {
- $locationOption = new LocationOption($mapKey, $mapKSecret);
- $locationOption->setLocation($latitude, $longitude);
- $location = new Location($locationOption);
- $res = $location->request();
- if ($res->error) {
- return null;
- }
- if ($res->status) {
- return null;
- }
- if (!$res->result) {
- return null;
- }
- return $res->result;
- } catch (\Throwable $e) {
- return null;
- }
- }
- }
|