Skip to content

Commit

Permalink
Merge pull request #11 from ofcold/dev-3
Browse files Browse the repository at this point in the history
Dev 3
  • Loading branch information
lilianjin authored Jun 21, 2018
2 parents 037c5cd + 8e58c69 commit 957fdda
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 56 deletions.
34 changes: 24 additions & 10 deletions src/Qcold/Qcloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ofcold\LuminousSMS\Qcold;

use Ofcold\LuminousSMS\Handlers;
use Ofcold\LuminousSMS\Exceptions\MethodNotFoundException;

/**
* Class Qcloud
Expand Down Expand Up @@ -52,6 +53,20 @@ class Qcloud extends Handlers
]
];

/**
* The Signature instance.
*
* @var Signature
*/
protected static $signature;

/**
* The Template instance.
*
* @var Templatye
*/
protected static $template;

/**
* Send SMS to send.
*
Expand All @@ -63,28 +78,27 @@ class Qcloud extends Handlers
*/
public function send() : array
{
return (new Sender($this))
->render();
return Sender::render($this);
}

/**
* Get the SignaTure instance.
*
* @param string|null $method
* @param string $method
* @param array $attributes
*
* @return mixed
*/
public function getSignature(?string $method = null, ...$attributes)
public function getSignature(string $method, ...$attributes)
{
$instance = new SignaTure($this);
$instance = static::$signature ?: new SignaTure($this);

if ( $method && method_exists($instance, $method) )
if ( method_exists($instance, $method) )
{
return $instance->$method(...$attributes);
}

return $instance;
throw new MethodNotFoundException("Add {$method} does not exist object SignaTure");
}

/**
Expand All @@ -97,13 +111,13 @@ public function getSignature(?string $method = null, ...$attributes)
*/
public function getTemplate(?string $method = null, ...$attributes)
{
$instance = new Template($this);
$instance = static::$template ?: new Template($this);

if ( $method && method_exists($instance, $method) )
if ( method_exists($instance, $method) )
{
return $instance->$method(...$attributes);
}

return $instance;
throw new MethodNotFoundException("Add {$method} does not exist object Template");
}
}
61 changes: 25 additions & 36 deletions src/Qcold/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ofcold\LuminousSMS\Qcold;

use Ofcold\LuminousSMS\Results;
use Ofcold\LuminousSMS\Helpers;
use Ofcold\LuminousSMS\Exceptions\HandlerBadException;

Expand All @@ -21,62 +22,45 @@
*/
class Sender
{
/**
* The qcloud instance.
*
* @var Qcloud
*/
protected $qcloud;

/**
* Create an a new Sender.
*
* @param Qcloud $qcloud
*/
public function __construct(Qcloud $qcloud)
{
$this->qcloud = $qcloud;
}

/**
* Sender SMS
*
* @return mixed
*/
public function render()
public static function render(Qcloud $qcloud)
{
$method = method_exists($this, $this->qcloud->getMessage()->getType())
? $this->qcloud->getMessage()->getType()
$method = method_exists(__CLASS__, $qcloud->getMessage()->getType())
? $qcloud->getMessage()->getType()
: 'text';

$params = $this->$method();
$params = static::$method($qcloud);

// Set SMS flag.
if ( $sign = $this->qcloud->getMessage()->getSign() )
if ( $sign = $qcloud->getMessage()->getSign() )
{
$params['sign'] = $sign;
}

// Set the full mobile phone.
$params['tel'] = [
'nationcode' => $this->qcloud->getMessage()->getCode(),
'mobile' => $this->qcloud->getMessage()->getMobilePhone()
'nationcode' => $qcloud->getMessage()->getCode(),
'mobile' => $qcloud->getMessage()->getMobilePhone()
];

$params['time'] = time();
$params['ext'] = '';

$random = Helpers::random(10);

$params['sig'] = $this->createSign($params, $random);
$params['sig'] = static::createSign($params, $random, $qcloud);

return Results::render($this->qcloud->request(
return Results::render($qcloud->request(
'post',
sprintf(
'%s%s?sdkappid=%s&random=%s',
Qcloud::REQUEST_URL,
Qcloud::REQUEST_METHOD[$this->qcloud->getMessage()->getType()],
$this->qcloud->getConfig('app_id'),
Qcloud::REQUEST_METHOD[$qcloud->getMessage()->getType()],
$qcloud->getConfig('app_id'),
$random
),
[
Expand All @@ -94,13 +78,15 @@ public function render()
*
* Text SMS request body.
*
* @param Qcloud $qcloud
*
* @return array
*/
protected function text() : array
protected static function text(Qcloud $qcloud) : array
{
return [
'type' => (int)($this->qcloud->getMessage()->getType() !== 'text'),
'msg' => $this->qcloud->getMessage()->getContent(),
'type' => (int)($qcloud->getMessage()->getType() !== 'text'),
'msg' => $qcloud->getMessage()->getContent(),
'extend' => ''
];
}
Expand All @@ -110,18 +96,20 @@ protected function text() : array
*
* Voice SMS request body.
*
* @param Qcloud $qcloud
*
* @return array
*/
protected function voice() : array
protected static function voice(Qcloud $qcloud) : array
{
return [
'promptfile' => $this->qcloud->getMessage()->getContent(),
'promptfile' => $qcloud->getMessage()->getContent(),
'prompttype' => 2,
'playtimes' => 2
];
}

public function templateId()
public static function templateId(Qcloud $qcloud)
{

}
Expand All @@ -131,18 +119,19 @@ public function templateId()
*
* @param array $params
* @param string $random
* @param Qcloud $qcloud
*
* @return string
*/
protected function createSign(array $params, string $random) : string
protected static function createSign(array $params, string $random, Qcloud $qcloud) : string
{
ksort($params);

return hash(
'sha256',
sprintf(
'appkey=%s&random=%s&time=%s&mobile=%s',
$this->qcloud->getConfig('app_key'),
$qcloud->getConfig('app_key'),
$random,
$params['time'],
$params['tel']['mobile']
Expand Down
21 changes: 11 additions & 10 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ $sms = new LuminousSMS(include __DIR__ . '/resources/config/sms.php');

// -------------------------------------------------------------------------------------------------
// Send Message.
// $result = $sms->sender(function($messager) {
// $messager
// ->setMobilePhone('18898726543')
// ->setType(Ofcold\LuminousSMS\Contracts\MessageInterface::VOICE_MESSAGE)
// ->setContent('您的验证码是{code}, 验证码将在2分钟后失效!请及时使用。')
// ->setPaserData(['code' => rand(1000, 9999)]);
// });
$result = $sms->sender(function($messager) {
$messager
->setMobilePhone('18898726543')
->setType(Ofcold\LuminousSMS\Contracts\MessageInterface::VOICE_MESSAGE)
->setContent('您的验证码是{code}, 验证码将在2分钟后失效!请及时使用。')
->setPaserData(['code' => rand(1000, 9999)]);
var_dump($messager);
});

// var_dump($result);
var_dump($result);

// -------------------------------------------------------------------------------------------------

Expand All @@ -34,6 +35,6 @@ $sms = new LuminousSMS(include __DIR__ . '/resources/config/sms.php');
// Edit
//$resuts = $sms->createHandler('qcloud')->getSignature('edit', '150986', '你是哪里来');
//
$resuts = $sms->createHandler('qcloud')->getSignature('remove', 150987);
// $resuts = $sms->createHandler('qcloud')->getSignature('remove', 150987);

var_dump($resuts);
// var_dump($resuts);

0 comments on commit 957fdda

Please sign in to comment.