Exemplo n.º 1
0
 /**
  * 添加日游媒体
  */
 public function ActionAddTripMedia()
 {
     if (!empty($_GET['file_name']) && !empty($_GET['id'])) {
         // 获取 文件名、日游ID、文件标题、文件描述
         $trip_id = $_GET['id'];
         $file_name = $_GET['file_name'];
         $file_title = empty($_GET['file_title']) ? '' : $_GET['file_title'];
         $file_description = empty($_GET['file_description']) ? '' : $_GET['file_description'];
         // 根据日游ID查询日游名称
         $trip_name = Trip::model()->findByPk($trip_id);
         // 根据日游名称生成日游文件夹 [riyou]
         $forder_name = implode('', IconvEncode::gbk2Pinyin($trip_name['TripName']));
         // 根据文件后缀名生成媒体文件夹 [riyou/video/]
         $suffix = trim(strtolower(strrchr($file_name, '.')), '.');
         if (in_array($suffix, Params::$picture)) {
             $file_type = 1;
             $forder_name .= '/picture/';
         } else {
             if (in_array($suffix, Params::$video)) {
                 $file_type = 2;
                 $forder_name .= '/video/';
             } else {
                 exit('文件类型错误!');
             }
         }
         // 生成完整的文件上传目录 [uploads/brs/files/TripMedia/riyou/video/]
         $file_path = FILE_PATH . 'TripMedia/' . $forder_name;
         // 创建目录
         DirectoryFile::dirCreate($file_path);
         // 验证目录是否创建成功
         if (is_dir($file_path)) {
             // 生成文件名 [1409734292_3197.jpg]
             $file_name = time() . '_' . rand(100, 10000) . '.' . $suffix;
             // 上传文件
             file_put_contents($file_path . $file_name, file_get_contents('php://input'));
             // 模型赋值
             $trip_model = new TripMedia();
             $trip_model->TripId = $trip_id;
             $trip_model->FileTitle = $file_title;
             $trip_model->FileDescription = $file_description;
             $trip_model->FileUrl = $file_path . $file_name;
             $trip_model->FileType = $file_type;
             // 将文件信息存入数据库
             if ($trip_model->save()) {
                 echo $file_path . $file_name;
             } else {
                 exit('信息保存失败!');
             }
         } else {
             exit('目录创建失败!');
         }
     } else {
         $trip_model = new TripMedia();
         // 跳转至添加页
         $this->render('add_trip_media', array('trip_media_model' => $trip_model, 'trip_id' => Yii::app()->request->getParam('trip_id')));
     }
 }
 /**
  * 增加图片类型页
  */
 public function ActionAddPictureCategory()
 {
     $picture_category_model = new PictureCategory();
     $valid = array('无效', '有效');
     //有提交表单,则保存
     if (isset($_POST['PictureCategory'])) {
         $cat_pinyin_arr = IconvEncode::gbk2Pinyin($_POST['PictureCategory']['PictureCategoryName']);
         $cat_pinyin = implode("", $cat_pinyin_arr);
         $_POST['PictureCategory']['PictureCategoryPinyin'] = $cat_pinyin;
         $picture_category_model->attributes = $_POST['PictureCategory'];
         $picture_category_model->ParentId = empty($_POST['PictureCategory']['ParentId']) ? '0' : $_POST['PictureCategory']['ParentId'];
         if ($picture_category_model->save()) {
             Yii::app()->user->setFlash('save_pic', '添加成功');
             $this->redirect(Yii::app()->createUrl('PictureCategory/ListPictureCategory'));
         } else {
             Yii::app()->user->setFlash('save_pic', '添加失败');
         }
     }
     // 查询所有一级菜单
     $picture_category = PictureCategory::model()->findAllByAttributes(array('ParentId' => 0));
     // 跳转至添加页
     $this->renderPartial('add_picture_category', array('picture_category_model' => $picture_category_model, 'picture_category' => $picture_category, 'valid' => $valid));
 }
