request->post('username'); //用户名 $password = $this->request->post('password'); //密码 //$captcha = $this->request->post('captcha'); //图形验证码,如果输错了需要重新刷新验证码图片 if (empty($username) || empty($password)) { $this->fail(500, '参数校验错误'); } // if (!ThinkCaptcha::check($captcha, 'login', 0)) { // $this->fail(501, '验证码错误'); // } $data = Db::table('tb_user') ->where(['username' => $username]) ->where(['password' => md5($password)]) ->find(); if (!$data) { $this->fail(502, '用户名或密码错误'); } //检测是否激活 if ($data['state'] == 0) { $this->fail(503, '账号未激活,请前往您的邮箱地址:' . $data['email'] . ',进行验证通过~'); } //检测是否被封号 if ($data['state'] == 2) { $this->fail(504, '您的账号已被封停~'); } //更新登录信息 Db::table('tb_user') ->where(['id' => $data['id']]) ->update([ 'login_count' => $data['login_count'] + 1, 'last_login_ip' => $this->request->ip(), 'last_login' => time(), ]); //写登录日志 Db::table('tb_user_login_log') ->insert([ 'user_id' => $data['id'], 'ip' => $this->request->ip(), 'user_agent' => $this->request->header('user-agent'), 'created_at' => time(), ]); //写登录Token $token = Uuid::uuid4()->toString(); Db::table('tb_user_token') ->insert([ 'user_id' => $data['id'], 'token' => $token, 'expired_at' => time() + 24 * 60 * 60, ]); $this->success('success', [ 'token' => $token ]); } //登录 public function loginV2() { $username = $this->request->post('username'); //用户名 $password = $this->request->post('password'); //密码 //$captcha = $this->request->post('captcha'); //图形验证码,如果输错了需要重新刷新验证码图片 if (empty($username) || empty($password)) { $this->fail(500, '参数校验错误'); } // if (!ThinkCaptcha::check($captcha, 'login', 0)) { // $this->fail(501, '验证码错误'); // } $data = Db::table('tb_user') ->where(['username' => $username]) ->where(['password' => md5($password)]) ->find(); if (!$data) { $this->fail(502, '用户名或密码错误'); } //检测是否激活 if ($data['state'] == 0) { $this->fail(503, '账号未激活,请前往您的邮箱地址:' . $data['email'] . ',进行验证通过~'); } //检测是否被封号 if ($data['state'] == 2) { $this->fail(504, '您的账号已被封停~'); } //更新登录信息 Db::table('tb_user') ->where(['id' => $data['id']]) ->update([ 'login_count' => $data['login_count'] + 1, 'last_login_ip' => $this->request->ip(), 'last_login' => time(), ]); //写登录日志 Db::table('tb_user_login_log') ->insert([ 'user_id' => $data['id'], 'ip' => $this->request->ip(), 'user_agent' => $this->request->header('user-agent'), 'created_at' => time(), ]); //写登录Token $token = Uuid::uuid4()->toString(); Db::table('tb_user_token') ->insert([ 'user_id' => $data['id'], 'token' => $token, 'expired_at' => time() + 24 * 60 * 60, ]); $this->success('success', [ 'token' => $token ]); } //退出登录 public function logout() { $token = $this->request->post('token'); if (empty($token)) { $this->fail(500, 'token参数校验错误'); } Db::table('tb_user_token') ->where(['token' => $token]) ->delete(); $this->success('success', null); } //修改密码,修改成功后token会释放,需要转跳到登录页重新登录 public function changePwd() { $oldPassword = $this->request->post('oldPassword'); $newPassword = $this->request->post('newPassword'); if (empty($oldPassword) || empty($newPassword)) { $this->fail(500, '参数校验错误'); } $user = $this->getUser(); $data = Db::table('tb_user') ->where(['id' => $user->user_id]) ->where(['password' => md5($oldPassword)]) ->find(); if (!$data) { $this->fail(501, '当前密码错误'); } Db::table('tb_user') ->where(['id' => $user->user_id]) ->update([ 'password' => md5($newPassword), ]); //释放token,Token会立马失效 Db::table('tb_user_token') ->where(['token' => $user->token]) ->delete(); $this->success('success', null); } //获取系统自带头像 public function getSystemAvatar() { $data = Db::table('tb_system_avatar') ->order('id ASC') ->select(); $this->success('success', $data); } //获取注册验证码图片 public function getRegisterCaptchaImage() { return ThinkCaptcha::printImg('register'); //return Captcha::create('register'); } //获取登录验证码图片 public function getLoginCaptchaImage() { return ThinkCaptcha::printImg('login'); } //检查用户名是否存在(被注册) public function checkUsernameExists() { $username = $this->request->post('username'); if (empty($username)) { $this->fail('用户名不能为空'); } $exists = true; $user = Db::table('tb_user') ->where(['username' => $username]) ->find(); if (!$user) { $exists = false; } $this->success('success', [ 'isUsernameExists' => $exists, ]); } //注册 public function register() { $username = $this->request->post('username'); //用户名 $password = $this->request->post('password'); //密码 $avatar = $this->request->post('avatar'); //头像 $email = $this->request->post('email'); //邮箱账号 $captcha = $this->request->post('captcha'); //图形验证码,如果输错了需要重新刷新验证码图片 if (empty($username) || empty($password) || !is_numeric($avatar) || empty($email)) { $this->fail(500, '参数校验错误'); } // var_dump(Captcha::check($captcha, 'register')); // echo ThinkCaptcha::check($captcha, 'register', 0); // exit(); // if (!ThinkCaptcha::check($captcha, 'register')) { // $this->fail(501, '验证码错误'); // } $user = Db::table('tb_user') ->where(['username' => $username]) ->find(); if ($user) { $this->fail(502, '用户名已经被注册'); } //拦截邮箱是否被注册 $emailUser = Db::table('tb_user') ->where(['email' => $email]) ->find(); if ($emailUser) { $this->fail(503, '邮箱账号已经被使用过了~'); } Db::table('tb_user') ->insert([ 'username' => $username, //用户名 'password' => md5($password), //密码 'created_at' => time(), //创建时间 'last_login' => time(), //上次登录时间 'login_count' => 0, //登录次数 'avatar' => 'system://' . $avatar, //头像URL 'state' => 1, //状态 0:未激活 1:正常 2:封号 'balance' => 0, //余额 'email' => $email, //邮箱账号 'is_email_verified' => 0, //邮箱是否认证 0:未认证 1:已认证 'score' => 0, //积分数量 'gold' => 0, //金币数量 'exp' => 0, //经验值 ]); $this->success('success', null); } //取用户信息 public function getUserInfo() { $user = Db::table('tb_user') ->where(['id' => $this->getUser()->user_id]) ->find(); //解析获取头像url if (str_starts_with($user['avatar'], 'system://')) { $data = explode('//', $user['avatar']); $avatarId = intval($data[1]); $avatar = Db::table('tb_system_avatar') ->where(['id' => $avatarId]) ->find(); $user['avatar'] = $avatar['image_url']; } unset($user['password']); $this->success('success', $user); } //上传头像 public function uploadAvatar() { $file = $this->request->file('file'); if (empty($file)) { $this->fail(500, '上传的图片文件不能为空'); } // 使用验证器验证上传的文件 validate(['file' => [ // 限制文件大小(单位b),这里限制为2M 'fileSize' => 2 * 1024 * 1024, // 限制文件后缀,多个后缀以英文逗号分割 'fileExt' => 'jpg,jpeg,png', ]])->check(['file' => $file]); $saveName = \think\facade\Filesystem::disk('public')->putFile('avatar', $file); Db::table('tb_user') ->where(['id' => $this->getUser()->user_id]) ->update([ 'avatar' => $this->request->scheme() . '://' . $this->request->host() . '/storage/' . $saveName, ]); $this->success('success', [ 'filePath' => $this->request->scheme() . '://' . $this->request->host() . '/storage/' . $saveName, ]); } }