You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.4 KiB

  1. <?php
  2. namespace api\logic;
  3. use backend\modules\shop\models\ars\Comment;
  4. use Yii;
  5. use yii\base\Component;
  6. use yii\web\BadRequestHttpException;
  7. use yii\web\ServerErrorHttpException;
  8. /**
  9. * @author iron
  10. * @email weiriron@gmail.com
  11. * @package api\logic
  12. */
  13. class CommentLogic extends Component
  14. {
  15. public $viewAction = 'view';
  16. /**
  17. * @return Comment
  18. * @throws BadRequestHttpException
  19. * @throws ServerErrorHttpException
  20. *
  21. */
  22. public function create()
  23. {
  24. $content = Yii::$app->request->getBodyParam('content');
  25. $images = Yii::$app->request->getBodyParam('images');
  26. if (empty($content)) {
  27. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  28. }
  29. $comment = new Comment();
  30. $comment->user_id = Yii::$app->user->getId();
  31. $comment->nickname = Yii::$app->user->identity->nickname;
  32. $comment->avatar = Yii::$app->user->identity->avatar;
  33. if (!$comment->save()) {
  34. throw new ServerErrorHttpException('服务器保存评论失败');
  35. }
  36. if (!empty($images) && is_array($images)) {
  37. foreach ($images as $image) {
  38. $data = Helper::uploadImage('content/', $image);
  39. Helper::saveFileMsg($data, $comment->id, 4);
  40. }
  41. }
  42. Helper::createdResponse($comment, $this->viewAction);
  43. return $comment;
  44. }
  45. }