cmbYouku_Api/app/sms/AliSms.php

49 lines
1.4 KiB
PHP

<?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');
}
$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];
}
}