laravel网站开发实例----4

用户注册接口全实现

上一篇博客实现了发邮件验证码的功能,现在来写整个注册的逻辑
当用户发起注册的时候,给用户邮箱发送验证码,并存储到缓存中,当用户注册的时候,给验证码和验证码对应的Key即可注册成功,避免暴力破解。
两个接口的代码
1、用户得到验证码



namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Mail;
class UsersController extends Controller
{
    //用邮件发送验证码,并存储到缓存中
    public function store(Request $request)
    {
        $this->validate($request,[
            'email' => 'required|unique:users',
        ]);
        // 生成4位随机数,左侧补0
        $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
        $email = $request->email;
        //发送邮件验证码
        $name = $request->name;
        $flag = Mail::send('test',['name'=>$name,'code'=>$code],function($message) use ($email){
            $to = $email;
            $message ->to($to)->subject('验证码');
        });
        $info = "邮件已发送,如长时间没收到邮件,请重试";

        $key = 'RegisterCodes'.str_random(15);
        $expiredAt = now()->addMinutes(10);
        // 缓存验证码 10分钟过期。
        \Cache::put($key, ['email' => $request->email, 'code' => $code], $expiredAt);

        return $this->response->array([
            'key' => $key,
            'expired_at' => $expiredAt->toDateTimeString(),
            'info' => $info,
        ])->setStatusCode(201);
    }
}

2、根据验证码注册



namespace App\Http\Controllers\Api;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UsersRegisterController extends Controller
{
    //
    public function store(Request $request){
        $this->validate($request,[
           'schoolid' => 'required|unique:users|max:10',
            'email' => 'required|unique:users',
            'password' => 'required|min:8|max:16',
        ]);
        $verifyData = \Cache::get($request->verification_key);
        if (!$verifyData) {
            return response('验证码失效',200);
        }
        $code = $request->verification_code;
        if (hash_equals($verifyData['code'],$code)){
            //如果验证成功,就将所有信息存入数据库
            $user = User::create([
                'name' => $request->name,
                'schoolid' =>$request->schoolid,
                'email' => $verifyData['email'],
                'password' => bcrypt($request->password),
                'sex' =>$request->sex,
                'xueyuan'=>$request->xueyuan,
                'zhuanye'=>$request->zhuanye,
                'shifouhuiyuan' => 0,
            ]);
            \Cache::forget($request->verification_key);
            return $this->response->created();
        }else{
            $rs['info'] = '验证码错误';
            $rs['status'] = '400';
            return response($rs,200);
        }
    }
}

这里说一下,当使用模型类的时候,直接使用 模型类:: 例如,创建新的用户

$user = User::create([
    'name' => $request->name,
    'schoolid' =>$request->schoolid,
    'email' => $verifyData['email'],
    'password' => bcrypt($request->password),
    'sex' =>$request->sex,
    'xueyuan'=>$request->xueyuan,
    'zhuanye'=>$request->zhuanye,
    'shifouhuiyuan' => 0,
]);

使用缓存的方法:

//存储到缓存中
\Cache::put($key, ['email' => $request->email, 'code' => $code],
//从缓存中取出
\Cache::get($request->verification_key);
//从缓存中清除
\Cache::forget($request->verification_key);

3、路由设置

$api = app('Dingo\Api\Routing\Router');

$api->version('v1', [
    'namespace' => 'App\Http\Controllers\Api'
], function($api) {
    //短信验证码
    $api->post('registerCodes', 'UsersController@store')
        ->name('api.registerCodes.store');
    //用户注册
    $api->post('users','UsersRegisterController@store')
        ->name('api.users.store');
});

下一讲 用户管理系统接口全实现

你可能感兴趣的:(laravel,laravel实战)