修改密码

This commit is contained in:
邓皓元 2018-12-11 11:28:11 +08:00
parent 881c48398d
commit d0f649a337
7 changed files with 100 additions and 13 deletions

View File

@ -113,9 +113,9 @@ class AccountService extends Service implements JwtServiceContract
DB::beginTransaction(); DB::beginTransaction();
if($attributes['password']){ if ($attributes['password']) {
$attributes['salt'] = Str::random(6); $attributes['salt'] = Str::random(6);
$attributes['password'] = md5(md5($attributes['password']).$attributes['salt']); $attributes['password'] = md5($attributes['password'].$attributes['salt']);
} }
if (!$attributes['id']) { if (!$attributes['id']) {

View File

@ -0,0 +1,84 @@
<?php
namespace App\Domains\Company\Http\Controllers;
use App\Core\Controller;
use Illuminate\Http\Request;
use App\Domains\Virtual\Services\CompanyAccountService;
class AccountController extends Controller
{
protected $request;
protected $companyAccountService;
/**
* 构造函数,自动注入.
*/
public function __construct(Request $request, CompanyAccountService $companyAccountService)
{
$this->request = $request;
$this->companyAccountService = $companyAccountService;
$this->account = $request->user('company');
}
/**
* 修改密码.
*
* @return \Illuminate\Http\Response
*/
public function passwordByOld()
{
$oldPassword = $this->request->get('old_password');
$password = $this->request->get('password');
if ($this->account->password !== md5($oldPassword . $this->account->salt)) {
return err('原密码不正确');
}
if ($this->account->password === md5($password . $this->account->salt)) {
return err('密码未修改');
}
$attributes = [
'id' => $this->account->id,
'password' => $password,
];
$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);
return res($account, '修改成功');
}
/**
* 绑定手机.
*
* @return \Illuminate\Http\Response
*/
public function mobile()
{
$attributes = [
'id' => $this->account->id,
'mobile' => $this->request->get('mobile'),
];
$account = $this->companyAccountService->store($attributes);
return res($account, '修改成功');
}
}

View File

@ -1,10 +1,13 @@
<?php <?php
// Prefix: /api/companies // Prefix: /api/companies
$router->group(['prefix' => 'companies', 'as' => 'companies'], function($router) { $router->group(['prefix' => 'companies', 'as' => 'companies', 'middleware' => ['companyAuth']], function ($router) {
// The controllers live in Domains/Company/Http/Controllers // The controllers live in Domains/Company/Http/Controllers
$router->get('/', ['as' => 'index', 'uses' => 'CompanyController@index']); $router->get('/', ['as' => 'index', 'uses' => 'AccountController@index']);
$router->post('/account/password_by_old', ['as' => 'account.passwordByOld', 'uses' => 'AccountController@passwordByOld']);
$router->post('/account/password', ['as' => 'account.password', 'uses' => 'AccountController@password', 'middleware' => ['verify_code']]);
$router->post('/account/mobile', ['as' => 'account.mobile', 'uses' => 'AccountController@mobile', 'middleware' => ['verify_code']]);
/** /**
* 需要认证的接口 * 需要认证的接口

View File

@ -14,9 +14,9 @@ class VerifyCodeAuthenticate
{ {
$mobile = $request->get('mobile', ''); $mobile = $request->get('mobile', '');
$verify_code = $request->get('verify_code', ''); $verifyCode = $request->get('verify_code', '');
app(SmsService::class)->verifyCode($mobile, $code); app(SmsService::class)->verifyCode($mobile, $verifyCode);
return $next($request); return $next($request);
} }

View File

@ -24,6 +24,6 @@ class MiddlewareServiceProvider extends ServiceProvider
* @var array * @var array
*/ */
protected $routeMiddleware = [ protected $routeMiddleware = [
'verifyCode' => \App\Domains\Sms\Http\Middleware\VerifyCodeAuthenticate::class, 'verify_code' => \App\Domains\Sms\Http\Middleware\VerifyCodeAuthenticate::class,
]; ];
} }

View File

@ -71,14 +71,14 @@ class SmsService extends Service
$freqsecs = 60; // 重试时间 $freqsecs = 60; // 重试时间
$code = rand(100000, 999999); $verifyCode = rand(100000, 999999);
$message = new VcodeMessage(['code' => $code, 'product' => $product]); $message = new VcodeMessage(['code' => $code, 'product' => $product]);
$this->send($mobile, $message); $this->send($mobile, $message);
Cache::put(self::$cacheVcodePrefix.$mobile, [ Cache::put(self::$cacheVcodePrefix.$mobile, [
'mobile' => $mobile, 'mobile' => $mobile,
'created_time' => time(), 'created_time' => time(),
'vcode' => $code, 'verify_code' => $verifyCode,
'freq' => $freqsecs, 'freq' => $freqsecs,
], self::$cacheVcodeMinutes); ], self::$cacheVcodeMinutes);
@ -90,13 +90,13 @@ class SmsService extends Service
* *
* @return void * @return void
*/ */
public function verifyCode($mobile, $code) public function verifyCode($mobile, $verifyCode)
{ {
$key = self::$cacheVcodePrefix.$mobile; $key = self::$cacheVcodePrefix.$mobile;
$cacheCode = Cache::get($key); $cacheCode = Cache::get($key);
if ((!$cacheCode['verifycode'] || $cacheCode['verifycode'] != $code) && $code != 998877) { if ((!$cacheCode['verify_code'] || $cacheCode['verify_code'] != $verifyCode) && $verifyCode != 998877) {
throw new InvalidArgumentException('验证码错误, 请重新输入'); throw new InvalidArgumentException('验证码错误, 请重新输入');
} else { } else {
Cache::forget($key); Cache::forget($key);

View File

@ -97,7 +97,7 @@ class CompanyAccountService extends Service implements JwtServiceContract
if ($attributes['password']) { if ($attributes['password']) {
$attributes['salt'] = Str::random(6); $attributes['salt'] = Str::random(6);
$attributes['password'] = md5(md5($attributes['password']).$attributes['salt']); $attributes['password'] = md5($attributes['password'].$attributes['salt']);
} }
if (!$attributes['id']) { if (!$attributes['id']) {