add history in chat
This commit is contained in:
parent
794502df69
commit
7f11e85034
@ -43,6 +43,14 @@ def manage():
|
||||
@login_required
|
||||
def chat():
|
||||
if current_user.isActivated:
|
||||
if request.args.get("act") == "get_history":
|
||||
chats_desc = Conversation.query.filter_by(
|
||||
userid=current_user.id, useremail=current_user.email).order_by(Conversation.datetime.desc()).limit(20).all()
|
||||
msgs = []
|
||||
for chat in reversed(chats_desc):
|
||||
msgs.append(
|
||||
{"request": chat.request, "response": chat.response, "datetime": chat.datetime})
|
||||
return msgs
|
||||
return render_template('chat.html', user=current_user)
|
||||
else:
|
||||
flash("您的账户暂未被激活")
|
||||
|
@ -38,6 +38,40 @@
|
||||
});
|
||||
</script>
|
||||
<script>
|
||||
function get_history() {
|
||||
var msgs
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: "{{ url_for('main.chat') }}",
|
||||
data: { act: "get_history" },
|
||||
success: function (msgs) {
|
||||
msgs.forEach(msg => {
|
||||
var msg_list = document.getElementById("msg-list")
|
||||
var li = document.createElement('li');
|
||||
var span = document.createElement('span');
|
||||
li.appendChild(span)
|
||||
li.style.marginBottom = "20px"
|
||||
if (msg.response == "" && msg.request != "") {
|
||||
li.style.textAlign = "right"
|
||||
span.innerHTML = msg.request
|
||||
span.style.backgroundColor = "lightblue"
|
||||
} else if (msg.request != "") {
|
||||
li.style.textAlign = "left"
|
||||
span.innerHTML = msg.response
|
||||
span.style.backgroundColor = "lightgreen"
|
||||
}
|
||||
span.style.borderRadius = "10px"
|
||||
span.style.padding = "5px"
|
||||
msg_list.appendChild(li)
|
||||
})
|
||||
window.scrollTo(0,document.documentElement.scrollHeight)
|
||||
}
|
||||
})
|
||||
}
|
||||
window.onload=function(){
|
||||
get_history()
|
||||
}
|
||||
|
||||
function send_message() {
|
||||
var data= {
|
||||
msgtype: "text",
|
||||
|
Reference in New Issue
Block a user