it-devgroup / laravel-activation-code
laravel 激活码
2.0.0
2023-08-04 18:06 UTC
Requires
- php: ^8.0
- illuminate/collections: ^9.0|^10.0
- illuminate/database: ^9.0|^10.0
- illuminate/support: ^9.0|^10.0
README
为Lumen安装
1. 打开文件 bootstrap/app.php
并添加新的服务提供者
$app->register(\ItDevgroup\LaravelActivationCode\Providers\ActivationCodeServiceProvider::class);
取消注释的字符串
$app->withFacades();
$app->withEloquent();
添加在 $app->configure('app'); 之后
$app->configure('activation_code');
2. 运行命令
创建配置文件
php artisan activation:code:publish --tag=config
创建迁移文件
php artisan activation:code:publish --tag=migration
生成表格
php artisan migrate
为laravel安装
1. 打开文件 config/app.php 并搜索
'providers' => [
...
]
添加到部分
\ItDevgroup\LaravelActivationCode\Providers\ActivationCodeServiceProvider::class,
示例
'providers' => [
...
\ItDevgroup\LaravelActivationCode\Providers\ActivationCodeServiceProvider::class,
]
2. 运行命令
创建配置文件
php artisan vendor:publish --provider="ItDevgroup\LaravelActivationCode\Providers\ActivationCodeServiceProvider" --tag=config
创建迁移文件
php artisan activation:code:publish --tag=migration
生成表格
php artisan migrate
环境变量
文件 .env
输入代码的总尝试次数
ACTIVATION_CODE_DEFAULT_MAX_ATTEMPT=5
输入代码的总尝试次数(短信模式)
ACTIVATION_CODE_SMS_MAX_ATTEMPT=5
生成代码模式
ACTIVATION_CODE_DEFAULT_GENERATE_MODE=5
生成代码模式(短信模式)
ACTIVATION_CODE_SMS_GENERATE_MODE=4
代码长度
ACTIVATION_CODE_DEFAULT_CODE_LENGTH=20
代码长度(短信模式)
ACTIVATION_CODE_SMS_CODE_LENGTH=5
代码TTL
ACTIVATION_CODE_DEFAULT_CODE_TTL=1h
代码TTL(短信模式)
ACTIVATION_CODE_SMS_CODE_TTL=5m
自定义模型
第1步
创建激活码的自定义模型
示例
文件: app/CustomFile.php
内容
<?php
namespace App;
class CustomFile extends \ItDevgroup\LaravelActivationCode\Model\ActivationCode
{
}
如果需要更改表名或需要添加其他代码
<?php
namespace App;
class CustomFile extends \ItDevgroup\LaravelActivationCode\Model\ActivationCode
{
protected $table = 'YOUR_TABLE_NAME';
// other code
}
第2步
打开 config/activation_code.php 并更改参数 "model",例如
...
// remove
'model' => \ItDevgroup\LaravelActivationCode\Model\ActivationCode::class,
// add
'model' => \App\CustomFile::class,
用法
初始化服务
$service = app(\ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::class);
或注入
// use
use ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface;
// constructor
public function __construct(
ActivationCodeServiceInterface $activationCodeService
)
我们将使用变量 $service
生成代码
返回 \ItDevgroup\LaravelActivationCode\Model\ActivationCode eloquent 模型
使用最少参数的基本用法
$model = $service->make('test@test.com', 'user_register');
使用参数的用法
$model = $service->make('test@test.com', 'user_register', 10);
// 1 parameter - receiver for code (email, phone or other)
// 2 parameter - type code (context) (user activation, password recovery, confirm order or other)
// 3 parameter - (optional) entity ID
检查激活码是否有效
返回 \ItDevgroup\LaravelActivationCode\Model\ActivationCode eloquent 模型
如果返回模型,则代码有效
$model = $service->get('test@test.com', '12345', 'user_register');
// with extra parameters
$model = $service->get('test@test.com', '12345', 'user_register', true, true);
// 1 parameter - receiver for code (email, phone or other)
// 2 parameter - code
// 3 parameter - type code (context) (user activation, password recovery, confirm order or other)
// 4 parameter - (optional) use exception (true - use (default), false - not use)
// 5 parameter - (optional) disabled attempts for code (true - disable, false - enable (default))
或
$model = $service->getByCode('12345', 'user_register');
$model = $service->getByCode('12345', 'user_register', false);
// 1 parameter - code
// 2 parameter - type code (context) (user activation, password recovery, confirm order or other)
// 3 parameter - (optional) use exception (true - use (default), false - not use)
删除激活码
如果需要激活代码或仅删除它,请使用
$service->delete($model);
配置激活码
所有配置都是可选的,具体取决于任务
示例
$service
->setMode(\ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::MODE_SMS)
->setGenerateCodeMode(\ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::GENERATE_CODE_MODE_ALPHABET_LOWER)
->setCodeLength(7)
->setCodeTTL('20m')
->setMaxAttempt(3);
$service
->setMode('sms')
->setGenerateCodeMode(3)
->setCodeLength(7)
->setCodeTTL('20m')
->setMaxAttempt(5);
- setMode - 代码生成模式(null - 使用默认配置,sms - 使用短信模式的配置)(仅适用于未手动覆盖的配置)
- setGenerateCodeMode - 生成代码的规则
-
- ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::GENERATE_CODE_MODE_ALPHABET (1) - 仅字母,不区分大小写
-
- ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::GENERATE_CODE_MODE_ALPHABET_LOWER (2) - 仅小写字母
-
- ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::GENERATE_CODE_MODE_ALPHABET_UPPER (3) - 仅大写字母
-
- ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::GENERATE_CODE_MODE_NUMBER (4) - 仅数字
-
- ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::GENERATE_CODE_MODE_ALL (5) - (默认) 字母和数字
- setCodeLength - 代码长度
- setCodeTTL - 代码生命周期,格式:10m(例如:10 - 10秒,10m - 10分钟,10h - 10小时,10d - 10天)
- setMaxAttempt - 输入代码的最大尝试次数(检查代码时使用)
重置配置
- 模式
- 生成代码
- 代码长度
- 代码TTL
- 最大尝试次数
$service->reset();
示例
// generate code
$model = $service->make('test@test.com', 'user_register');
// get code
$model = $service->get('test@test.com', '12345', 'user_register');
... you PHP code
// activate of code
$service->delete($model);
使用自定义配置
// custom configuration
$service
->setMode(\ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::MODE_SMS)
->setGenerateCodeMode(\ItDevgroup\LaravelActivationCode\ActivationCodeServiceInterface::GENERATE_CODE_MODE_ALPHABET_LOWER)
->setCodeLength(7)
->setCodeTTL('20m')
->setMaxAttempt(3);
// create code
$model = $service->make('test@test.com', 'user_register');
// get code
$service->setMaxAttempt(3);
$model = $service->get('test@test.com', '12345', 'user_register');
... you PHP code
// activate of code
$service->delete($model);