短信接口

This commit is contained in:
fthvgb1 2018-06-03 16:11:51 +08:00
parent 86ca9aefce
commit 32c0b14902
11 changed files with 239 additions and 14 deletions

View File

@ -42,3 +42,6 @@ MAIL_ENCRYPTION=null
PUSHER_APP_ID= PUSHER_APP_ID=
PUSHER_APP_KEY= PUSHER_APP_KEY=
PUSHER_APP_SECRET= PUSHER_APP_SECRET=
# 云片
YUNPIAN_API_KEY=

View File

@ -0,0 +1,11 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller as BaseController;
use Dingo\Api\Routing\Helpers;
class Controller extends BaseController
{
use Helpers;
}

View File

@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\VerificationCodeRequest;
use Overtrue\EasySms\EasySms;
class VerificationCodesController extends Controller
{
public function store(VerificationCodeRequest $request, EasySms $easySms)
{
$phone = $request->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);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
class VerificationCodeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'phone' => [
'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'
]
];
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Overtrue\EasySms\EasySms;
class EasySmsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(EasySms::class, function ($app) {
return new EasySms(config('easysms'));
});
$this->app->alias(EasySms::class, 'easysms');
}
}

View File

@ -9,6 +9,7 @@
"type": "project", "type": "project",
"require": { "require": {
"php": ">=7.0.0", "php": ">=7.0.0",
"dingo/api": "2.0.0-alpha2",
"doctrine/dbal": "^2.6", "doctrine/dbal": "^2.6",
"fideloper/proxy": "~3.3", "fideloper/proxy": "~3.3",
"guzzlehttp/guzzle": "~6.3", "guzzlehttp/guzzle": "~6.3",
@ -19,13 +20,13 @@
"laravel/tinker": "~1.0", "laravel/tinker": "~1.0",
"mews/captcha": "~2.0", "mews/captcha": "~2.0",
"mews/purifier": "~2.0", "mews/purifier": "~2.0",
"overtrue/easy-sms": "^1.0",
"overtrue/laravel-lang": "^3.0", "overtrue/laravel-lang": "^3.0",
"overtrue/pinyin": "~3.0", "overtrue/pinyin": "~3.0",
"predis/predis": "~1.0", "predis/predis": "~1.0",
"spatie/laravel-permission": "~2.7", "spatie/laravel-permission": "~2.7",
"summerblue/administrator": "~1.1", "summerblue/administrator": "~1.1",
"viacreative/sudo-su": "~1.1", "viacreative/sudo-su": "~1.1"
"dingo/api": "2.0.0-alpha2"
}, },
"require-dev": { "require-dev": {
"barryvdh/laravel-debugbar": "^3.1", "barryvdh/laravel-debugbar": "^3.1",

53
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "be9bf7555100c9cd575aece4ea97f923", "content-hash": "6a770296ff23a10490492e56ff7e0137",
"packages": [ "packages": [
{ {
"name": "cakephp/chronos", "name": "cakephp/chronos",
@ -2179,6 +2179,47 @@
], ],
"time": "2018-03-25T17:35:16+00:00" "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", "name": "overtrue/laravel-lang",
"version": "3.0.8", "version": "3.0.8",
@ -2715,16 +2756,16 @@
}, },
{ {
"name": "psy/psysh", "name": "psy/psysh",
"version": "v0.9.4", "version": "v0.9.5",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/bobthecow/psysh.git", "url": "https://github.com/bobthecow/psysh.git",
"reference": "4d969a0e08e1e05e7207c07cb4207017ecc9a331" "reference": "0951e91ac04ca28cf317f3997a0adfc319e80106"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/bobthecow/psysh/zipball/4d969a0e08e1e05e7207c07cb4207017ecc9a331", "url": "https://api.github.com/repos/bobthecow/psysh/zipball/0951e91ac04ca28cf317f3997a0adfc319e80106",
"reference": "4d969a0e08e1e05e7207c07cb4207017ecc9a331", "reference": "0951e91ac04ca28cf317f3997a0adfc319e80106",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2783,7 +2824,7 @@
"interactive", "interactive",
"shell" "shell"
], ],
"time": "2018-05-22T06:48:07+00:00" "time": "2018-06-02T16:39:22+00:00"
}, },
{ {
"name": "ramsey/uuid", "name": "ramsey/uuid",

View File

@ -177,6 +177,8 @@ return [
App\Providers\EventServiceProvider::class, App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
App\Providers\EasySmsServiceProvider::class,
], ],
/* /*

26
config/easysms.php Normal file
View File

@ -0,0 +1,26 @@
<?php
return [
// HTTP 请求的超时时间(秒)
'timeout' => 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'),
],
],
];

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddPhoneToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->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();
});
}
}

View File

@ -12,17 +12,16 @@
*/ */
$api = app('Dingo\Api\Routing\Router'); $api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) { $api->version('v1', ['namespace' => 'App\Http\Controllers\Api'], function ($api) {
$api->get('version', function () { $api->post('verificationCodes', 'VerificationCodesController@store')
return response('this is version v1'); ->name('api.verificationCodes.store');
});
}); });
$api->version('v2', function ($api) { /*$api->version('v2', function ($api) {
$api->get('version', function () { $api->get('version', function () {
return response('this is version v2'); return response('this is version v2');
}); });
}); });*/
/*Route::middleware('auth:api')->get('/user', function (Request $request) { /*Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user(); return $request->user();