p_ysk/app/model/AlibabaSms.php

188 lines
6.7 KiB
PHP

<?php
namespace app\model;
use support\think\Db;
use think\Exception;
use support\Log;
use think\Model;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\CreateSmsTemplateRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\GetSmsTemplateRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\UpdateSmsTemplateRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\DeleteSmsTemplateRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\QuerySendDetailsRequest;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils;
class AlibabaSms extends Model
{
protected static $sms_code = '陕西超掌柜科技';
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Dysmsapi Client
*/
public static function createClient(){
$config = new Config([
"accessKeyId" => config('cons.alibaba_sms.access_key_id'),
"accessKeySecret" => config('cons.alibaba_sms.access_key_secret')
]);
$config->endpoint = "dysmsapi.aliyuncs.com";
return new Dysmsapi($config);
}
/**
* 发送短信
* @param array $args
* @return void
*/
public static function main($args){
Log::info('最终发送短信--' . json_encode($args, 256));
$client = self::createClient();
$sendSmsRequest = new SendSmsRequest($args);
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值
$res = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
Log::info('发送短信接口返回---》' . json_encode($res->body->toArray()));
return $res->body->toArray();
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// 如有需要,请打印 error
Utils::assertAsString($error->message);
Log::info('发送短信错误--' . $error->message);
}
}
// 申请新短信模版
public static function CreateSmsTemplate($data)
{
Log::info('最终发送短信模版--' . json_encode($data, 256));
$client = self::createClient();
$createSmsTemplateRequest = new CreateSmsTemplateRequest($data);
$runtime = new RuntimeOptions([]);
try {
$res = $client->createSmsTemplateWithOptions($createSmsTemplateRequest, $runtime);
Log::info('申请新短信模版接口返回---》' . json_encode($res->body->toArray()));
return $res->body->toArray();
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::info('申请新短信模版错误--' . $error->message);
return false;
}
}
// 查询模版审核详情
public static function GetSmsTemplate($data)
{
$client = self::createClient();
$getSmsTemplateRequest = new GetSmsTemplateRequest($data);
$runtime = new RuntimeOptions([]);
try {
$res = $client->getSmsTemplateWithOptions($getSmsTemplateRequest, $runtime);
Log::info('查询模版审核详情接口返回---》' . json_encode($res->body->toArray()));
return $res->body->toArray();
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::info('查询模版审核详情接口错误--' . $error->message);
}
}
// 修改模版
public static function UpdateSmsTemplate($data)
{
Log::info('最终修改发送短信模版--' . json_encode($data));
$client = self::createClient();
$updateSmsTemplateRequest = new UpdateSmsTemplateRequest($data);
$runtime = new RuntimeOptions([]);
try {
$res = $client->updateSmsTemplateWithOptions($updateSmsTemplateRequest, $runtime);
Log::info('修改模版接口返回---》' . json_encode($res->body->toArray()));
return $res->body->toArray();
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::info('修改模版接口错误--' . $error->message);
}
}
// 删除短信模版
public static function DeleteSmsTemplate($data)
{
$client = self::createClient();
$deleteSmsTemplateRequest = new DeleteSmsTemplateRequest($data);
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值
$res = $client->deleteSmsTemplateWithOptions($deleteSmsTemplateRequest, $runtime);
Log::info('删除短信模版接口返回---》' . json_encode($res->body->toArray()));
if($res->body->code == 'OK') {
return true;
}else {
return false;
}
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::info('删除短信模版接口错误--' . $error->message);
}
}
/**
* 查询短信发送结果
*/
public static function QuerySendDetails($data)
{
$client = self::createClient();
$querySendDetailsRequest = new QuerySendDetailsRequest($data);
$runtime = new RuntimeOptions([]);
try {
$res = $client->querySendDetailsWithOptions($querySendDetailsRequest, $runtime);
Log::info('查询短信发送结果接口返回---》' . json_encode($res->body->toArray()));
return $res->body->toArray();
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Utils::assertAsString($error->message);
Log::info('查询短信发送结果接口错误--' . $error->message);
}
}
}