Commit 3c29764a authored by xiabin's avatar xiabin

添加评论收藏接口

parent dd2a8a55
......@@ -3,6 +3,8 @@
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/**
* This is the model class for table "comments".
......@@ -25,13 +27,29 @@ class Comments extends \yii\db\ActiveRecord
return 'comments';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
],
// if you're using datetime instead of UNIX timestamp:
// 'value' => new Expression('NOW()'),
],];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'course_id', 'parent_id', 'content', 'ip', 'created_at'], 'required'],
[['user_id', 'course_id', 'parent_id', 'content', 'ip'], 'required'],
[['user_id', 'course_id', 'parent_id', 'created_at'], 'integer'],
[['content'], 'string'],
[['ip'], 'string', 'max' => 32],
......
......@@ -37,7 +37,7 @@ class HomeController extends \yii\web\Controller
'allow' => TRUE,
'actions' => ['comments'],
'roles' => ['@'],
'verbs' => ['GET']
'verbs' => ['POST']
],
],
]
......@@ -51,8 +51,6 @@ class HomeController extends \yii\web\Controller
}
public $enableCsrfValidation = FALSE;
/**
* 点赞或者取消点赞
* @param $coursewareId
......@@ -121,16 +119,21 @@ class HomeController extends \yii\web\Controller
*/
public function actionComments()
{
$this->enableCsrfValidation = FALSE;
$request = Yii::$app->request;
if ($request->isAjax) {
$parentId = $request->post('parent-id');
$comments = Html::encode($request->post('comments'));
$content = Html::encode($request->post('content'));
$courseId = $request->post('course-id');
//验证数据是否正常
if ($parent = Comments::existById($courseId) or
Course::existById($courseId) or
$courseId === NULL
//0代表为一级评论不需要验证
if ($parentId == 0) {
$parentStatus = TRUE;
} else {
$parentStatus = Comments::existById($parentId);
}
if ($parentStatus and
Course::existById($courseId) and
$content !== NULL
) {
$userId = Yii::$app->user->identity->getId();
......@@ -138,9 +141,10 @@ class HomeController extends \yii\web\Controller
$comments->user_id = $userId;
$comments->parent_id = $parentId;
$comments->course_id = $courseId;
$comments->content = $content;
$comments->ip = Utils::getRealIP();
$result = $comments->save();
Utils::ajaxResponse($result);
Utils::ajaxResponse($result, ($result === FALSE) ? $comments->getErrors() : NULL);
} else {
throw new InvalidParamException('参数错误');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment