說明:用于獲取文檔的評(píng)論列表、評(píng)論分頁列表
使用方法:{% commentList 變量名稱 with archiveId="1" type="page|list" %}
如將變量定義為 comments {% commentList comments with archiveId="1" type="page" %}...{% endcommentList %}
commentList
支持的參數(shù)有:archiveId
archiveId
為指定的文檔ID;order
order
可以指定顯示的排序規(guī)則,支持依據(jù) id正序排序 order="id desc"
、按id倒敘排序 order="id desc"
;limit
數(shù)量的列表,比如limit="10"
則只會(huì)顯示10條,limit
在不是分頁列表的時(shí)候,支持offset
模式,也就是 ,
分隔模式,如想從第2條開始,獲取10條數(shù)據(jù),可以設(shè)置成 limit="2,10"
;type
type
支持按 page、list 方式列出。默認(rèn)值為list,如果type="page"
后續(xù)可用 分頁標(biāo)簽 pagination
來組織分頁顯示 {% pagination pages with show="5" %}
。siteId
siteId
一般不需要填寫,如果你使用后臺(tái)的多站點(diǎn)管理創(chuàng)建了多個(gè)站點(diǎn),并且想調(diào)用其他站點(diǎn)的數(shù)據(jù),則可以通過指定 siteId
來實(shí)現(xiàn)調(diào)用指定站點(diǎn)的數(shù)據(jù)。comments 是一個(gè)數(shù)組對(duì)象,因此需要使用 for
循環(huán)來輸出
Id
ArchiveId
UserName
UserId
Ip
VoteCount
Content
ParentId
Status
Status = 1 表示審核通過, status = 0 時(shí)審核中,不要顯示出來Parent
Parent 包含上級(jí)評(píng)論的完整 item,字段和評(píng)論item相同CreatedTime
時(shí)間戳,需要使用格式化時(shí)間戳為日期格式 {{stampToDate(item.CreatedTime, "2006-01-02")}}
{# list 列表展示 #}
<div>
{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
{% for item in comments %}
<div>
<div>
<span>
{% if item.Status != 1 %}
審核中:{{item.UserName|truncatechars:6}}
{% else %}
{{item.UserName}}
{% endif %}
</span>
{% if item.Parent %}
<span>回復(fù)</span>
<span>
{% if item.Status != 1 %}
審核中:{{item.Parent.UserName|truncatechars:6}}
{% else %}
{{item.Parent.UserName}}
{% endif %}
</span>
{% endif %}
<span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
</div>
<div>
{% if item.Parent %}
<blockquote>
{% if item.Parent.Status != 1 %}
該內(nèi)容正在審核中:{{item.Parent.Content|truncatechars:9}}
{% else %}
{{item.Parent.Content|truncatechars:100}}
{% endif %}
</blockquote>
{% endif %}
{% if item.Status != 1 %}
該內(nèi)容正在審核中:{{item.Content|truncatechars:9}}
{% else %}
{{item.Content}}
{% endif %}
</div>
<div class="comment-control" data-id="{{item.Id}}" data-user="{{item.UserName}}">
<a class="item" data-id="praise">贊(<span class="vote-count">{{item.VoteCount}}</span>)</a>
<a class="item" data-id=reply>回復(fù)</a>
</div>
</div>
{% endfor %}
{% endcommentList %}
</div>
{# page 分頁列表展示 #}
<div>
{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
{% for item in comments %}
<div>
<div>
<span>
{% if item.Status != 1 %}
審核中:{{item.UserName|truncatechars:6}}
{% else %}
{{item.UserName}}
{% endif %}
</span>
{% if item.Parent %}
<span>回復(fù)</span>
<span>
{% if item.Status != 1 %}
審核中:{{item.Parent.UserName|truncatechars:6}}
{% else %}
{{item.Parent.UserName}}
{% endif %}
</span>
{% endif %}
<span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
</div>
<div>
{% if item.Parent %}
<blockquote>
{% if item.Parent.Status != 1 %}
該內(nèi)容正在審核中:{{item.Parent.Content|truncatechars:9}}
{% else %}
{{item.Parent.Content|truncatechars:100}}
{% endif %}
</blockquote>
{% endif %}
{% if item.Status != 1 %}
該內(nèi)容正在審核中:{{item.Content|truncatechars:9}}
{% else %}
{{item.Content}}
{% endif %}
</div>
<div class="comment-control" data-id="{{item.Id}}" data-user="{{item.UserName}}">
<a class="item" data-id="praise">贊(<span class="vote-count">{{item.VoteCount}}</span>)</a>
<a class="item" data-id=reply>回復(fù)</a>
</div>
</div>
{% endfor %}
{% endcommentList %}
</div>
<div>
{% pagination pages with show="5" %}
<ul>
<li>總數(shù):{{pages.TotalItems}}條,總共:{{pages.TotalPages}}頁,當(dāng)前第{{pages.CurrentPage}}頁</li>
<li class="{% if pages.FirstPage.IsCurrent %}active{% endif %}"><a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a></li>
{% if pages.PrevPage %}
<li><a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a></li>
{% endif %}
{% for item in pages.Pages %}
<li class="{% if item.IsCurrent %}active{% endif %}"><a href="{{item.Link}}">{{item.Name}}</a></li>
{% endfor %}
{% if pages.NextPage %}
<li><a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a></li>
{% endif %}
<li class="{% if pages.LastPage.IsCurrent %}active{% endif %}"><a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a></li>
</ul>
{% endpagination %}
</div>
評(píng)論使用form表單提交,提交后臺(tái)接收的地址為:/comment/publish
。需要提交的字段有
字段 | 是否必填 | 說明 |
---|---|---|
archive_id | 是 | 對(duì)應(yīng)的文檔ID |
user_name | 是 | 評(píng)論的用戶名 |
content | 是 | 評(píng)論內(nèi)容 |
parent_id | 否 | 上級(jí)評(píng)論ID,當(dāng)回復(fù),某條評(píng)論的時(shí)候,要帶上才會(huì)產(chǎn)生關(guān)聯(lián) |
return | 否 | 提交后,指定后端返回的格式,可選的值有:`html`、`json`,默認(rèn)為 html |
表單代碼示例
<form method="post" action="/comment/publish">
<input type="hidden" name="return" value="html">
<input type="hidden" name="archive_id" value="{% archiveDetail with name="Id" %}">
<div>
<label>用戶名</label>
<div>
<input type="text" name="user_name" required lay-verify="required" placeholder="請(qǐng)?zhí)顚懩年欠Q" autocomplete="off">
</div>
</div>
<div>
<label>評(píng)論內(nèi)容</label>
<div>
<textarea name="content" placeholder="" id="comment-content-field" rows="5"></textarea>
</div>
</div>
<div>
<div>
<button type="submit">提交評(píng)論</button>
<button type="reset">重置</button>
</div>
</div>
</form>
可以給某條評(píng)論的內(nèi)容點(diǎn)贊,點(diǎn)贊使用form表單提交,點(diǎn)贊提交后臺(tái)接收地址為:/comment/praise
。點(diǎn)贊需要提交的字段有:
字段 | 是否必填 | 說明 |
---|---|---|
id | 是 | 評(píng)論內(nèi)容ID |
評(píng)論點(diǎn)贊只支持返回 json 格式的數(shù)據(jù),因此需要用 js post 來提交。
示例代碼
<div class="comment-control">
<a class="item vote-comment" data-id="praise" data-id="{{item.Id}}">贊(<span class="vote-count">{{item.VoteCount}}</span>)</a>
</div>
$('.vote-comment').click(function(e) {
let that = $(this);
let commentId = $(this).data('id');
//贊
$.ajax({
url: '/comment/praise',
method: "post",
data: {id: commentId},
dataType: "json",
success: function (res) {
if(res.code === 0) {
alert(res.msg);
that.find('.vote-count').text(res.data.vote_count)
} else {
alert(res.msg);
}
},
error: function (err) {
alert(res.msg);
}
});
});