Exemplo n.º 3
0
 /**
  * 添加文件
  * 参数格式:{"FileName":"文件名.后缀名","FileClassify":"文件分类ID","FileStream":"MIME base64格式编码"} 键名区分大小写
  * FileName:必填
  * FileStream:必填
  * FileClassify:必填 [文件分类ID必须包含在文件分类ID的枚举中]
  * @return string 保存成功返回[原始文件名+相对路径+接口返回状态] 失败返回[原始文件名+相对路径+状态+错误代码]
  * 错误代码说明:[1.JSON参数错误 / 2.文件分类ID未包含在文件分类ID的枚举中 / 3.上传了不支持的文件类型 / 4.JSON参数的键名错误或缺少必填参数 / 5.文件上传失败 / 6.文件信息保存失败 / 7.接口异常]
  */
 public function ActionSaveFile()
 {
     $json_data = array();
     // 返回的JSON数据
     $json_params = Yii::app()->request->getParam('json_params');
     // 获取JSON参数
     $json_key = array('filename', 'filestream', 'fileclassify');
     // 正确的JSON键名
     try {
         if (!empty($json_params) && IconvEncode::IsJson($json_params)) {
             // 解析JSON并将键名全部转为小写
             $file_data = array_change_key_case(json_decode($json_params, true));
             // 验证数据正确性与完整性
             foreach ($file_data as $key => $val) {
                 $file_data[$key] = trim($val);
                 // fileclassify必须为数字且必须包含在文件分类ID的枚举中
                 if ($key == 'fileclassify' && (!is_numeric($val) || !array_key_exists($file_data['fileclassify'], $this->file_classify))) {
                     exit(json_encode(self::CreateErrorMsg(2)));
                     // 文件分类ID未包含在文件分类ID的枚举中
                 }
                 // 验证文件类型
                 if ($key == 'filename') {
                     $suffix = strtolower(trim(strrchr($file_data['filename'], '.'), '.'));
                     // 获取文件后缀名
                     if (!in_array($suffix, $this->file_type)) {
                         exit(json_encode(self::CreateErrorMsg(3)));
                         // 上传了不支持的文件类型
                     }
                 }
             }
             $file_data = array_filter($file_data);
             // 去空格与去空值后的JSON数据数组
             $num = count(array_diff(array_keys($file_data), $json_key));
             // 判断JSON的键是否正确
             // 验证通过
             if ($num === 0) {
                 // 生成并创建上传路径
                 $upload_path = in_array($file_data['fileclassify'], $this->secret_classify) ? SECRET_FILE_PATH : FILE_PATH;
                 $folder_name = implode('', IconvEncode::gbk2Pinyin($this->file_classify[$file_data['fileclassify']]));
                 // 生成文件分类的文件夹名称
                 $file_path = $upload_path . $folder_name . '/' . date('Y-m-d', time()) . '/';
                 DirectoryFile::dirCreate($file_path);
                 // 检测目录是否存在
                 if (!file_exists($file_path)) {
                     exit(json_encode(self::CreateErrorMsg(5)));
                     // 文件上传失败
                 }
                 // 生成文件名 格式:【文件分类_时间戳_随机数 / qianzheng_1407315015_4189.doc】
                 $file_new_name = $folder_name . '_' . time() . '_' . rand(100, 10000) . strrchr($file_data['filename'], '.');
                 // 上传文件
                 file_put_contents($file_path . $file_new_name, base64_decode(str_replace(' ', '+', $file_data['filestream'])));
                 // 填充模型
                 $file_model = new File();
                 $file_model->FileNewName = $file_new_name;
                 $file_model->FileName = $file_data['filename'];
                 $file_model->FilePath = $file_path . $file_new_name;
                 $file_model->FileClassify = $file_data['fileclassify'];
                 $file_model->CreateTime = date('Y-m-d', time());
                 // 将文件信息保存至数据库并返回Json数据 [原始文件名.后缀名 + 相对路径]
                 if ($file_model->save()) {
                     $json_data['FileName'] = $file_data['filename'];
                     $json_data['FilePath'] = str_replace('uploads/', '', $file_path) . $file_new_name;
                     $json_data['IsError'] = 'false';
                     echo json_encode($json_data);
                 } else {
                     exit(json_encode(self::CreateErrorMsg(6)));
                     // 文件信息保存失败
                 }
             } else {
                 exit(json_encode(self::CreateErrorMsg(4)));
                 // JSON参数的键名错误或缺少必填参数
             }
         } else {
             exit(json_encode(self::CreateErrorMsg(1)));
             // JSON参数错误
         }
     } catch (Exception $e) {
         exit(json_encode(self::CreateErrorMsg(7)));
         // 接口异常
     }
 }
