Skip to content

Commit

Permalink
问答前端部分
Browse files Browse the repository at this point in the history
  • Loading branch information
xutl committed Nov 25, 2017
1 parent 2cf8993 commit 06b283d
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 34 deletions.
20 changes: 18 additions & 2 deletions frontend/controllers/QuestionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'];
}
}


/**
* 获取模型
*
Expand Down
94 changes: 66 additions & 28 deletions frontend/views/assets/js/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
Expand All @@ -60,6 +32,8 @@ window.yii.question = (function ($) {
pub.clear_comments($(this).data('model_id'));
});

pub.handleCollect();
pub.handleFollow();
},

/**
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions frontend/views/comment/_item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
use yii\helpers\Url;
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;

/**
* @var \yuncms\question\models\QuestionComment $model
*/
?>
<div class="media-left">
<a href="<?= Url::to(['/user/space/view', 'id' => $model->user_id]) ?>" target="_blank">
<img class="media-object avatar-27" alt="<?= Html::encode($model->user->nickname) ?>"
src="<?= $model->user->getAvatar() ?>">
</a>
</div>
<div class="media-body">
<div class="media-heading">
<a href="<?= Url::to(['/user/space/view', 'id' => $model->user_id]) ?>"
target="_blank"><?= $model->user->nickname ?></a>
<?php if ($model->to_user_id): ?>
<span class="text-muted">回复 </span>
<a href="<?= Url::to(['/user/space/view', 'id' => $model->to_user_id]) ?>"
target="_blank"><?= $model->toUser->nickname ?></a>
<?php endif; ?>
</div>
<div class="content"><p><?= HtmlPurifier::process($model->content) ?></p></div>
<div class="media-footer">
<span class="text-muted"><?= Yii::$app->formatter->asRelativeTime($model->created_at); ?></span>
<?php if (!Yii::$app->user->isGuest && $model->user_id != Yii::$app->user->id): ?>
<a href="#" class="ml-10 comment-reply"
data-model_id="<?= $model->model_id ?>" data-to_user_id="<?= $model->user_id ?>"
data-model_class="<?= $model->model_class ?>"
data-message="回复 <?= Html::encode($model->user->nickname) ?>"><i class="fa fa-reply"></i> 回复</a>
<?php endif; ?>
</div>

</div>
10 changes: 10 additions & 0 deletions frontend/views/comment/detail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
use yii\helpers\Url;
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
<div class="media">
<?= $this->render(
'_item.php', ['model' => $model]
) ?>
</div>
13 changes: 13 additions & 0 deletions frontend/views/comment/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
use yii\widgets\Pjax;
use yii\widgets\ListView;
?>
<?php Pjax::begin(); ?>
<?= ListView::widget([
'options' => ['class' => null],
'dataProvider' => $dataProvider,
'itemView' => '_item',//子视图
'itemOptions' => ['class' => 'media'],
'layout' => "{items}\n{pager}",
]); ?>
<?php Pjax::end(); ?>
8 changes: 4 additions & 4 deletions frontend/views/question/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ class="text-muted"> - <?= $bestAnswer->user->profile->city ?></span>
<!-- 关注部分 -->
<li>
<?php if (!Yii::$app->user->isGuest && $model->isFollowed(Yii::$app->user->getId())): ?>
<button type="button" data-target="follow-button" class="btn btn-success btn-sm active"
data-source_type="question" data-source_id="<?= $model->id ?>" data-show_num="true"
<button type="button" data-target="question-follow" class="btn btn-success btn-sm active"
data-model_id="<?= $model->id ?>" data-show_num="true"
data-toggle="tooltip" data-placement="right" title="" data-original-title="关注后将获得更新提醒">
已关注
</button>
<?php else: ?>
<button type="button" data-target="follow-button" class="btn btn-success btn-sm"
data-source_type="question" data-source_id="<?= $model->id ?>" data-show_num="true"
<button type="button" data-target="question-follow" class="btn btn-success btn-sm"
data-model_id="<?= $model->id ?>" data-show_num="true"
data-toggle="tooltip" data-placement="right" title="" data-original-title="关注后将获得更新提醒">
关注
</button>
Expand Down

0 comments on commit 06b283d

Please sign in to comment.