This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,83 @@
<?php
namespace Stemcell\Sms\Model;
use Stemcell\Sms\Api\Data\GatewayResponseInterface;
use Magento\Backend\App\ConfigInterface;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Math\Random;
use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use Psr\Log\LoggerInterface;
class AliyunSmsSender extends SmsSender
{
const ENDPOINT = 'dysmsapi.aliyuncs.com';
protected LoggerInterface $logger;
public function __construct(
ConfigInterface $config,
Redis $redis,
DateTimeFactory $dateTimeFactory,
Random $random,
GatewayResponseInterface $gatewayResponse,
DataObjectHelper $dataObjectHelper,
LoggerInterface $logger,
array $data = []
) {
parent::__construct($config, $redis, $dateTimeFactory, $random, $gatewayResponse, $dataObjectHelper, $data);
$this->logger = $logger;
}
/**
* @param String $field
* @return string
* @throws LocalizedException
*/
protected function getAliyunConfig(String $field) {
$path = 'sms/aliyun/'.$field;
$value = (string)$this->config->getValue($path);
if (!$value) {
throw new LocalizedException(__('please config %path', ['path'=>$path]));
}
return $value;
}
/**
* @return GatewayResponseInterface
* @throws LocalizedException
*/
protected function smsSend() : GatewayResponseInterface
{
$config = new Config([
"accessKeyId" => $this->getAliyunConfig('accessKeyId'),
"accessKeySecret" => $this->getAliyunConfig('accessKeySecret')
]);
$config->endpoint = self::ENDPOINT;
$client = new Dysmsapi($config);
$sendSmsRequest = new SendSmsRequest([
"signName" => $this->getSmsConfig('signature'),
"templateCode" => $this->getSmsConfig('template_code'),
"phoneNumbers" => $this->mobile,
"templateParam" => json_encode(['code'=>$this->code])
]);
$resp = $client->sendSms($sendSmsRequest)->body->toMap();
$this->logger->info('Aliyun Sms api response', $resp);
$resp[GatewayResponseInterface::RESPONSE] = $resp;
$resp[GatewayResponseInterface::MESSAGE] = $resp['Message'];
$resp[GatewayResponseInterface::TYPE] = $this->getSenderType();
$resp[GatewayResponseInterface::IS_SUCCESS] = $resp['Code']==='OK';
return $this->convertArrayToGatewayResponse($resp);
}
protected function getSenderType(): string
{
return 'aliyun';
}
}
@@ -0,0 +1,73 @@
<?php
namespace Stemcell\Sms\Model\Data;
use Stemcell\Sms\Api\Data\GatewayResponseInterface;
use Magento\Framework\DataObject;
class GatewayResponse extends DataObject implements GatewayResponseInterface
{
/**
* @param bool $isSuccess
* @return $this
*/
public function setIsSuccess(bool $isSuccess): self
{
$this->setData(self::IS_SUCCESS, $isSuccess);
return $this;
}
/**
* @return bool
*/
public function getIsSuccess(): bool
{
return $this->getData(self::IS_SUCCESS);
}
/**
* @param string $message
* @return $this
*/
public function setMessage(string $message): self
{
$this->setData(self::MESSAGE, $message);
return $this;
}
/**
* @return string
*/
public function getMessage(): string | null
{
return $this->getData(self::MESSAGE);
}
/**
* @param array $response
* @return $this
*/
public function setResponse(array $response): self
{
$this->setData(self::RESPONSE, $response);
return $this;
}
/**
* @return string[]
*/
public function getResponse(): array
{
return $this->getData(self::RESPONSE);
}
public function setType(string $type): self
{
$this->setData(self::TYPE, $type);
return $this;
}
public function getType(): string
{
return $this->getData(self::TYPE);
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace Stemcell\Sms\Model;
use Magento\Framework\App\DeploymentConfig;
class Redis extends \Magento\Framework\Cache\Backend\Redis
{
public function __construct(DeploymentConfig $config)
{
parent::__construct(
$config->get('redis')
);
}
/**
* @return \Credis_Client
*/
public function getRedis() {
return $this->_redis;
}
}
@@ -0,0 +1,11 @@
<?php
namespace Stemcell\Sms\Model;
use Stemcell\Sms\Api\Data\GatewayResponseInterface;
interface SmsSendInterface
{
public function send(): GatewayResponseInterface;
public function validate(string $code): bool;
}
+193
View File
@@ -0,0 +1,193 @@
<?php
namespace Stemcell\Sms\Model;
use Stemcell\Sms\Api\Data\GatewayResponseInterface;
use Magento\Backend\App\ConfigInterface;
use Magento\Framework\Exception\InvalidArgumentException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Math\Random;
use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
use \Magento\Framework\Api\DataObjectHelper;
/**
* property type should equal to captcha formId
* @package Stemcell\Sms\Model
*/
abstract class SmsSender implements SmsSendInterface
{
protected String $type;
protected String $mobile;
protected String $code;
protected bool $success;
protected String $message;
protected int $smsInterval = 0;
protected ConfigInterface $config;
protected Redis $redis;
protected DateTimeFactory $dateTimeFactory;
protected Random $random;
protected GatewayResponseInterface $gatewayResponse;
protected DataObjectHelper $dataObjectHelper;
public function __construct(
ConfigInterface $config,
Redis $redis,
DateTimeFactory $dateTimeFactory,
Random $random,
GatewayResponseInterface $gatewayResponse,
DataObjectHelper $dataObjectHelper,
array $data = []
) {
if (!isset($data['mobile']) || !isset($data['type'])) {
throw new LocalizedException(__('Mobile and Form Type is required'));
}
$this->mobile = $data['mobile'];
$this->random = $random;
$this->type = $data['type'];
$this->config = $config;
$this->redis = $redis;
$this->gatewayResponse = $gatewayResponse;
$this->dataObjectHelper = $dataObjectHelper;
$this->dateTimeFactory = $dateTimeFactory;
}
/**
* @return String
*/
public function getCode() {
return $this->code;
}
/**
* @return ConfigInterface
*/
public function getAppConfig() {
return $this->config;
}
public function getSmsConfig(String $field) {
$path = 'sms/'.$this->type.'/'.$field;
$value = $this->config->getValue($path);
if (!$value) {
throw new LocalizedException(__('please config %path', ['path'=>$path]));
}
return $value;
}
/**
* @param $path
* @return mixed
*/
protected function getGlobalConfig($path) {
$value = $this->config->getValue($path);
return $value;
}
public function getSuccess() {
return $this->success;
}
public function getMessage() {
return $this->message;
}
/**
* @return int
* @throws InvalidArgumentException
*/
public function getSmsInterval() {
if (!$this->type || !$this->mobile) {
throw new InvalidArgumentException(_('Please call setMobile,setType first'));
}
$sentAt = $this->smsInterval = (int)$this->redis->getRedis()
->hGet($this->type.'-'.$this->mobile, 'sent_at');
$now = $this->dateTimeFactory->create()->gmtTimestamp();
return $now-$sentAt;
}
/**
* @throws InvalidArgumentException
* @throws LocalizedException
*/
protected function beforeSend() {
if (!$this->type || !$this->mobile) {
throw new LocalizedException(__('Please call setMobile,setType first'));
}
$configInterval = $this->getGlobalConfig('sms/sms/time_interval');
if ($this->getSmsInterval() && $this->getSmsInterval() < $configInterval) {
throw new LocalizedException(__('SMS is sent too frequently, hold on please'));
}
}
/**
* @return GatewayResponseInterface
* @throws InvalidArgumentException
* @throws LocalizedException
*/
public function send(): GatewayResponseInterface
{
$this->beforeSend();
$this->code = Random::getRandomNumber(99999, 999999);
$response = $this->smsSend();
if ($response->getIsSuccess() || (int)$this->getGlobalConfig('sms/sms/debug')) {
$this->redis->getRedis()->hMSet(
$this->getSmsHashKey(),
[
'code' => $this->code,
'sent_at' => $this->dateTimeFactory->create()->gmtTimestamp()
]
);
$lifetime = (int)$this->getGlobalConfig('sms/sms/lifetime');
if ($lifetime){
$this->redis->getRedis()->expire($this->getSmsHashKey(), $lifetime);
}
}
return $response;
}
/**
* redis hash key
* @return string
*/
protected function getSmsHashKey() {
return $this->type.'-'.$this->mobile;
}
/**
* sms code validate
* @param string $code
* @return bool
*/
public function validate(string $code): bool
{
$storedCode = (int)$this->redis->getRedis()->hGet($this->getSmsHashKey(),'code');
if (!$storedCode) {
return false;
}
return $storedCode === (int)$code;
}
/**
* @param array $data
* @return GatewayResponseInterface
*/
protected function convertArrayToGatewayResponse(array $data) {
$this->dataObjectHelper->populateWithArray($this->gatewayResponse, $data, GatewayResponseInterface::class);
return $this->gatewayResponse;
}
protected abstract function smsSend(): GatewayResponseInterface;
protected abstract function getSenderType(): string;
}