Ejemplo n.º 1
0
 public function settings()
 {
     if (!is_login()) {
         $this->error('请登录后再进行操作!', '/Home/User/login');
     }
     $upfolder = "uploads/";
     //文件上传目录
     if (!empty($_FILES)) {
         $tempfile = $_FILES['filedata']['tmp_name'];
         //文件临时目录
         $path_info = pathinfo($upfolder . $_FILES['filedata']['name']);
         $filename = session('user_auth_sign') . '.' . $path_info['extension'];
         $imagepath = $upfolder . $filename;
         $array = array();
         if (move_uploaded_file($tempfile, $upfolder . $filename)) {
             //将文件从临时目录移动到保存目录
             $array = array('status' => 1, 'src' => 'http://' . $_SERVER['HTTP_HOST'] . '/' . $upfolder . $filename);
             echo json_encode($array);
         } else {
             echo 0;
         }
         exit;
     }
     /*************按照坐标裁切图片********************/
     $action = $_GET['action'];
     if ($action == 'jcrop') {
         $crop = $_POST['crop'];
         if ($crop) {
             $targ_w = $targ_h = 100;
             $src = $crop['path'];
             //图片全地址
             $pathinfo = pathinfo($src);
             $filename = 'uploads/small' . $targ_w . '_' . $pathinfo['basename'];
             $img_r = imagecreatefromjpeg($src);
             //从url新建一图像
             $dst_r = imageCreateTrueColor($targ_w, $targ_h);
             //创建一个真色彩的图片源
             imagecopyresampled($dst_r, $img_r, 0, 0, $crop['x'], $crop['y'], $targ_w, $targ_h, $crop['w'], $crop['h']);
             imagejpeg($dst_r, $filename, 90);
             return 1;
         }
     }
     if (IS_POST) {
         $user = new UserApi();
         //执行用户信息修改
         /*
         type = 1 修改资料
         type = 2 修改密码
         */
         if (I('post.type') == 1) {
             $data = array('uid' => session('user_auth_sign'), 'sex' => I('post.sex') == 'man' ? 1 : 0, 'birthday' => I('post.birthday'), 'content' => I('post.content'));
             if ($user->user_update($data)) {
                 $this->success('修改成功!', U('/Home/User/settings'));
             } else {
                 $this->error('修改失败!', U('/Home/User/settings'));
             }
         } elseif (I('post.type') == 2) {
             $user_info = M('user')->where('uid=%d', session('user_auth_sign'))->find();
             if ($user_info['password'] != md5(I('post.oldpw'))) {
                 $this->error('原密码错误!');
                 return;
             }
             $data = array('uid' => session('user_auth_sign'), 'password' => md5(I('newpw')));
             if ($user->user_update($data)) {
                 $this->success('修改成功!', U('/Home/User/settings'));
             } else {
                 $this->error('修改失败!', U('/Home/User/settings'));
             }
         }
     } else {
         //获取用户信息
         $user = M('user');
         //实例化user数据表
         $user_info = $user->where('uid=%d', session('user_auth_sign'))->find();
         $this->assign('user_info', $user_info);
         $this->display();
     }
 }