流程分析
申請 api_key
雲片網 https://www.yunpian.com/
1.身份信息驗證
2.簽名
3.範本
獲取api_key 在env 中配置 YUNPIAN_API_KEY =#############(apikey)
獲取短信發送元件
composer require "overtrue/easy-sms"
封裝 easy-sms 元件方便使用
· touch config/easysms.php 創建一個配置文件 並添加一下內容
5.0,
// 預設發送配置
'default' => [
// 網關調用策略,預設:順序調用
'strategy' => OvertrueEasySmsStrategiesOrderStrategy::class,
// 預設可用的發送網關
'gateways' => [
'yunpian',
],
],
// 可用的網關配置
'gateways' => [
'errorlog' => [
'file' => '/tmp/easy-sms.log',
],
'yunpian' => [
'api_key' => env('YUNPIAN_API_KEY'),
],
],
];
· 創建一個 ServiceProvider
php artisan make:provider EasySmsServiceProvider
· 修改該文件
app->singleton(EasySms::class, function ($app) {
return new EasySms(config('easysms'));
});
$this->app->alias(EasySms::class, 'easysms');
}
}
· config/app.php 註冊服務商
AppProvidersEasySmsServiceProvider::class,
調試短信
php artisan thinker
$sms = app('easysms');
try {
$sms->send(13212345678, [
'content' => '【xxx提醒】您的驗證碼是1234。如非本人操作,請忽略本短信',
]);
} catch (OvertrueEasySmsExceptionsNoGatewayAvailableException $exception) {
$message = $exception->getException('yunpian')->getMessage();
dd($message);
}
測試成功後就可以接着實現api了
短信接口api構建
添加使用者註冊route
$api = app('DingoApiRoutingRouter');
$api->version('v1', [
'namespace' => 'AppHttpControllersApi'
], function($api) {
// 短信驗證碼
$api->post('verificationCodes', 'VerificationCodesController@store')
->name('api.verificationCodes.store');
});
數據庫遷移 創建phone字段
php artisan make:migration add_phone_to_users_table --table=users
新建表 和 撤銷 https://laravelacademy.org/post/130.html 文檔
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('email')->nullable(false)->change();
});
}
運行遷移
php artisan make:migrate
如果需要修改字段 則需要加上doctrine/dbal元件
composer require doctrine/dbal
創建api表單請求驗證類
php artisan make:request Api/VerificationCodeRequest
修改該類文件
[
'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'
]
];
}
}
創建控制器
php artisan make:controller Api/VerificationCodesController
可以先隨便打印一些東西postman測試route打通
編寫短信發送邏輯
phone;
// 生成4位隨機數,左側補0
$code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
try {
$result = $easySms->send($phone, [
'content' => "【xxx測試】您的驗證碼是{$code}。如非本人操作,請忽略本短信"
]);
} catch (OvertrueEasySmsExceptionsNoGatewayAvailableException $exception) {
$message = $exception->getException('yunpian')->getMessage();
return $this->response->errorInternal($message ?? '短信發送異常');
}
$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);
}
}
可能會碰到沒有安裝相關證書curl: (60) SSL certificate problem: unable to get local issuer certificate 錯誤