Lumen5.1 使用Mail邮件且找回密码功能

发邮件

Lumen5.1 使用Mail邮件

找回密码

  1. 路由
//找回密码
    $app->get('/recover','CompController@recoverPage');
    $app->post('/recover','CompController@recover');

    //重置密码
    $app->get('/revise/{guid}/{token}','CompController@revise');
    $app->post('/revise','CompController@doRevise');
  1. 控制器
  • 找回密码并发送邮件
     /**
     * 找回密码页
     * @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');
        // 验证数据
        $validator = \Validator::make($data, ['email' => 'required|email']);
        if ($validator->fails()) return redirect('/recover')->withErrors($validator);
        // 判断数据库中是否存在email
        $uri = config('uri.remote') . '/comp/forgetPassword/' . $data['email'];
        $res = ExtCurl::post($uri);
        // 判断服务器为空
        if (empty($res)) return view('admin.errors.404')->withErrors(['status' => 500, 'msg' => '服务器繁忙请稍候']);
        if (empty($res['status']) || $res['status'] != 200) return view('admin.errors.404')->withErrors($res);
        // 判断是否是禁用状态
        if ($res['data']['status'] ==1) {
            // 如果等于1,说明不是禁用状态
            // 存储session
            session(['tmpUser' => $res['data']]);
            // 发邮件
            $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('找回密码链接已发送到邮箱,请到邮箱中点击链接找回密码');
            } catch (\Exception $e) {
                return redirect('/recover')->withErrors($e->getMessage());

            }
        }

    }
  • 重置密码
     /**
     * 重置密码页面
     */
    public function revise($guid, $token)
    {
        //验证session中是否有数据
        if (empty(session())) return redirect('/login');

        //验证数据
        $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('数据不正确,请重新申请');
    }

    /**
     * 重置密码操作
     */
    public function doRevise(Request $request)
    {
        $data = $request->only('password', 'token');
        //验证数据
        $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('数据不正确,请重新申请');

        //把新密码提交到数据库
        $uri = config('uri.remote') . '/comp/resetPassword/';
        $res = ExtCurl::post($uri, $data);
        //判断服务器为空
        if (empty($res)) return view('admin.errors.404')->withErrors(['status' => 500, 'msg' =>'服务器繁忙请稍后']);
        if (empty($res['status']) || $res['status'] != 200) return redirect('/revise/' . $sessionGuid . '/' . $sessionToken)->withErrors($res);
        return redirect('/login');
    }

短信发送功能

发送短信功能

你可能感兴趣的:(Lumen5.1 使用Mail邮件且找回密码功能)