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.

52 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. * Class CartLogic
  12. * @package api\logic
  13. */
  14. class CommentLogic extends Component
  15. {
  16. public $viewAction = 'view';
  17. /**
  18. * @return Comment
  19. * @throws BadRequestHttpException
  20. * @throws ServerErrorHttpException
  21. *
  22. */
  23. public function create()
  24. {
  25. $content = Yii::$app->request->getBodyParam('content');
  26. $images = Yii::$app->request->getBodyParam('images');
  27. if (empty($content)) {
  28. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  29. }
  30. $comment = new Comment();
  31. $comment->user_id = Yii::$app->user->getId();
  32. $comment->nickname = Yii::$app->user->identity->nickname;
  33. $comment->avatar = Yii::$app->user->identity->avatar;
  34. if (!$comment->save()) {
  35. throw new ServerErrorHttpException('服务器保存评论失败');
  36. }
  37. if (!empty($images) && is_array($images)) {
  38. foreach ($images as $image) {
  39. $data = Helper::uploadImage('content/', $image);
  40. Helper::saveFileMsg($data, $comment->id, 4);
  41. }
  42. }
  43. Helper::createdResponse($comment, $this->viewAction);
  44. return $comment;
  45. }
  46. }