Exemplo n.º 4
0
 /**
  *保存图片页
  */
 public function ActionSavePicture()
 {
     //获取参数
     $city_id = isset($_GET['city_id']) ? $_GET['city_id'] : "";
     $create_thumbnails_pic = isset($_GET['create_thumbnails_pic']) ? $_GET['create_thumbnails_pic'] : "";
     $picture_category_id = isset($_GET['picture_category_id']) ? $_GET['picture_category_id'] : "";
     $picture_name = isset($_GET['picture_name']) ? $_GET['picture_name'] : "";
     $picture_title = isset($_GET['picture_title']) ? $_GET['picture_title'] : "";
     $picture_description = isset($_GET['picture_description']) ? $_GET['picture_description'] : "";
     //解析图片名称和扩展名
     $name_noext = substr($picture_name, 0, strrpos($picture_name, '.'));
     //名称(无扩展名)
     $name_ext = strrchr($picture_name, ".");
     //扩展名
     //将图片中文名称转换为拼音
     $name_noext_arr = IconvEncode::gbk2Pinyin($name_noext);
     $name_noext_pinyin = implode("", $name_noext_arr);
     //中、小图名称
     $picture_middle_name = $name_noext_pinyin . "_middle" . $name_ext;
     //中规格图名称
     $picture_samll_name = $name_noext_pinyin . "_samll" . $name_ext;
     //小规格图名称
     //获取图片类型
     $picture_category = PictureCategory::model()->findByPk($picture_category_id);
     if (isset($picture_category) && !empty($picture_category)) {
         if (isset($picture_category['PictureCategoryPinyin']) && !empty($picture_category['PictureCategoryPinyin'])) {
             //创建图片路径
             $pic_dir_path = IMAGES_PATH . $picture_category['PictureCategoryPinyin'] . '/' . date("Y-m") . '/';
             DirectoryFile::dirCreate($pic_dir_path);
             //大图上传
             $pic_big_src = $pic_dir_path . $name_noext_pinyin . $name_ext;
             //上传原始图片并生产规定格式的中图和小图
             file_put_contents($pic_big_src, file_get_contents('php://input'));
             //存放大图
             //图片信息存入数据库
             if (empty($picture_title)) {
                 $picture_title = $name_noext;
             }
             $picture_model = new Picture();
             $picture_model->CityId = $city_id;
             $picture_model->CategoryId = $picture_category_id;
             $picture_model->Name = $picture_name;
             $picture_model->Title = $picture_title;
             $picture_model->Description = $picture_description;
             $picture_model->save();
             //图片中、小图片路径保存到数据库
             $pic_id = $picture_model->attributes['ID'];
             if (isset($pic_id) && !empty($pic_id)) {
                 $picture_source['big'] = new PictureSource();
                 $picture_source['big']->PicId = $pic_id;
                 $picture_source['big']->PicSrc = $pic_big_src;
                 $picture_source['big']->SizeType = 0;
                 //原图
                 // 判断是否生成中、小图
                 if ($create_thumbnails_pic) {
                     $pic_middle_src = $pic_dir_path . $picture_middle_name;
                     $pic_small_src = $pic_dir_path . $picture_samll_name;
                     ImageChange::imgResize($pic_big_src, $pic_middle_src, 380, 210);
                     //生成中图
                     ImageChange::imgResize($pic_big_src, $pic_small_src, 180, 120);
                     //生成小图
                     $picture_source['middle'] = new PictureSource();
                     $picture_source['middle']->PicId = $pic_id;
                     $picture_source['middle']->PicSrc = $pic_middle_src;
                     $picture_source['middle']->SizeType = 1;
                     //中图
                     $picture_source['small'] = new PictureSource();
                     $picture_source['small']->PicId = $pic_id;
                     $picture_source['small']->PicSrc = $pic_small_src;
                     $picture_source['small']->SizeType = 2;
                     //小图
                 }
                 //事务[S]
                 $transaction = Yii::app()->db->beginTransaction();
                 //开启事务
                 try {
                     foreach ($picture_source as $pic_src) {
                         if (!$pic_src->save()) {
                             throw new Exception();
                         }
                     }
                     $transaction->commit();
                     //提交事务
                 } catch (Exception $e) {
                     $transaction->rollBack();
                     //回滚事务
                 }
                 //事务[S]
             }
             echo $pic_dir_path;
         }
     }
 }
