Fix some issues

This commit is contained in:
linghaihui 2023-04-06 18:57:54 +08:00
parent c9985629b0
commit 532420d7f1
3 changed files with 30 additions and 18 deletions

View File

@ -33,16 +33,8 @@ Component({
}, },
lifetimes: { lifetimes: {
attached() { attached() {
var that = this app.globalData.cht = this
app.globalData.cht = that this.initMessageHistory()
that.initMessageHistory()
wx.getSystemInfo({
success: function (res) {
that.setData({
systemInfo: res,
})
},
})
}, },
detached() { detached() {
try {} catch (error) {} try {} catch (error) {}
@ -55,6 +47,7 @@ Component({
closeShareOnCopy: closeShareOnCopy, closeShareOnCopy: closeShareOnCopy,
showShare: false, showShare: false,
loadingData: false, loadingData: false,
systemInfo: systemInfo,
height: systemInfo.windowHeight - parseInt(100 / 750 * systemInfo.windowWidth) - ((systemInfo.platform == "ios" || systemInfo.platform == "android") ? 22 : 5) height: systemInfo.windowHeight - parseInt(100 / 750 * systemInfo.windowWidth) - ((systemInfo.platform == "ios" || systemInfo.platform == "android") ? 22 : 5)
}, },
methods: { methods: {
@ -106,13 +99,13 @@ Component({
if (filterData.length == 0 && e) { if (filterData.length == 0 && e) {
setTimeout(() => { setTimeout(() => {
wx.showToast({ wx.showToast({
title: "已加载完成" title: "已全部加载完成"
}) })
}, 300) }, 100)
} else { } else {
setTimeout(() => { setTimeout(() => {
wx.hideLoading() wx.hideLoading()
}, 300) }, 100)
} }
}) })
}).catch(res => { }).catch(res => {
@ -122,7 +115,6 @@ Component({
}) })
}, },
initMessageHistory() { initMessageHistory() {
//this.bindscrolltoupper()
var that = this var that = this
wx.getStorage({ wx.getStorage({
key: "chatList", key: "chatList",
@ -133,7 +125,12 @@ Component({
that.setData({ that.setData({
chatList: data, chatList: data,
}) })
} else {
that.bindscrolltoupper()
} }
},
fail: function () {
that.bindscrolltoupper()
} }
}) })
}, },

View File

@ -45,6 +45,13 @@ app.config.WEBSOCKET_PING_TIMEOUT = 30
OPENAI_CONVERSATION = defaultdict(lambda: []) OPENAI_CONVERSATION = defaultdict(lambda: [])
def show_chatgpt(sid):
for openid in conversation_ctr.get_openai_whitelist():
if openid.decode() in sid:
return 1
return 0
def reset_cookie(): def reset_cookie():
if not LOCK.acquire(blocking=False): if not LOCK.acquire(blocking=False):
return return
@ -217,6 +224,8 @@ async def ws_openai_chat(_, ws):
data = raw_json.loads(await ws.recv()) data = raw_json.loads(await ws.recv())
logger.info('[openai] Websocket receive data: %s', data) logger.info('[openai] Websocket receive data: %s', data)
sid = data['sid'] sid = data['sid']
if not show_chatgpt(sid):
raise Exception('无权限访问此服务')
q = data['q'] q = data['q']
# 保存30个对话 # 保存30个对话
history_conversation = OPENAI_CONVERSATION[sid][-30:] history_conversation = OPENAI_CONVERSATION[sid][-30:]
@ -227,7 +236,7 @@ async def ws_openai_chat(_, ws):
response = openai.ChatCompletion.create( response = openai.ChatCompletion.create(
model='gpt-3.5-turbo', model='gpt-3.5-turbo',
messages=history_conversation, messages=history_conversation,
temperature=1.2, temperature=0.8,
stream=True, stream=True,
) )
chunks = [] chunks = []
@ -269,6 +278,8 @@ async def openai_chat(request):
try: try:
logger.info('[openai] Http request payload: %s', request.json) logger.info('[openai] Http request payload: %s', request.json)
sid = request.json.get('sid') sid = request.json.get('sid')
if not show_chatgpt(sid):
raise Exception('无权限访问此服务')
q = request.json.get('q') q = request.json.get('q')
history_conversation = OPENAI_CONVERSATION[sid][-30:] history_conversation = OPENAI_CONVERSATION[sid][-30:]
history_conversation.append({ history_conversation.append({
@ -278,7 +289,7 @@ async def openai_chat(request):
response = openai.ChatCompletion.create( response = openai.ChatCompletion.create(
model='gpt-3.5-turbo', model='gpt-3.5-turbo',
messages=history_conversation, messages=history_conversation,
temperature=1.2, temperature=0.8,
stream=True, stream=True,
) )
chunks = [] chunks = []
@ -306,7 +317,7 @@ async def last_sync_time(request):
@app.post('/save') @app.post('/save')
async def save(request): async def save(request):
conversation_ctr.save(request.json.get('sid'), request.json.get('conversations')) conversation_ctr.save(request.json.get('sid'), request.json.get('conversations'))
return json({'saved': 0}) return json({'saved': show_chatgpt(request.json.get('sid'))})
@app.route('/query') @app.route('/query')

View File

@ -15,6 +15,7 @@ class ConversationCtr:
LAST_SYNC_TIME_KEY = 'bing:last_sync_time:%s' LAST_SYNC_TIME_KEY = 'bing:last_sync_time:%s'
CONVERSATION_LIST_KEY = 'bing:conversation_list:%s' CONVERSATION_LIST_KEY = 'bing:conversation_list:%s'
OPENAI_WHITE_LIST_KEY = 'bing:openai_white_list'
def __init__(self, client=None) -> None: def __init__(self, client=None) -> None:
self.redis_client = client self.redis_client = client
@ -54,5 +55,8 @@ class ConversationCtr:
key = self.CONVERSATION_LIST_KEY % sid key = self.CONVERSATION_LIST_KEY % sid
self.redis_client.delete(key) self.redis_client.delete(key)
def get_openai_whitelist(self):
return self.redis_client.lrange(self.OPENAI_WHITE_LIST_KEY, 0, 10000)
conversation_ctr = ConversationCtr() conversation_ctr = ConversationCtr()