From 06b283db1dec6c9d047bf9e4dbcf0e27f12c2fbc Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 25 Nov 2017 18:50:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=AE=E7=AD=94=E5=89=8D=E7=AB=AF=E9=83=A8?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/controllers/QuestionController.php | 20 ++++- frontend/views/assets/js/question.js | 94 +++++++++++++++------ frontend/views/comment/_item.php | 37 ++++++++ frontend/views/comment/detail.php | 10 +++ frontend/views/comment/index.php | 13 +++ frontend/views/question/view.php | 8 +- 6 files changed, 148 insertions(+), 34 deletions(-) create mode 100644 frontend/views/comment/_item.php create mode 100644 frontend/views/comment/detail.php create mode 100644 frontend/views/comment/index.php diff --git a/frontend/controllers/QuestionController.php b/frontend/controllers/QuestionController.php index ff044aa..b3f6008 100644 --- a/frontend/controllers/QuestionController.php +++ b/frontend/controllers/QuestionController.php @@ -20,6 +20,7 @@ use yii\web\ServerErrorHttpException; use yuncms\question\models\Question; use yuncms\question\models\QuestionAnswer; +use yuncms\question\models\QuestionAttention; use yuncms\question\models\QuestionCollection; use yuncms\tag\models\Tag; @@ -298,13 +299,28 @@ public function actionCollection() /** * 关注问题 + * @return array + * @throws ServerErrorHttpException */ public function actionAttention() { - + Yii::$app->response->format = Response::FORMAT_JSON; + $source = $this->findModel(Yii::$app->request->post('model_id')); + if (($attention = $source->getAttentions()->andWhere(['user_id' => Yii::$app->user->getId()])->one()) != null) { + $attention->delete(); + return ['status' => 'unfollowed']; + } else { + $model = new QuestionAttention(); + $model->load(Yii::$app->request->post(), ''); + $model->model_id = $source->id; + //$model->user_id = Yii::$app->user->getId(); + if ($model->save() === false && !$model->hasErrors()) { + throw new ServerErrorHttpException('Failed to update the object for unknown reason.'); + } + return ['status' => 'followed']; + } } - /** * 获取模型 * diff --git a/frontend/views/assets/js/question.js b/frontend/views/assets/js/question.js index e2bbafc..4c5212e 100644 --- a/frontend/views/assets/js/question.js +++ b/frontend/views/assets/js/question.js @@ -24,34 +24,6 @@ window.yii.question = (function ($) { jQuery.post("/question/answer/adopt", {answerId: jQuery(this).data('answer_id')}); }); - $(document).on('click', '[data-target="question-collect"]', function (e) { - $(this).button('loading'); - var collect_btn = $(this); - var model_id = $(this).data('model_id'); - var show_num = $(this).data('show_num'); - $.post("/question/question/collection", {model_id: model_id}, function (result) { - collect_btn.removeClass('disabled'); - collect_btn.removeAttr('disabled'); - if (result.status === 'collected') { - collect_btn.html('已收藏'); - collect_btn.addClass('active'); - } else { - collect_btn.html('收藏'); - collect_btn.removeClass('active'); - } - - /*是否操作收藏数*/ - if (Boolean(show_num)) { - var collect_num = collect_btn.nextAll("#collection-num").html(); - if (result.status === 'collected') { - collect_btn.nextAll("#collection-num").html(parseInt(collect_num) + 1); - } else { - collect_btn.nextAll("#collection-num").html(parseInt(collect_num) - 1); - } - } - }); - }); - $(".widget-comments").on('show.bs.collapse', function () { pub.load_comments($(this).data('model_id')); }); @@ -60,6 +32,8 @@ window.yii.question = (function ($) { pub.clear_comments($(this).data('model_id')); }); + pub.handleCollect(); + pub.handleFollow(); }, /** @@ -91,6 +65,70 @@ window.yii.question = (function ($) { $.get('/question/comment/index', {id: id}, function (html) { $("#comments-" + id + " .widget-comment-list").append(html); }); + }, + + handleCollect: function () { + $(document).on('click', '[data-target="question-collect"]', function (e) { + $(this).button('loading'); + var collect_btn = $(this); + var model_id = $(this).data('model_id'); + var show_num = $(this).data('show_num'); + $.post("/question/question/collection", {model_id: model_id}, function (result) { + collect_btn.removeClass('disabled'); + collect_btn.removeAttr('disabled'); + if (result.status === 'collected') { + collect_btn.html('已收藏'); + collect_btn.addClass('active'); + } else { + collect_btn.html('收藏'); + collect_btn.removeClass('active'); + } + + /*是否操作收藏数*/ + if (Boolean(show_num)) { + var collect_num = collect_btn.nextAll("#collection-num").html(); + if (result.status === 'collected') { + collect_btn.nextAll("#collection-num").html(parseInt(collect_num) + 1); + } else { + collect_btn.nextAll("#collection-num").html(parseInt(collect_num) - 1); + } + } + }); + }); + }, + + /** + * 关注问题 + */ + handleFollow: function () { + $(document).on('click', '[data-target="question-follow"]', function (e) { + $(this).button('loading'); + var follow_btn = $(this); + var model_id = $(this).data('model_id'); + var show_num = $(this).data('show_num'); + $.post("/question/question/attention", {model_id: model_id}, function (result) { + follow_btn.removeClass('disabled'); + follow_btn.removeAttr('disabled'); + if (result.status == 'followed') { + follow_btn.html('已关注'); + follow_btn.addClass('active'); + } else { + follow_btn.html('关注'); + follow_btn.removeClass('active'); + } + + /*是否操作关注数*/ + if (Boolean(show_num)) { + var follower_num = follow_btn.nextAll("#follower-num").html(); + if (result.status == 'followed') { + follow_btn.nextAll("#follower-num").html(parseInt(follower_num) + 1); + } else { + follow_btn.nextAll("#follower-num").html(parseInt(follower_num) - 1); + } + } + return callback(result.status); + }); + }); } }; return pub; diff --git a/frontend/views/comment/_item.php b/frontend/views/comment/_item.php new file mode 100644 index 0000000..435eded --- /dev/null +++ b/frontend/views/comment/_item.php @@ -0,0 +1,37 @@ + +
+ + <?= Html::encode($model->user->nickname) ?> + +
+
+
+ user->nickname ?> + to_user_id): ?> + 回复 + toUser->nickname ?> + +
+

content) ?>

+ + +
\ No newline at end of file diff --git a/frontend/views/comment/detail.php b/frontend/views/comment/detail.php new file mode 100644 index 0000000..5b8e695 --- /dev/null +++ b/frontend/views/comment/detail.php @@ -0,0 +1,10 @@ + +
+ render( + '_item.php', ['model' => $model] + ) ?> +
\ No newline at end of file diff --git a/frontend/views/comment/index.php b/frontend/views/comment/index.php new file mode 100644 index 0000000..a525ccc --- /dev/null +++ b/frontend/views/comment/index.php @@ -0,0 +1,13 @@ + + + ['class' => null], + 'dataProvider' => $dataProvider, + 'itemView' => '_item',//子视图 + 'itemOptions' => ['class' => 'media'], + 'layout' => "{items}\n{pager}", +]); ?> + \ No newline at end of file diff --git a/frontend/views/question/view.php b/frontend/views/question/view.php index b7c85cb..781ec01 100644 --- a/frontend/views/question/view.php +++ b/frontend/views/question/view.php @@ -188,14 +188,14 @@ class="text-muted"> - user->profile->city ?>
  • user->isGuest && $model->isFollowed(Yii::$app->user->getId())): ?> - -