Commit 930f6fd7 authored by xiabin's avatar xiabin

编写部分接口

parent 08a28d97
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace common\models; namespace common\models;
use Yii; use Yii;
use yii\behaviors\TimestampBehavior;
use yii\redis\ActiveRecord;
/** /**
* This is the model class for table "support". * This is the model class for table "support".
...@@ -22,13 +24,30 @@ class Support extends \yii\db\ActiveRecord ...@@ -22,13 +24,30 @@ class Support extends \yii\db\ActiveRecord
return 'support'; return 'support';
} }
/**
* @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', 'courseware_id', 'created_at'], 'required'], [['user_id', 'courseware_id',], 'required'],
[['user_id', 'courseware_id', 'created_at'], 'integer'], [['user_id', 'courseware_id', 'created_at'], 'integer'],
]; ];
} }
...@@ -45,4 +64,17 @@ class Support extends \yii\db\ActiveRecord ...@@ -45,4 +64,17 @@ class Support extends \yii\db\ActiveRecord
'created_at' => 'Created At', 'created_at' => 'Created At',
]; ];
} }
/**
* 获取该课程是否点赞
* @param $userId
* @param $coursewareId
* @return mixed
*/
public static function findByUserIdAndCoursewareId($userId, $coursewareId)
{
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();
}
} }
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
namespace frontend\controllers; namespace frontend\controllers;
use common\models\Support;
use Yii;
use common\libs\Utils;
class HomeController extends \yii\web\Controller class HomeController extends \yii\web\Controller
{ {
public function actionIndex() public function actionIndex()
...@@ -9,4 +13,65 @@ class HomeController extends \yii\web\Controller ...@@ -9,4 +13,65 @@ class HomeController extends \yii\web\Controller
return $this->render('index'); return $this->render('index');
} }
/**
* 点赞或者取消点赞
* @param $coursewareId
* @throws \yii\web\ForbiddenHttpException
*/
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();
} else {
Support::deleteAll('id=:id', ['id' => $support['id']]);
}
Utils::ajaxResponse(TRUE);
} else {
throw new \yii\web\ForbiddenHttpException('非法请求');
}
}
/**
* 收藏
* @param $courseId
* @throws \yii\web\ForbiddenHttpException
*/
public function actionFavorite($courseId)
{
if (Yii::$app->request->isAjax) {
$userId = Yii::$app->user->identity->getId();
Utils::ajaxResponse(TRUE, $courseId, $userId);
} else {
throw new \yii\web\ForbiddenHttpException('非法请求');
}
}
/**
* 评论
* @throws \yii\web\ForbiddenHttpException
*/
public function actionComments()
{
if (Yii::$app->request->isAjax) {
$userId = Yii::$app->user->identity->getId();
Utils::ajaxResponse(TRUE, '', $userId);
} 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