From 32c0b14902954d7ef4ed83ba7f8b1e57ab226d29 Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Sun, 3 Jun 2018 16:11:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 3 ++ app/Http/Controllers/Api/Controller.php | 11 ++++ .../Api/VerificationCodesController.php | 41 ++++++++++++++ .../Requests/Api/VerificationCodeRequest.php | 34 ++++++++++++ app/Providers/EasySmsServiceProvider.php | 33 ++++++++++++ composer.json | 5 +- composer.lock | 53 ++++++++++++++++--- config/app.php | 2 + config/easysms.php | 26 +++++++++ ..._06_03_144825_add_phone_to_users_table.php | 34 ++++++++++++ routes/api.php | 11 ++-- 11 files changed, 239 insertions(+), 14 deletions(-) create mode 100644 app/Http/Controllers/Api/Controller.php create mode 100644 app/Http/Controllers/Api/VerificationCodesController.php create mode 100644 app/Http/Requests/Api/VerificationCodeRequest.php create mode 100644 app/Providers/EasySmsServiceProvider.php create mode 100644 config/easysms.php create mode 100644 database/migrations/2018_06_03_144825_add_phone_to_users_table.php diff --git a/.env.example b/.env.example index 23c63fa..302ea5a 100644 --- a/.env.example +++ b/.env.example @@ -42,3 +42,6 @@ MAIL_ENCRYPTION=null PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= + +# 云片 +YUNPIAN_API_KEY= \ No newline at end of file diff --git a/app/Http/Controllers/Api/Controller.php b/app/Http/Controllers/Api/Controller.php new file mode 100644 index 0000000..46c3d6b --- /dev/null +++ b/app/Http/Controllers/Api/Controller.php @@ -0,0 +1,11 @@ +get('phone'); + // 生成4位随机数,左侧补0 + + + if (!app()->environment('production')) { + $code = '1234'; + } else { + try { + $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT); + $result = $easySms->send($phone, [ + 'content' => "【Lbbs社区】您的验证码是{$code}。如非本人操作,请忽略本短信" + ]); + } catch (\GuzzleHttp\Exception\ClientException $exception) { + $response = $exception->getResponse(); + $result = json_decode($response->getBody()->getContents(), true); + return $this->response->errorInternal($result['msg'] ?? '短信发送异常'); + } + } + + $key = 'verificationCode_' . str_random(15); + $expiredAt = now()->addMinutes(10); + // 缓存验证码 10分钟过期。 + \Cache::put($key, ['phone' => $phone, 'code' => $code], $expiredAt); + + return $this->response->array([ + 'key' => $key, + 'expired_at' => $expiredAt->toDateTimeString(), + ])->setStatusCode(201); + } +} diff --git a/app/Http/Requests/Api/VerificationCodeRequest.php b/app/Http/Requests/Api/VerificationCodeRequest.php new file mode 100644 index 0000000..fb9c00b --- /dev/null +++ b/app/Http/Requests/Api/VerificationCodeRequest.php @@ -0,0 +1,34 @@ + [ + 'required', + 'regex:/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/', + 'unique:users' + ] + ]; + } +} diff --git a/app/Providers/EasySmsServiceProvider.php b/app/Providers/EasySmsServiceProvider.php new file mode 100644 index 0000000..986ba8a --- /dev/null +++ b/app/Providers/EasySmsServiceProvider.php @@ -0,0 +1,33 @@ +app->singleton(EasySms::class, function ($app) { + return new EasySms(config('easysms')); + }); + + $this->app->alias(EasySms::class, 'easysms'); + } +} diff --git a/composer.json b/composer.json index 002047f..72556db 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "type": "project", "require": { "php": ">=7.0.0", + "dingo/api": "2.0.0-alpha2", "doctrine/dbal": "^2.6", "fideloper/proxy": "~3.3", "guzzlehttp/guzzle": "~6.3", @@ -19,13 +20,13 @@ "laravel/tinker": "~1.0", "mews/captcha": "~2.0", "mews/purifier": "~2.0", + "overtrue/easy-sms": "^1.0", "overtrue/laravel-lang": "^3.0", "overtrue/pinyin": "~3.0", "predis/predis": "~1.0", "spatie/laravel-permission": "~2.7", "summerblue/administrator": "~1.1", - "viacreative/sudo-su": "~1.1", - "dingo/api": "2.0.0-alpha2" + "viacreative/sudo-su": "~1.1" }, "require-dev": { "barryvdh/laravel-debugbar": "^3.1", diff --git a/composer.lock b/composer.lock index 7a9c60e..2c3a16c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "be9bf7555100c9cd575aece4ea97f923", + "content-hash": "6a770296ff23a10490492e56ff7e0137", "packages": [ { "name": "cakephp/chronos", @@ -2180,6 +2180,47 @@ "time": "2018-03-25T17:35:16+00:00" }, { + "name": "overtrue/easy-sms", + "version": "1.0.8", + "source": { + "type": "git", + "url": "https://github.com/overtrue/easy-sms.git", + "reference": "2a1c840d3f42287f77958df3c40366e0bf277087" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/overtrue/easy-sms/zipball/2a1c840d3f42287f77958df3c40366e0bf277087", + "reference": "2a1c840d3f42287f77958df3c40366e0bf277087", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.2", + "php": ">=5.6" + }, + "require-dev": { + "mockery/mockery": "1.0.x-dev", + "phpunit/phpunit": "^6.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Overtrue\\EasySms\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "overtrue", + "email": "i@overtrue.me" + } + ], + "description": "The easiest way to send short message.", + "time": "2018-05-25T05:27:04+00:00" + }, + { "name": "overtrue/laravel-lang", "version": "3.0.8", "source": { @@ -2715,16 +2756,16 @@ }, { "name": "psy/psysh", - "version": "v0.9.4", + "version": "v0.9.5", "source": { "type": "git", "url": "https://github.com/bobthecow/psysh.git", - "reference": "4d969a0e08e1e05e7207c07cb4207017ecc9a331" + "reference": "0951e91ac04ca28cf317f3997a0adfc319e80106" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/bobthecow/psysh/zipball/4d969a0e08e1e05e7207c07cb4207017ecc9a331", - "reference": "4d969a0e08e1e05e7207c07cb4207017ecc9a331", + "url": "https://api.github.com/repos/bobthecow/psysh/zipball/0951e91ac04ca28cf317f3997a0adfc319e80106", + "reference": "0951e91ac04ca28cf317f3997a0adfc319e80106", "shasum": "" }, "require": { @@ -2783,7 +2824,7 @@ "interactive", "shell" ], - "time": "2018-05-22T06:48:07+00:00" + "time": "2018-06-02T16:39:22+00:00" }, { "name": "ramsey/uuid", diff --git a/config/app.php b/config/app.php index b1d464e..0b33c3e 100644 --- a/config/app.php +++ b/config/app.php @@ -177,6 +177,8 @@ return [ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, + App\Providers\EasySmsServiceProvider::class, + ], /* diff --git a/config/easysms.php b/config/easysms.php new file mode 100644 index 0000000..30a083f --- /dev/null +++ b/config/easysms.php @@ -0,0 +1,26 @@ + 5.0, + + // 默认发送配置 + 'default' => [ + // 网关调用策略,默认:顺序调用 + 'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class, + + // 默认可用的发送网关 + 'gateways' => [ + 'yunpian', + ], + ], + // 可用的网关配置 + 'gateways' => [ + 'errorlog' => [ + 'file' => '/tmp/easy-sms.log', + ], + 'yunpian' => [ + 'api_key' => env('YUNPIAN_API_KEY'), + ], + ], +]; \ No newline at end of file diff --git a/database/migrations/2018_06_03_144825_add_phone_to_users_table.php b/database/migrations/2018_06_03_144825_add_phone_to_users_table.php new file mode 100644 index 0000000..3c97b75 --- /dev/null +++ b/database/migrations/2018_06_03_144825_add_phone_to_users_table.php @@ -0,0 +1,34 @@ +string('phone')->nullable()->unique()->after('name'); + $table->string('email')->nullable()->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('phone'); + $table->string('eamil')->nullable(false)->change(); + }); + } +} diff --git a/routes/api.php b/routes/api.php index 03c1bb8..05ec45f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -12,17 +12,16 @@ */ $api = app('Dingo\Api\Routing\Router'); -$api->version('v1', function ($api) { - $api->get('version', function () { - return response('this is version v1'); - }); +$api->version('v1', ['namespace' => 'App\Http\Controllers\Api'], function ($api) { + $api->post('verificationCodes', 'VerificationCodesController@store') + ->name('api.verificationCodes.store'); }); -$api->version('v2', function ($api) { +/*$api->version('v2', function ($api) { $api->get('version', function () { return response('this is version v2'); }); -}); +});*/ /*Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user();