登录
public function loginCheck(Request $request){ /** * 先验证是否重复登录 */ try { $check = $request->checkToken('__token__'); if (false === $check){ throw new \Exception('不可重复登录',201); } }catch(\Exception $e){ return json([ 'code'=>$e->getCode(), 'msg'=>$e->getMessage(), 'data'=>[] ]); } $user = $request->all(); try { validate(LoginValidate::class)->batch(true)->check($user); if (!captcha_check($user['captcha'])){ throw new ValidateException(['captcha'=>'验证码错误']); } $username = $user['username']; $password = $user['password']; $data = Db::table('user')->where('username',$username)->find(); if ($data){ if ($data['password'] == md5($password)){ Session::set('username',$username); Log::write("用户".$username."登录成功",'info'); return json([ 'code'=>200, 'msg'=>'登陆成功', 'data'=>[], ]); }else{ throw new ValidateException(['password'=>'密码错误']); } }else{ throw new ValidateException(['username'=>'用户名错误']); } }catch (ValidateException $e){ return json([ 'code'=>$e->getCode(), 'msg' =>$e->getError(), 'data'=>[], ]); } } public function login(){ if (Session::has('username')){ return \redirect('article'); } return View::fetch('/login'); } /** * 显示资源列表 * * @return \think\Response */
水印
public function save(Request $request,Category $category,\app\admin\model\Article $article) { if (!Session::has('username')){ return View::fetch('/login'); } $data = $request->all(); if (!array_key_exists('article_avatar',$data)){ $data['article_avatar'] = ''; } if (!array_key_exists('article_status',$data)){ $data['article_status'] = ''; } if (!array_key_exists('article_recommend',$data)){ $data['article_recommend'] = ''; } $data['article_title'] = trim($data['article_title'],' '); try { validate(ArticleValidate::class)->batch(true)->check($data); /** * 开始给图片生成缩略图,添加文字水印 */ if (!file_exists('./uploads/'.date('Ymd'))){ mkdir('./uploads/'.date('Ymd')); } $file = $_FILES['article_avatar']; $fileName = $file['name']; $suffix = substr($fileName,strrpos($fileName,'.')); $article_avatar = 'uploads/'.date('Ymd').'/'.md5(date('Ymd')).$suffix; $image = Image::open($data['article_avatar'])->thumb(200,200); $image = $image->water('./uploads/logo2.png',Image::WATER_SOUTHEAST,30)->text('柴少出品',getcwd().'/uploads/1.ttf',10,'#ff0000')->save($article_avatar); $data['article_avatar'] = $article_avatar; $data['create_time'] = strtotime(date('Ymd')); $res = $article::saveOne($data); if (!$res){ Log::write('插入失败','error'); } Log::write('插入成功','info'); return \redirect('article'); }catch (ValidateException $exception){ $cate = $category->getAll(); View::assign(['errors'=>$exception->getError(),'cate'=>$cate]); return View::fetch('article_add'); } }