123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace App\Repositories\Eloquent;
- use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
- use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
- use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
- use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
- use App\Repositories\Contracts\SmsInterface;
- use Darabonba\OpenApi\Models\Config;
- class AliSmsFacadeRepository extends BaseRepository implements SmsInterface
- {
- private static $instance = null;
- private $client;
- public $request;
- private function __construct($config)
- {
- $config = new Config($config);
- $config->endpoint = "dysmsapi.ap-southeast-1.aliyuncs.com";
- $this->client = new Dysmsapi($config);
- $this->request = new SendSmsRequest();
- }
- public static function client($config = [])
- {
- if (self::$instance === null) {
- if(!$config) {
- $config = [
- // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
- "accessKeyId" => config('sms.alisms.accessKeyId'),
- // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
- "accessKeySecret" => config('sms.alisms.accessKeySecret'),
- ];
- }
- self::$instance = new self($config);
- }
- return self::$instance;
- }
- public function getClient()
- {
- return $this->client;
- }
- public function setRequest($phoneNumber, $templateCode, $templateParam)
- {
- $this->request->phoneNumbers = $phoneNumber; // 接收短信的手机号码
- $this->request->signName = config('sms.alisms.signName'); // 短信签名名称
- $this->request->templateCode = $templateCode; // 短信模板ID
- $this->request->templateParam = json_encode($templateParam); // 短信模板变量对应的实际值
- return $this;
- }
- public function send()
- {
-
- $client = $this->getClient();
- try{
- $runtime = new RuntimeOptions();
- $runtime->maxIdleConns = 3;
- $runtime->connectTimeout = 1000;
- $runtime->readTimeout = 1000;
- $res = $client->sendSms($this->request, $runtime);
- var_dump($res);exit;
- }catch (TeaUnableRetryError $e) {
- // 获取报错数据
- var_dump($e->getErrorInfo());
- // 获取报错信息
- var_dump($e->getMessage());
- // 获取最后一次报错的 Exception 实例
- var_dump($e->getLastException());
- // 获取最后一次请求的 Request 实例
- var_dump($e->getLastRequest());
- }
- }
- }
|