<?php

namespace App\Services;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use App\Http\Response\CatchError;

class AliyunService
{
    use CatchError;
    public function getLocalMobile()
    {
        AlibabaCloud::accessKeyClient(config('aliyun.id'), config('aliyun.secret'))
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        $token = request()->input('access_token');
        if (empty($token)) throw new \Exception("缺少阿里云access_token");
        try {
            $result = AlibabaCloud::rpc()
                ->product('Dypnsapi')
                ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('GetMobile')
                ->method('POST')
                ->host('dypnsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'AccessToken' => $token,
                    ],
                ])
                ->request();
            $result = $result->toArray();
            if ($result['Code'] == 'OK') {
                return $result['GetMobileResultDTO']['Mobile'];
            }
            return false;
        } catch (ClientException $e) {
            $this->getError($e);
            return false;
        } catch (ServerException $e) {
            $this->getError($e);
            return false;
        } catch (\Exception $e) {
            $this->getError($e);
            return false;
        }
    }
}
