// +---------------------------------------------------------------------- namespace crmeb\services\sms\storage; use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi; use crmeb\basic\BaseSms; use \Exception; use AlibabaCloud\Tea\Exception\TeaError; use Darabonba\OpenApi\Models\Config as AliConfig; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; use think\exception\ValidateException; use think\facade\Config; /** * Class Aliyun * @package crmeb\services\sms\storage */ class Aliyun extends BaseSms { /** * 短信签名 * @var string */ protected $sign = ''; /** * 模板id * @var array */ protected $templateIds = []; /** * @param array $config * @return mixed|void */ protected function initialize(array $config = []) { parent::initialize($config); $this->templateIds = Config::get($this->configFile . '.stores.' . $this->name . '.template_id', []); $this->sign = Config::get($this->configFile . '.stores.' . $this->name . '.sign_name', ''); } public function createClient() { $config = new AliConfig([ // 您的 AccessKey ID "accessKeyId" => Config::get($this->configFile . '.stores.' . $this->name . '.access_key_id'), // 您的 AccessKey Secret "accessKeySecret" => Config::get($this->configFile . '.stores.' . $this->name . '.access_key_secret') ]); // 访问的域名 $config->endpoint = "dysmsapi.aliyuncs.com"; return new Dysmsapi($config); } protected function getTemplateCode(string $templateId) { return $this->templateIds[$templateId] ?? null; } /** * @param string $phone * @param string $templateId * @param array $data * @return array[]|bool|mixed */ public function send(string $phone, string $templateId, array $data = []) { if (empty($phone)) { return $this->setError('电话号码不能为空'); } //验证码只支持一个参数 if ($templateId == 'VERIFICATION_CODE') { unset($data['time']); } $temp = $this->getTemplateCode($templateId); if (is_null($temp)) { throw new ValidateException('模版ID不存在'); } $client = $this->createClient(); $sendSmsRequest = new SendSmsRequest([ "phoneNumbers" => $phone, "signName" => $this->sign, "templateCode" => $temp, "templateParam" => json_encode($data), ]); $runtime = new RuntimeOptions([]); try { $resp = $client->sendSmsWithOptions($sendSmsRequest, $runtime); } catch (Exception $error) { if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } throw new ValidateException('【阿里云平台错误提示】:' . $error->message); } if (isset($resp) && $resp->body->code !== 'OK') { throw new ValidateException('【阿里云平台错误提示】:' . $resp->body->message); } return 'ok'; } public function open() { } public function modify(string $sign = null, string $phone, string $code) { } public function info() { } public function temps(int $page = 0, int $limit = 10, int $type = 1) { } public function apply(string $title, string $content, int $type) { } public function applys(int $tempType, int $page, int $limit) { } public function record($record_id) { } }