基于thinkPHP6框架的Ajax登录和图片水印

登录

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');
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值