cmbYouku_Api/app/sms/AliSms.php

51 lines
1.5 KiB
PHP
Raw Normal View History

2024-07-01 15:57:07 +08:00
<?php
namespace app\sms;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
class AliSms
{
public static function getInstance($appId,$appKey)
{
$config = new Config([
'accessKeyId' => $appId,
'accessKeySecret' => $appKey
]);
$config->endpoint = 'dysmsapi.aliyuncs.com';
return new Dysmsapi($config);
}
/** 阿里云发送短信
* phone_numbers 手机号码
* 目前短信验证码模板变量为 code
* 其他参数根据消息模板传递
* @param $data
* @param $type
* @return array
*/
public static function sendSms($data,$type = 1)
{
$template = env('alisms.template_sms_code');
if($type == 2) {
$template = env('alisms.template_issue_code');
2024-07-05 15:22:57 +08:00
} else if ($type == 3) {
$template = env('alisms.template_agreement_pay_code');
2024-07-01 15:57:07 +08:00
}
$client = self::getInstance(env('alisms.app_id'),env('alisms.app_key'));
$sms = new SendSmsRequest([]);
$sms->phoneNumbers = $data['phone_numbers'];
$sms->templateCode = $template;
$sms->signName = env('alisms.sign_name');
$sms->templateParam = json_encode($data);
$runtime = new RuntimeOptions([]);
$result = $client->sendSmsWithOptions($sms,$runtime);
return ['code' => $result->statusCode,'message' => $result->body->message];
}
}