Commit 3c29764a authored by xiabin's avatar xiabin

添加评论收藏接口

parent dd2a8a55
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace common\models; namespace common\models;
use Yii; use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/** /**
* This is the model class for table "comments". * This is the model class for table "comments".
...@@ -25,13 +27,29 @@ class Comments extends \yii\db\ActiveRecord ...@@ -25,13 +27,29 @@ class Comments extends \yii\db\ActiveRecord
return 'comments'; 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 * @inheritdoc
*/ */
public function rules() public function rules()
{ {
return [ 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'], [['user_id', 'course_id', 'parent_id', 'created_at'], 'integer'],
[['content'], 'string'], [['content'], 'string'],
[['ip'], 'string', 'max' => 32], [['ip'], 'string', 'max' => 32],
......
...@@ -37,7 +37,7 @@ class HomeController extends \yii\web\Controller ...@@ -37,7 +37,7 @@ class HomeController extends \yii\web\Controller
'allow' => TRUE, 'allow' => TRUE,
'actions' => ['comments'], 'actions' => ['comments'],
'roles' => ['@'], 'roles' => ['@'],
'verbs' => ['GET'] 'verbs' => ['POST']
], ],
], ],
] ]
...@@ -51,8 +51,6 @@ class HomeController extends \yii\web\Controller ...@@ -51,8 +51,6 @@ class HomeController extends \yii\web\Controller
} }
public $enableCsrfValidation = FALSE;
/** /**
* 点赞或者取消点赞 * 点赞或者取消点赞
* @param $coursewareId * @param $coursewareId
...@@ -121,16 +119,21 @@ class HomeController extends \yii\web\Controller ...@@ -121,16 +119,21 @@ class HomeController extends \yii\web\Controller
*/ */
public function actionComments() public function actionComments()
{ {
$this->enableCsrfValidation = FALSE;
$request = Yii::$app->request; $request = Yii::$app->request;
if ($request->isAjax) { if ($request->isAjax) {
$parentId = $request->post('parent-id'); $parentId = $request->post('parent-id');
$comments = Html::encode($request->post('comments')); $content = Html::encode($request->post('content'));
$courseId = $request->post('course-id'); $courseId = $request->post('course-id');
//验证数据是否正常
if ($parent = Comments::existById($courseId) or //0代表为一级评论不需要验证
Course::existById($courseId) or if ($parentId == 0) {
$courseId === NULL $parentStatus = TRUE;
} else {
$parentStatus = Comments::existById($parentId);
}
if ($parentStatus and
Course::existById($courseId) and
$content !== NULL
) { ) {
$userId = Yii::$app->user->identity->getId(); $userId = Yii::$app->user->identity->getId();
...@@ -138,9 +141,10 @@ class HomeController extends \yii\web\Controller ...@@ -138,9 +141,10 @@ class HomeController extends \yii\web\Controller
$comments->user_id = $userId; $comments->user_id = $userId;
$comments->parent_id = $parentId; $comments->parent_id = $parentId;
$comments->course_id = $courseId; $comments->course_id = $courseId;
$comments->content = $content;
$comments->ip = Utils::getRealIP(); $comments->ip = Utils::getRealIP();
$result = $comments->save(); $result = $comments->save();
Utils::ajaxResponse($result); Utils::ajaxResponse($result, ($result === FALSE) ? $comments->getErrors() : NULL);
} else { } else {
throw new InvalidParamException('参数错误'); 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