發(fā)郵件
Lumen5.1 使用Mail郵件
找回密碼
- 路由
//找回密碼
$app->get('/recover','CompController@recoverPage');
$app->post('/recover','CompController@recover');
//重置密碼
$app->get('/revise/{guid}/{token}','CompController@revise');
$app->post('/revise','CompController@doRevise');
- 控制器
/**
* 找回密碼頁
* @return \Illuminate\View\View
*/
public function recoverPage()
{
return view('mails.recoverpw');
}
/**
* 找回密碼操作
* @return \Illuminate\View\View
*/
public function recover(Request $request)
{
// 首先獲取email
$data = $request->only('email');
// 驗證數(shù)據(jù)
$validator = \Validator::make($data, ['email' => 'required|email']);
if ($validator->fails()) return redirect('/recover')->withErrors($validator);
// 判斷數(shù)據(jù)庫中是否存在email
$uri = config('uri.remote') . '/comp/forgetPassword/' . $data['email'];
$res = ExtCurl::post($uri);
// 判斷服務(wù)器為空
if (empty($res)) return view('admin.errors.404')->withErrors(['status' => 500, 'msg' => '服務(wù)器繁忙請稍候']);
if (empty($res['status']) || $res['status'] != 200) return view('admin.errors.404')->withErrors($res);
// 判斷是否是禁用狀態(tài)
if ($res['data']['status'] ==1) {
// 如果等于1,說明不是禁用狀態(tài)
// 存儲session
session(['tmpUser' => $res['data']]);
// 發(fā)郵件
$user = new \stdClass();
$user->username = $res['data']['name'];
$user->email = $res['data']['email'];
$link = url('/revise/' . $res['data']['guid'] . '/' . $res['data']['token']);
try {
\Illuminate\Support\Facades\Mail::send('mails.msg', ['link' => $link], function ($m) use($user) {
$m->to($user->email, $user->username)->subject('重置密碼信息!');
});
return redirect('/recover')->withErrors('找回密碼鏈接已發(fā)送到郵箱撕阎,請到郵箱中點擊鏈接找回密碼');
} catch (\Exception $e) {
return redirect('/recover')->withErrors($e->getMessage());
}
}
}
/**
* 重置密碼頁面
*/
public function revise($guid, $token)
{
//驗證session中是否有數(shù)據(jù)
if (empty(session())) return redirect('/login');
//驗證數(shù)據(jù)
$data = ['guid' => $guid, 'token' => $token];
$validator = \Validator::make($data,[
'guid' => 'required|string|size:32',
'token' => 'required|string|size:50',
]);
if ($validator->fails()) return redirect('/recover')->withErrors($validator);
//獲取session中的guid和token
$sessionGuid = session('tmpUser.guid');
$sessionToken = session('tmpUser.token');
//做對比
if ($guid == $sessionGuid && $token == $sessionToken) return view('mails.revisepw');
return redirect('/recover')->withErrors('數(shù)據(jù)不正確,請重新申請');
}
/**
* 重置密碼操作
*/
public function doRevise(Request $request)
{
$data = $request->only('password', 'token');
//驗證數(shù)據(jù)
$validator = \Validator::make($data, [
'password' => 'required|string|min:6|max:128',
'token' => 'required|string|size:50',
]);
if ($validator->fails()) return redirect('/recover')->withErrors($validator);
$sessionToken = session('tmpUser.token');
$sessionGuid = session('tmpUser.guid');
$data['email'] = session('tmpUser.email');
if ($data['token'] != $sessionToken) return redirect('/recover')->withErrors('數(shù)據(jù)不正確,請重新申請');
//把新密碼提交到數(shù)據(jù)庫
$uri = config('uri.remote') . '/comp/resetPassword/';
$res = ExtCurl::post($uri, $data);
//判斷服務(wù)器為空
if (empty($res)) return view('admin.errors.404')->withErrors(['status' => 500, 'msg' =>'服務(wù)器繁忙請稍后']);
if (empty($res['status']) || $res['status'] != 200) return redirect('/revise/' . $sessionGuid . '/' . $sessionToken)->withErrors($res);
return redirect('/login');
}
短信發(fā)送功能
發(fā)送短信功能
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者