diff --git a/src/Sms.php b/src/Sms.php index 88d52bb..e4d685f 100644 --- a/src/Sms.php +++ b/src/Sms.php @@ -80,20 +80,22 @@ public function setStorage(StorageInterface $storage) */ public function send($to) { - $this->setKey($to); + try { - //1. get code from storage. - $code = $this->getCodeFromStorage(); + $this->setKey($to); - if ($this->needNewCode($code)) { - $code = $this->getNewCode($to); - } + //1. get code from storage. + $code = $this->getCodeFromStorage(); - $validMinutes = (int)config('ibrand.sms.code.validMinutes', 5); + if ($this->needNewCode($code)) { + $code = $this->getNewCode($to); + } + + $validMinutes = (int)config('ibrand.sms.code.validMinutes', 5); + + $message = new CodeMessage($code->code, $validMinutes); - $message = new CodeMessage($code->code, $validMinutes); - try { $results = $this->easySms->send($to, $message); foreach ($results as $key => $value) { @@ -105,13 +107,27 @@ public function send($to) return true; } } - } catch (NoGatewayAvailableException $e) { + + } catch (NoGatewayAvailableException $noGatewayAvailableException) { + return false; + } catch (\Exception $exception) { return false; } return false; } + + /** + * check china mobile. + * @param $to + * @return false|int + */ + public function verifyMobile($to) + { + return preg_match('/^(?=\d{11}$)^1(?:3\d|4[57]|5[^4\D]|66|7[^249\D]|8\d|9[89])\d{8}$/', $to); + } + /** * @param $to * diff --git a/src/SmsController.php b/src/SmsController.php index c30a019..74bd2e9 100644 --- a/src/SmsController.php +++ b/src/SmsController.php @@ -27,6 +27,9 @@ public function postSendCode() { $mobile = request('mobile'); + if(!config('app.debug') && !Sms::verifyMobile($mobile)){ + return response()->json(['success' => false, 'message' => '无效手机号码']); + } if (!Sms::canSend($mobile)) { return response()->json(['success' => false, 'message' => '每60秒发送一次']); diff --git a/tests/SessionSmsTest.php b/tests/SessionSmsTest.php index 56e8657..9b91940 100644 --- a/tests/SessionSmsTest.php +++ b/tests/SessionSmsTest.php @@ -1,14 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ namespace iBrand\Sms\Test; - use iBrand\Sms\Storage\SessionStorage; class SessionSmsTest extends \Orchestra\Testbench\TestCase @@ -42,4 +44,4 @@ protected function getEnvironmentSetUp($app) $app['config']->set('ibrand.sms', require __DIR__.'/../config/config.php'); $app['config']->set('ibrand.sms.storage', SessionStorage::class); } -} \ No newline at end of file +} diff --git a/tests/SmsControllerTest.php b/tests/SmsControllerTest.php index ff12ef3..0597ba0 100644 --- a/tests/SmsControllerTest.php +++ b/tests/SmsControllerTest.php @@ -1,14 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ namespace iBrand\Sms\Test; - class SmsControllerTest extends \Orchestra\Testbench\TestCase { /** @@ -36,27 +38,32 @@ protected function getPackageAliases($app) public function testPostSendCode() { //1. test success mobile. - $response = $this->post('sms/verify-code',['mobile'=>'18988885555']); + $response = $this->post('sms/verify-code', ['mobile' => '18973305743']); $response ->assertStatus(200) ->assertJson(['success' => true, 'message' => '短信发送成功']); //2. test repeat in 60 seconds. - - $response = $this->post('sms/verify-code',['mobile'=>'18988885555']); + $response = $this->post('sms/verify-code', ['mobile' => '18973305743']); $response ->assertStatus(200) ->assertJson(['success' => false, 'message' => '每60秒发送一次']); + + //3. test invalid mobile. + $response = $this->post('sms/verify-code', ['mobile' => '10000000000']); + + $response + ->assertStatus(200) + ->assertJson(['success' => false, 'message' => '无效手机号码']); } public function testInfo() { - $response= $this->get('sms/info?mobile=18988885555'); + $response = $this->get('sms/info?mobile=18988885555'); $response ->assertStatus(200); } - -} \ No newline at end of file +} diff --git a/tests/SmsTestTrait.php b/tests/SmsTestTrait.php index 48db4d9..c097c51 100644 --- a/tests/SmsTestTrait.php +++ b/tests/SmsTestTrait.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace iBrand\Sms\Test; use iBrand\Sms\Storage\CacheStorage; @@ -7,14 +16,13 @@ trait SmsTestTrait { - public function testStorage() { Sms::setStorage(new CacheStorage()); $storage = Sms::getStorage(); - $this->assertEquals(CacheStorage::class, get_class($storage)); + $this->assertSame(CacheStorage::class, get_class($storage)); } /** @@ -24,7 +32,7 @@ public function testKey() { $key = md5('ibrand.sms.18988888888'); Sms::setKey('18988888888'); - $this->assertEquals($key, Sms::getKey()); + $this->assertSame($key, Sms::getKey()); } /** @@ -32,7 +40,6 @@ public function testKey() */ public function testSend() { - //1. test send. $result = Sms::send('18988888888'); $this->assertTrue($result); @@ -77,4 +84,4 @@ public function testCheckCode() $this->assertFalse($result); } -} \ No newline at end of file +}