Esempio n. 1
0
 /**
  *  头像设置
  * @return mixed
  */
 public function actionAvatar()
 {
     /** @var AvatarForm $model */
     $model = Yii::createObject(AvatarForm::className());
     if ($model->load(Yii::$app->request->post())) {
         if ($model->user->avatar) {
             // 删除头像
             $model->deleteImage();
         }
         $image = $model->uploadImage();
         $hasError = true;
         if ($image !== false) {
             $path = $model->getNewUploadedImageFile();
             if ($image->saveAs($path)) {
                 $hasError = false;
             }
         }
         if ($hasError) {
             $model->useDefaultImage();
         }
         if ($model->save() === false) {
             $hasError = true;
         }
         if ($hasError) {
             Yii::$app->session->setFlash('error', '您的头像更新失败');
         } else {
             Yii::$app->session->setFlash('success', '您的用户信息修改成功');
         }
         return $this->refresh();
     }
     return $this->render('avatar', ['model' => $model]);
 }
 /**
  *  头像设置
  * @return mixed
  */
 public function actionAvatar()
 {
     /** @var SettingsForm $model */
     $model = Yii::createObject(AvatarForm::className());
     if ($model->load(Yii::$app->request->post())) {
         if ($model->user->avatar) {
             // 删除头像
             $model->deleteImage();
         }
         $image = $model->uploadImage();
         if ($model->save()) {
             if ($image !== false) {
                 $path = $model->getImageFile();
                 $image->saveAs($path);
             }
             Yii::$app->session->setFlash('success', '您的用户信息修改成功');
             return $this->refresh();
         }
     }
     return $this->render('avatar', ['model' => $model]);
 }