Commit dd2a8a55 authored by xiabin's avatar xiabin

添加评论接口

parent 4627a05a
......@@ -28,8 +28,8 @@ class Utils
/**
* @param bool $status
* @param null $data
* @param null $messgae
* @param null $data
*/
public static function ajaxResponse($status = TRUE, $messgae = NULL, $data = NULL)
{
......
......@@ -53,4 +53,15 @@ class Comments extends \yii\db\ActiveRecord
'created_at' => 'Created At',
];
}
/**
* 根据id判断数据是否存在
* @param $id
* @return bool
*/
public static function existById($id)
{
$result = self::find()->select('id')->where('id=:id', [':id' => $id])->one();
return $result === NULL ? FALSE : TRUE;
}
}
......@@ -85,4 +85,15 @@ class Course extends \yii\db\ActiveRecord
'updated_at' => 'Updated At',
];
}
/**
* 根据id判断数据是否存在
* @param $id
* @return bool
*/
public static function existById($id)
{
$result = self::find()->select('id')->where('id=:id', [':id' => $id])->one();
return $result === NULL ? FALSE : TRUE;
}
}
......@@ -62,4 +62,15 @@ class Courseware extends \yii\db\ActiveRecord
'updated_at' => 'Updated At',
];
}
/**
* 根据id判断数据是否存在
* @param $id
* @return bool
*/
public static function existById($id)
{
$result = self::find()->select('id')->where('id=:id', [':id' => $id])->one();
return $result === NULL ? FALSE : TRUE;
}
}
......@@ -79,4 +79,17 @@ class Favorite extends \yii\db\ActiveRecord
':user_id' => $userId
])->join('left join', $tableB, "$tableA.user_id=$tableB.id")->asArray()->all();
}
/**
* 判断该用户是否收藏了该课堂
* @param $userId
* @param $courseId
* @return array|null|ActiveRecord
*/
public static function findByUserIdAndCourseId($userId, $courseId)
{
return self::find()->select('id,user_id,course_id,created_at')
->where("user_id=:user_id and course_id=:course_id", ['user_id' => $userId, 'course_id' => $courseId])
->one();
}
}
......@@ -75,6 +75,6 @@ class Support extends \yii\db\ActiveRecord
{
return self::find()->select('id,user_id,courseware_id,created_at')
->where("user_id=:user_id and courseware_id=:courseware_id", ['user_id' => $userId, 'courseware_id' => $coursewareId])
->asArray()->one();
->one();
}
}
......@@ -2,18 +2,57 @@
namespace frontend\controllers;
use common\models\Comments;
use common\models\Course;
use common\models\Courseware;
use common\models\Favorite;
use common\models\Support;
use Yii;
use common\libs\Utils;
use yii\base\InvalidParamException;
use yii\filters\AccessControl;
use yii\helpers\Html;
class HomeController extends \yii\web\Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
//权限限制
'access' => [
'class' => AccessControl::className(),
'only' => ['support', 'favorite', 'comments'],
'rules' => [
[
'allow' => TRUE,
'actions' => ['support', 'favorite'],
'roles' => ['@'],
],
[
'allow' => TRUE,
'actions' => ['comments'],
'roles' => ['@'],
'verbs' => ['GET']
],
],
]
];
}
public function actionIndex()
{
return $this->render('index');
}
public $enableCsrfValidation = FALSE;
/**
* 点赞或者取消点赞
* @param $coursewareId
......@@ -21,20 +60,23 @@ class HomeController extends \yii\web\Controller
*/
public function actionSupport($coursewareId)
{
if (Yii::$app->request->isAjax) {
$userId = Yii::$app->user->identity->getId();
$support = Support::findByUserIdAndCoursewareId($userId, $coursewareId);
//没有点赞则点赞否则则取消点赞
if (count($support) === 0) {
$support = new Support();
$support->user_id = $userId;
$support->courseware_id = $coursewareId;
$support->save();
if (Courseware::existById($coursewareId)) {
$support = Support::findByUserIdAndCoursewareId($userId, $coursewareId);
//没有点赞则点赞否则则取消点赞
if ($support === NULL) {
$support = new Support();
$support->user_id = $userId;
$support->courseware_id = $coursewareId;
$result = $support->save();
} else {
$result = $support->delete();
}
Utils::ajaxResponse($result);
} else {
Support::deleteAll('id=:id', ['id' => $support['id']]);
}
Utils::ajaxResponse(TRUE);
throw new InvalidParamException('参数错误');
};
} else {
throw new \yii\web\ForbiddenHttpException('非法请求');
}
......@@ -49,10 +91,23 @@ class HomeController extends \yii\web\Controller
public function actionFavorite($courseId)
{
if (Yii::$app->request->isAjax) {
$userId = Yii::$app->user->identity->getId();
//数据不存在则异常
if (Course::existById(['id' => $courseId])) {
$favorite = Favorite::findByUserIdAndCourseId($userId, $courseId);
if ($favorite === NULL) {
$favorite = new Favorite();
$favorite->user_id = $userId;
$favorite->course_id = $courseId;
$result = $favorite->save();
} else {
$result = $favorite->delete();
}
Utils::ajaxResponse($result);
Utils::ajaxResponse(TRUE, $courseId, $userId);
} else {
throw new InvalidParamException('参数错误');
}
} else {
throw new \yii\web\ForbiddenHttpException('非法请求');
......@@ -66,10 +121,30 @@ class HomeController extends \yii\web\Controller
*/
public function actionComments()
{
if (Yii::$app->request->isAjax) {
$userId = Yii::$app->user->identity->getId();
$this->enableCsrfValidation = FALSE;
$request = Yii::$app->request;
if ($request->isAjax) {
$parentId = $request->post('parent-id');
$comments = Html::encode($request->post('comments'));
$courseId = $request->post('course-id');
//验证数据是否正常
if ($parent = Comments::existById($courseId) or
Course::existById($courseId) or
$courseId === NULL
) {
$userId = Yii::$app->user->identity->getId();
$comments = new Comments();
$comments->user_id = $userId;
$comments->parent_id = $parentId;
$comments->course_id = $courseId;
$comments->ip = Utils::getRealIP();
$result = $comments->save();
Utils::ajaxResponse($result);
Utils::ajaxResponse(TRUE, '', $userId);
} else {
throw new InvalidParamException('参数错误');
}
} else {
throw new \yii\web\ForbiddenHttpException('非法请求');
}
......
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