明树Git Lab
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
D
deepseek_wechat_chat
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengfanpei
deepseek_wechat_chat
Commits
5d7b9c51
Commit
5d7b9c51
authored
Feb 21, 2025
by
yangyajing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
改为流式请求,监听滚动事件获取分页数据
parent
1f65507d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
133 additions
and
38 deletions
+133
-38
index.js
public/publicAccount/js/index.js
+133
-38
No files found.
public/publicAccount/js/index.js
View file @
5d7b9c51
$
(
function
()
{
// 判断是否已授权
let
openId
=
""
;
let
question
=
""
;
let
answer
=
""
;
let
page
=
1
;
let
pageSize
=
10
;
let
havePage
=
true
;
// 判断是否已授权
function
checkCode
()
{
let
searchCode
=
new
URLSearchParams
(
location
.
search
).
get
(
"code"
);
if
(
searchCode
)
{
...
...
@@ -19,7 +22,6 @@ $(function () {
})
// 获取问答记录
let
page
=
1
;
function
getList
()
{
$
.
ajax
({
url
:
"http://deepseek.bridata.com/api/question/getQuesList"
,
...
...
@@ -27,7 +29,7 @@ $(function () {
contentType
:
"application/json"
,
data
:
JSON
.
stringify
({
page
:
page
,
pageSize
:
5
,
pageSize
:
pageSize
,
openid
:
openId
}),
dataType
:
"json"
,
...
...
@@ -46,7 +48,11 @@ $(function () {
});
$
(
'.conversation-wrap'
).
html
(
html
+
$
(
'.conversation-wrap'
).
html
());
scrollBottom
();
havePage
=
conversationList
.
length
===
pageSize
;
}
},
error
:
function
()
{
alert
(
"网络连接失败"
);
}
})
}
...
...
@@ -65,6 +71,9 @@ $(function () {
openId
=
res
.
data
.
openid
;
getList
();
}
},
error
:
function
()
{
alert
(
"网络连接失败"
);
}
})
}
...
...
@@ -74,43 +83,122 @@ $(function () {
event
.
preventDefault
();
question
=
$
(
this
).
val
();
if
(
question
)
{
$
(
'.conversation-loading'
).
show
();
$
(
this
).
attr
(
"disabled"
,
true
);
$
.
ajax
({
url
:
"https://dashscope.aliyuncs.com/api/v1/apps/6de1065c75074ee1a11238ab90c1aa34/completion"
,
type
:
"post"
,
timeout
:
1800000
,
headers
:
{
"Authorization"
:
"Bearer sk-0ff2fc43671f4ed28dd7c80289da04e7"
,
"Content-Type"
:
"application/json"
},
contentType
:
"application/json"
,
data
:
JSON
.
stringify
({
async
function
handleAsk
()
{
try
{
const
response
=
await
fetch
(
"https://dashscope.aliyuncs.com/api/v1/apps/6de1065c75074ee1a11238ab90c1aa34/completion"
,
{
body
:
JSON
.
stringify
({
input
:
{
prompt
:
question
prompt
:
question
,
stream
:
true
},
parameters
:
{},
debug
:
{}
}),
dataType
:
"json"
,
success
:
(
res
)
=>
{
answer
=
res
.
output
.
text
;
method
:
"post"
,
headers
:
{
"Authorization"
:
"Bearer sk-0ff2fc43671f4ed28dd7c80289da04e7"
,
"Content-Type"
:
"application/json"
,
"X-DashScope-SSE"
:
"enable"
},
credentials
:
"omit"
,
mode
:
"cors"
});
if
(
response
.
status
==
200
)
{
const
reader
=
response
.
body
.
getReader
();
const
decoder
=
new
TextDecoder
();
$
(
"#question-input"
).
val
(
""
).
attr
(
"disabled"
,
false
);
while
(
true
)
{
const
{
value
,
done
}
=
await
reader
.
read
();
function
transformValue
(
val
)
{
const
string
=
decoder
.
decode
(
val
);
const
lines
=
string
.
split
(
'
\
n'
);
const
result
=
{};
lines
.
forEach
(
line
=>
{
const
[
k
,
v
]
=
line
.
split
(
':'
);
if
(
k
&&
v
)
{
if
(
k
===
'data'
)
{
result
[
k
]
=
JSON
.
parse
(
line
.
slice
(
line
.
indexOf
(
":"
)
+
1
));
}
else
{
result
[
k
]
=
v
;
}
}
});
return
result
;
}
const
result
=
transformValue
(
value
);
if
(
done
)
{
$
(
'.conversation-loading'
).
hide
();
saveConversation
();
break
;
}
answer
=
""
;
if
(
result
.
data
&&
result
.
data
.
output
&&
result
.
data
.
output
.
text
)
{
answer
=
result
.
data
.
output
.
text
;
}
$
(
".conversation-item"
).
eq
(
$
(
".conversation-item"
).
length
-
1
).
find
(
".answer-data"
).
html
(
answer
);
scrollBottom
();
}
}
else
{
$
(
".conversation-item"
).
eq
(
$
(
".conversation-item"
).
length
-
1
).
find
(
".answer-data"
).
html
(
"网络连接失败"
);
$
(
"#question-input"
).
val
(
""
).
attr
(
"disabled"
,
false
);
$
(
'.conversation-loading'
).
hide
();
}
}
catch
(
err
)
{
$
(
".conversation-item"
).
eq
(
$
(
".conversation-item"
).
length
-
1
).
find
(
".answer-data"
).
html
(
"网络连接失败"
);
$
(
"#question-input"
).
val
(
""
).
attr
(
"disabled"
,
false
);
$
(
'.conversation-loading'
).
hide
();
}
}
// 创建对话框
let
div
=
document
.
createElement
(
"div"
);
div
.
className
=
"conversation-item"
;
div
.
innerHTML
=
`<div class="conversation-item">
<div class="conversation-question">
div
.
innerHTML
=
`<div class="conversation-question">
<div class="question-data">
${
question
}
</div>
</div>
<div class="conversation-answer">
<div class="answer-data">
${
answer
}
</div>
<div class="answer-data">
</div>
</div>`
;
$
(
'.conversation-wrap'
)[
0
].
insertBefore
(
div
,
$
(
'.conversation-loading'
)[
0
]);
$
(
this
).
val
(
""
);
$
(
'.conversation-loading'
).
hide
();
scrollBottom
();
saveConversation
();
}
})
$
(
'.conversation-loading'
).
show
();
$
(
this
).
attr
(
"disabled"
,
true
);
handleAsk
();
// $.ajax({
// url: "https://dashscope.aliyuncs.com/api/v1/apps/6de1065c75074ee1a11238ab90c1aa34/completion",
// type: "post",
// timeout: 1800000,
// headers: {
// "Authorization": "Bearer sk-0ff2fc43671f4ed28dd7c80289da04e7",
// "Content-Type": "application/json",
// "X-DashScope-SSE": "enable"
// },
// contentType: "application/json",
// data: JSON.stringify({
// input: {
// prompt: question,
// stream: true
// },
// parameters: {},
// debug: {}
// }),
// dataType: "json",
// success: (res) => {
// answer = res.output.text;
// let div = document.createElement("div");
// div.className = "conversation-item";
// div.innerHTML = `<div class="conversation-item">
// <div class="conversation-question">
// <div class="question-data">${question}</div>
// </div>
// <div class="conversation-answer">
// <div class="answer-data">${answer}</div>
// </div>`;
// $('.conversation-wrap')[0].insertBefore(div, $('.conversation-loading')[0]);
// $(this).val("");
// $('.conversation-loading').hide();
// scrollBottom();
// // saveConversation();
// }
// })
}
}
});
...
...
@@ -137,4 +225,11 @@ $(function () {
let
content
=
$
(
".conversation-wrap"
)[
0
];
content
.
scrollTo
({
top
:
content
.
scrollHeight
,
behavior
:
"smooth"
});
}
// 监听滚动事件,获取分页数据
$
(
".conversation-wrap"
).
on
(
"scroll"
,
function
()
{
if
(
$
(
this
)[
0
].
scrollTop
===
0
&&
havePage
)
{
page
++
;
getList
();
}
})
})
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment