feat: 限制用户单日提问次数
This commit is contained in:
+52
-2
@@ -266,12 +266,62 @@ class WxRequest {
|
||||
post(url, data = {}, header = {}) {
|
||||
return this.request({ url, method: 'POST', data, header });
|
||||
}
|
||||
|
||||
/**
|
||||
* SSE stream request with chunked transfer.
|
||||
* Reuses baseUrl + token interceptor.
|
||||
* @param {string} url API path, e.g. '/plant/chat/stream'
|
||||
* @param {Object} data Query params (GET) or body (POST)
|
||||
* @param {Object} callbacks { onChunk, onDone, onError }
|
||||
* @returns {Object} wx.request task (call task.abort() to cancel)
|
||||
*/
|
||||
stream(url, data = {}, callbacks = {}) {
|
||||
const fullUrl = url.startsWith('http')
|
||||
? url
|
||||
: this.baseUrl + url;
|
||||
|
||||
// Build query string for GET
|
||||
const qs = Object.keys(data)
|
||||
.map(k => `${encodeURIComponent(k)}=${encodeURIComponent(data[k])}`)
|
||||
.join('&');
|
||||
const reqUrl = qs ? `${fullUrl}?${qs}` : fullUrl;
|
||||
|
||||
// Resolve token via same logic as interceptor
|
||||
let token = wx.getStorageSync('token');
|
||||
|
||||
const mergedHeader = {
|
||||
...this.header,
|
||||
'Accept': 'text/event-stream',
|
||||
};
|
||||
if (token) {
|
||||
mergedHeader['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const task = wx.request({
|
||||
url: reqUrl,
|
||||
method: 'GET',
|
||||
enableChunked: true,
|
||||
header: mergedHeader,
|
||||
success: () => {
|
||||
if (callbacks.onDone) callbacks.onDone();
|
||||
},
|
||||
fail: (err) => {
|
||||
if (callbacks.onError) callbacks.onError(err);
|
||||
},
|
||||
});
|
||||
|
||||
if (callbacks.onChunk) {
|
||||
task.onChunkReceived(callbacks.onChunk);
|
||||
}
|
||||
|
||||
return task;
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize with default instance
|
||||
const request = new WxRequest({
|
||||
baseUrl: 'http://192.168.0.184:8889',
|
||||
//baseUrl: 'https://go.sundynix.cn/api',
|
||||
//baseUrl: 'http://192.168.0.184:8889',
|
||||
baseUrl: 'https://go.sundynix.cn/api',
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user