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

<?php
namespace api\logic;
use backend\modules\shop\models\ars\Comment;
use Yii;
use yii\base\Component;
use yii\web\BadRequestHttpException;
use yii\web\ServerErrorHttpException;
/**
* @author iron
* @email weiriron@gmail.com
* @package api\logic
*/
class CommentLogic extends Component
{
public $viewAction = 'view';
/**
* @return Comment
* @throws BadRequestHttpException
* @throws ServerErrorHttpException
*
*/
public function create()
{
$content = Yii::$app->request->getBodyParam('content');
$images = Yii::$app->request->getBodyParam('images');
if (empty($content)) {
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
}
$comment = new Comment();
$comment->user_id = Yii::$app->user->getId();
$comment->nickname = Yii::$app->user->identity->nickname;
$comment->avatar = Yii::$app->user->identity->avatar;
if (!$comment->save()) {
throw new ServerErrorHttpException('服务器保存评论失败');
}
if (!empty($images) && is_array($images)) {
foreach ($images as $image) {
$data = Helper::uploadImage('content/', $image);
Helper::saveFileMsg($data, $comment->id, 4);
}
}
Helper::createdResponse($comment, $this->viewAction);
return $comment;
}
}