示例#1
0
 public function actionUploadPhoto()
 {
     if (Yii::$app->request->isPost) {
         $id = Yii::$app->request->post('sale_id');
         $path = Yii::$app->params['uploadSalePath'] . DIRECTORY_SEPARATOR . $id;
         BaseFileHelper::createDirectory($path);
         $file = UploadedFile::getInstanceByName('photos');
         if (!$file) {
             return false;
         }
         $model = new SalePhoto();
         $model->sale_id = $id;
         if ($model->save()) {
             $model->sort = $model->id;
             $name = $model->id . '.jpg';
             if ($model->save()) {
                 if (!$file->saveAs($path . DIRECTORY_SEPARATOR . $name)) {
                     $model->delete();
                 } else {
                     $model->hash = md5_file($path . DIRECTORY_SEPARATOR . $name);
                     $model->save();
                 }
             }
         }
         sleep(1);
         return true;
     }
     return false;
 }
示例#2
0
文件: Sale.php 项目: dench/resistor
 public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     // Photo copy - Start
     if ($insert && Yii::$app->request->post('photos')) {
         $new_path = Yii::$app->params['uploadSalePath'] . DIRECTORY_SEPARATOR . $this->id;
         BaseFileHelper::createDirectory($new_path);
         foreach (Yii::$app->request->post('photos') as $k => $v) {
             $old_photo = SalePhoto::findOne($k);
             if ($old_photo) {
                 $old = Yii::$app->params['uploadSalePath'] . DIRECTORY_SEPARATOR . $v . DIRECTORY_SEPARATOR . $old_photo->id . '.jpg';
                 if (file_exists($old)) {
                     $new_photo = new SalePhoto();
                     $new_photo->sale_id = $this->id;
                     if ($new_photo->save()) {
                         $new_photo->sort = $new_photo->id;
                         $new_photo->hash = md5_file($old);
                         if ($new_photo->save()) {
                             if (!copy($old, $new_path . DIRECTORY_SEPARATOR . $new_photo->id . '.jpg')) {
                                 $new_photo->delete();
                             }
                         }
                     }
                 }
             }
         }
     }
     // Photo copy - End
     $code = sprintf("%02d", $this->region_id) . sprintf("%03d", $this->district_id) . $this->id;
     Yii::$app->db->createCommand()->update('sale', ['code' => $code], ['id' => $this->id])->execute();
 }
示例#3
0
 public function actionPhoto()
 {
     Yii::beginProfile('action');
     $data = Yii::$app->request->post();
     if (empty($data['ParseImage']) || empty($data['id'])) {
         return false;
     }
     $parse = Parse::findOne($data['id']);
     if (empty($parse)) {
         return false;
     }
     //sleep(1);
     foreach ($data['ParseImage'] as $key => $url) {
         $url = @$url['url'];
         if (empty($key)) {
             $image = new ParseImage();
             $image->parse_id = $parse->id;
             $image->url = $url;
             Yii::beginProfile('get');
             if ($file = file_get_contents($url)) {
                 Yii::endProfile('get');
                 $path = Yii::$app->params['uploadSalePath'] . DIRECTORY_SEPARATOR . $parse->sale_id;
                 BaseFileHelper::createDirectory($path);
                 $photo = new SalePhoto();
                 $photo->sale_id = $parse->sale_id;
                 if ($photo->save()) {
                     $photo->sort = $photo->id;
                     $name = $photo->id . '.jpg';
                     if ($photo->save()) {
                         if (!file_put_contents($path . DIRECTORY_SEPARATOR . $name, $file)) {
                             $photo->delete();
                         } else {
                             $photo->hash = md5_file($path . DIRECTORY_SEPARATOR . $name);
                             $photo->save();
                             $image->photo_id = $photo->id;
                             $image->save();
                         }
                     }
                 }
             }
         } elseif (empty($url)) {
             $image = ParseImage::findOne($key);
             if (!empty($image)) {
                 if ($image->photo_id) {
                     $photo = SalePhoto::findOne($image->photo_id);
                     $photo->delete();
                 }
                 $image->delete();
             }
         }
     }
     Yii::endProfile('action');
     return true;
 }