Files
yusheng-php/application/common/controller/NumberAuth.php

64 lines
2.0 KiB
PHP
Raw Normal View History

2025-08-07 20:21:47 +08:00
<?php
namespace app\common\controller;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Dypnsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Models\CreateVerifySchemeRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use AlibabaCloud\Credentials\Credential;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
class NumberAuth
{
public static function getClient() {
$config = get_system_config();
$credential = new Credential();
$config = new Config([
"credential" => $credential,
'accessKeyId' => $config['aliyun_access_key_id'],
'accessKeySecret' => $config['aliyun_access_key_secret'],
]);
// Endpoint 请参考 https://api.aliyun.com/product/Dypnsapi
$config->endpoint = "dypnsapi.aliyuncs.com";
return new Dypnsapi($config);
}
/**
* 通过Token获取手机号
* @param string $token 客户端SDK返回的Token
* @return string|null
*/
public static function getMobileByToken($token) {
$client = self::getClient();
$createVerifySchemeRequest = new CreateVerifySchemeRequest([
'accessToken' => $token
]);
try {
$response = $client->getMobileWithOptions($createVerifySchemeRequest, new RuntimeOptions([]));
// var_dump($response->body);die;
if ($response->body->code == 'OK') {
return $response->body->getMobileResultDTO->mobile;
}
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
//NumberAuth::getMobileByToken(array_slice($token, 1));