<?php
namespace app\common\lib\sms;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use think\facade\Env;
use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
use Darabonba\OpenApi\Models\Config;
use Darabonba\OpenApi\Models\Params;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;
class AliSms
{
public static function createClient($accessKeyId, $accessKeySecret)
{
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
$config->endpoint = "dysmsapi.aliyuncs.com";
return new OpenApiClient($config);
}
public static function createApiInfo()
{
$params = new Params([
"action" => "SendSms",
"version" => "2017-05-25",
"protocol" => "HTTPS",
"method" => "POST",
"authType" => "AK",
"style" => "RPC",
"pathname" => "/",
"reqBodyType" => "json",
"bodyType" => "json"
]);
return $params;
}
public static function sendCode(string $phone, int $code): array
{
$client = self::createClient(Env::get(''), Env::get(''));
$params = self::createApiInfo();
$temp_param = ['code' => $code];
$queries = [];
$queries["PhoneNumbers"] = $phone;
$queries["SignName"] = "yshop";
$queries["TemplateCode"] = "SMS_461981253";
$queries["TemplateParam"] = json_encode($temp_param);
$runtime = new RuntimeOptions([]);
$request = new OpenApiRequest([
"query" => OpenApiUtilClient::query($queries)
]);
try {
$result = $client->callApi($params, $request, $runtime);
} catch (TeaUnableRetryError $teaUnableRetryError) {
return false;
}
return $result;
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}