Exemplo n.º 5
0
 /**
  * 添加酒店图片
  */
 public function ActionAddHotelPic()
 {
     $isPost = Yii::app()->request->isPostRequest;
     if (empty($isPost)) {
         // 获取酒店ID
         $hotel_id = intval(trim(Yii::app()->request->getParam('hotel_id')));
         // 根据酒店ID 获取酒店数据
         if (!empty($hotel_id)) {
             $hotel_model = Hotel::model()->findByPk($hotel_id);
             $this->render('add_hotel_picture', array('hotel_model' => $hotel_model));
         } else {
             Yii::app()->user->setFlash('参数错误,请刷新后重试');
         }
     } else {
         // 生成图的名称(大,中,小图)[S]
         $picture_name = isset($_GET['picture_name']) ? $_GET['picture_name'] : "";
         $picture_title = isset($_GET['picture_title']) ? $_GET['picture_title'] : "";
         $picture_desc = isset($_GET['picture_description']) ? $_GET['picture_description'] : "";
         // 解析图片名称和扩展名
         $name_noext = substr($picture_name, 0, strrpos($picture_name, '.'));
         //名称(无扩展名)
         $name_ext = strrchr($picture_name, ".");
         //扩展名
         $name_rand = md5(time() . mt_rand(1, 1000000));
         // 将图片中文名称转换为拼音
         $name_noext_arr = IconvEncode::gbk2Pinyin($name_noext);
         $name_noext_pinyin = implode("", $name_noext_arr) . $name_rand;
         // 中、小图名称
         $picture_middle_name = $name_noext_pinyin . "_middle" . $name_ext;
         //中规格图名称
         $picture_samll_name = $name_noext_pinyin . "_samll" . $name_ext;
         //小规格图名称
         // 生成图的名称(大,中,小图)[E]
         $hotel_id = Yii::app()->request->getParam('hotel_id');
         // 酒店ID
         $hotel_count = Hotel::model()->count('ID=:id', array(':id' => $hotel_id));
         if ($hotel_count > 0) {
             // 判断该酒店是否存在
             // 创建图片路径
             $pic_dir_path = IMAGES_PATH . 'hotel' . '/' . date("Y-m") . '/';
             DirectoryFile::dirCreate($pic_dir_path);
             // 大、中、小图上传
             $pic_big_src = $pic_dir_path . $name_noext_pinyin . $name_ext;
             $pic_middle_src = $pic_dir_path . $picture_middle_name;
             $pic_small_src = $pic_dir_path . $picture_samll_name;
             // 上传原始图片并生产规定格式的中图和小图
             file_put_contents($pic_big_src, file_get_contents('php://input'));
             // 存放大图
             ImageChange::imgResize($pic_big_src, $pic_middle_src, 380, 210);
             // 生成中图
             ImageChange::imgResize($pic_big_src, $pic_small_src, 180, 120);
             // 生成小图
             // 图片信息存入数据库
             if (empty($picture_title)) {
                 $picture_title = $name_noext;
             }
             // 图片路径保存到数据库
             $HotelPicture['big'] = new HotelPicture();
             $HotelPicture['big']->PicSrc = $pic_big_src;
             $HotelPicture['big']->PicTitle = $picture_title;
             $HotelPicture['big']->PicDes = $picture_desc;
             $HotelPicture['big']->Hotel_Id = $hotel_id;
             $HotelPicture['big']->SizeType = 0;
             $HotelPicture['middle'] = new HotelPicture();
             $HotelPicture['middle']->PicSrc = $pic_middle_src;
             $HotelPicture['middle']->PicTitle = $picture_title;
             $HotelPicture['middle']->PicDes = $picture_desc;
             $HotelPicture['middle']->Hotel_Id = $hotel_id;
             $HotelPicture['middle']->SizeType = 1;
             $HotelPicture['small'] = new HotelPicture();
             $HotelPicture['small']->PicSrc = $pic_small_src;
             $HotelPicture['small']->PicTitle = $picture_title;
             $HotelPicture['small']->PicDes = $picture_desc;
             $HotelPicture['small']->Hotel_Id = $hotel_id;
             $HotelPicture['small']->SizeType = 2;
             //事务[S]
             $transaction = Yii::app()->db->beginTransaction();
             //开启事务
             try {
                 foreach ($HotelPicture as $pic_src) {
                     if (!$pic_src->save()) {
                         throw new Exception();
                     }
                 }
                 $transaction->commit();
                 //提交事务
             } catch (Exception $e) {
                 $transaction->rollBack();
                 //回滚事务
             }
             //事务[E]
             echo $pic_dir_path;
         }
     }
 }