找回密码
This commit is contained in:
parent
d0f649a337
commit
43004275ab
@ -3,6 +3,7 @@ namespace App\Domains\Company\Http\Controllers;
|
|||||||
|
|
||||||
use App\Core\Controller;
|
use App\Core\Controller;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use App\Domains\Sms\Services\SmsService;
|
||||||
use App\Domains\Virtual\Services\CompanyAccountService;
|
use App\Domains\Virtual\Services\CompanyAccountService;
|
||||||
|
|
||||||
class AccountController extends Controller
|
class AccountController extends Controller
|
||||||
@ -25,39 +26,17 @@ class AccountController extends Controller
|
|||||||
*
|
*
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function passwordByOld()
|
public function password()
|
||||||
{
|
{
|
||||||
$oldPassword = $this->request->get('old_password');
|
$newPassword = $this->request->get('new_password');
|
||||||
$password = $this->request->get('password');
|
|
||||||
|
|
||||||
if ($this->account->password !== md5($oldPassword . $this->account->salt)) {
|
if ($this->account->password === md5($newPassword . $this->account->salt)) {
|
||||||
return err('原密码不正确');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->account->password === md5($password . $this->account->salt)) {
|
|
||||||
return err('密码未修改');
|
return err('密码未修改');
|
||||||
}
|
}
|
||||||
|
|
||||||
$attributes = [
|
$attributes = [
|
||||||
'id' => $this->account->id,
|
'id' => $this->account->id,
|
||||||
'password' => $password,
|
'password' => $newPassword,
|
||||||
];
|
|
||||||
|
|
||||||
$account = $this->companyAccountService->store($attributes);
|
|
||||||
|
|
||||||
return res($account, '修改成功');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 修改密码.
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function password()
|
|
||||||
{
|
|
||||||
$attributes = [
|
|
||||||
'id' => $this->account->id,
|
|
||||||
'password' => $this->request->get('password'),
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$account = $this->companyAccountService->store($attributes);
|
$account = $this->companyAccountService->store($attributes);
|
||||||
@ -81,4 +60,48 @@ class AccountController extends Controller
|
|||||||
|
|
||||||
return res($account, '修改成功');
|
return res($account, '修改成功');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 找回密码
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function resetStep1()
|
||||||
|
{
|
||||||
|
$username = $this->request->get('username');
|
||||||
|
|
||||||
|
if (!$this->account = $this->companyAccountService->fetch($username)) {
|
||||||
|
return err('用户名不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($this->account->mobile)) {
|
||||||
|
return err('用户未绑定手机号');
|
||||||
|
}
|
||||||
|
|
||||||
|
$freqsecs = app(SmsService::class)->sendVcode($this->account->mobile, '密码找回');
|
||||||
|
|
||||||
|
return res(['freg' => $freqsecs], '发送成功');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 找回密码
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function resetStep2()
|
||||||
|
{
|
||||||
|
$username = $this->request->get('username');
|
||||||
|
|
||||||
|
if (!$this->account = $this->companyAccountService->fetch($username)) {
|
||||||
|
return err('用户名不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($this->account->mobile)) {
|
||||||
|
return err('用户未绑定手机号');
|
||||||
|
}
|
||||||
|
|
||||||
|
app(SmsService::class)->verifyCode($this->account->mobile, $this->request->get('verify_code'));
|
||||||
|
|
||||||
|
return $this->password();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
24
app/Domains/Company/Http/Middleware/PasswordAuthenticate.php
Normal file
24
app/Domains/Company/Http/Middleware/PasswordAuthenticate.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Company\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Exceptions\AuthException;
|
||||||
|
use App\Exceptions\NotAllowedException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||||
|
|
||||||
|
class PasswordAuthenticate
|
||||||
|
{
|
||||||
|
public function handle(Request $request, Closure $next)
|
||||||
|
{
|
||||||
|
$account = $request->user('company');
|
||||||
|
$password = $request->get('password', '');
|
||||||
|
|
||||||
|
if ($account->password !== md5($password . $account->salt)) {
|
||||||
|
throw new NotAllowedException('密码不正确');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
@ -29,5 +29,6 @@ class CompanyServiceProvider extends ServiceProvider
|
|||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->app->register(RouteServiceProvider::class);
|
$this->app->register(RouteServiceProvider::class);
|
||||||
|
$this->app->register(MiddlewareServiceProvider::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
29
app/Domains/Company/Providers/MiddlewareServiceProvider.php
Normal file
29
app/Domains/Company/Providers/MiddlewareServiceProvider.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Company\Providers;
|
||||||
|
|
||||||
|
use Dipper\Foundation\Core\MiddlewareServiceProvider as ServiceProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class MiddlewareServiceProvider.
|
||||||
|
*
|
||||||
|
* @author HollyTeng <n.haoyuan@gmail.com>
|
||||||
|
*/
|
||||||
|
class MiddlewareServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 全局中间件
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $middleware = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 路由中间件
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $routeMiddleware = [
|
||||||
|
'company_password' => \App\Domains\Company\Http\Middleware\PasswordAuthenticate::class,
|
||||||
|
];
|
||||||
|
}
|
@ -5,8 +5,10 @@ $router->group(['prefix' => 'companies', 'as' => 'companies', 'middleware' => ['
|
|||||||
|
|
||||||
// The controllers live in Domains/Company/Http/Controllers
|
// The controllers live in Domains/Company/Http/Controllers
|
||||||
$router->get('/', ['as' => 'index', 'uses' => 'AccountController@index']);
|
$router->get('/', ['as' => 'index', 'uses' => 'AccountController@index']);
|
||||||
$router->post('/account/password_by_old', ['as' => 'account.passwordByOld', 'uses' => 'AccountController@passwordByOld']);
|
$router->get('/account/reset', ['as' => 'account.resetStep1', 'uses' => 'AccountController@resetStep1', 'middleware' => ['captcha']]);
|
||||||
$router->post('/account/password', ['as' => 'account.password', 'uses' => 'AccountController@password', 'middleware' => ['verify_code']]);
|
$router->post('/account/reset', ['as' => 'account.resetStep2', 'uses' => 'AccountController@resetStep2']);
|
||||||
|
$router->post('/account/password_by_old', ['as' => 'account.passwordByOld', 'uses' => 'AccountController@password', 'middleware' => ['company_password']]);
|
||||||
|
$router->post('/account/password_by_sms', ['as' => 'account.passwordBySms', 'uses' => 'AccountController@password', 'middleware' => ['verify_code']]);
|
||||||
$router->post('/account/mobile', ['as' => 'account.mobile', 'uses' => 'AccountController@mobile', 'middleware' => ['verify_code']]);
|
$router->post('/account/mobile', ['as' => 'account.mobile', 'uses' => 'AccountController@mobile', 'middleware' => ['verify_code']]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -72,7 +72,7 @@ class SmsService extends Service
|
|||||||
$freqsecs = 60; // 重试时间
|
$freqsecs = 60; // 重试时间
|
||||||
|
|
||||||
$verifyCode = rand(100000, 999999);
|
$verifyCode = rand(100000, 999999);
|
||||||
$message = new VcodeMessage(['code' => $code, 'product' => $product]);
|
$message = new VcodeMessage(['code' => $verifyCode, 'product' => $product]);
|
||||||
$this->send($mobile, $message);
|
$this->send($mobile, $message);
|
||||||
|
|
||||||
Cache::put(self::$cacheVcodePrefix.$mobile, [
|
Cache::put(self::$cacheVcodePrefix.$mobile, [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user