Browse Source

fix: optimize code format

Pchen. 1 year ago
parent
commit
fe81117546
3 changed files with 10 additions and 34 deletions
  1. 0 2
      app.js
  2. 7 5
      router.js
  3. 3 27
      wechat/ChatGPT.js

+ 0 - 2
app.js

@@ -9,8 +9,6 @@ const app = express()
 const cors = require('cors')
 app.use(cors())
 
-router.use(express.static('./public'))
-
 app.use(express.json())
 
 app.use(router)

+ 7 - 5
router.js

@@ -1,5 +1,5 @@
 const express = require('express')
-const { sendMessageToAPI, setApiKey, setApiUrl, setapp_code ,setmodel} = require('./wechat/ChatGPT')
+const { updateGPTConfig } = require('./wechat/ChatGPT')
 const sqlite3 = require('sqlite3')
 const jsonwebtoken = require('jsonwebtoken')
 const path = require('path')
@@ -20,6 +20,8 @@ var db = new sqlite3.Database(sqliteDbPath)
 
 const router = express.Router()
 
+router.use(express.static('./public'))
+
 // 定义中间件.unless指定哪些接口不需要进行token身份认证
 const { expressjwt: jwt } = require("express-jwt")
 const checkTokenMiddleware = jwt({ secret: secretKey, algorithms: ["HS256"] }).unless({
@@ -178,10 +180,10 @@ router.post('/getapiconfig', async (req, res) => {
 router.post('/apiconfig',async(req,res) => {
     const { apiKey,apiUrl,app_code,model } = req.body
     try {
-        setApiKey(apiKey)
-        setApiUrl(apiUrl)
-        setapp_code(app_code)
-        setmodel(model)
+        updateGPTConfig("apiKey", apiKey)
+        updateGPTConfig("apiUrl", apiUrl)
+        updateGPTConfig("app_code", app_code)
+        updateGPTConfig("model",model)
         res.send({status: 200,msg: '设置成功!'})
     } catch (error) {
         res.send({status: 500, msg: '设置失败!'})

+ 3 - 27
wechat/ChatGPT.js

@@ -30,9 +30,8 @@ async function loadConfigValues() {
         app_code = await getConfigValue('app_code')
         suffix = await getConfigValue('suffix')
         model = await getConfigValue('model')
-        console.log('api接口设置加载成功');
     } catch (error) {
-        console.error('加载api接口设置失败!', error);
+        console.error('加载api接口设置失败!', error)
     }
 }
 
@@ -65,36 +64,13 @@ async function sendMessageToAPI(message) {
 }
 
 // 更新api设置到数据库
-function updateapiConfigValue(configName, configValue) {
+function updateGPTConfig(configName, configValue) {
     const query = 'REPLACE INTO apiconfig (config, value) VALUES (?, ?)';
     db.run(query, [configName, configValue], (err) => {
         if (err) {
             console.error('更新数据失败:', err);
-        } else {
-            console.log('更新数据成功');
         }
     });
 }
 
-// 设置是否自动回复
-function setApiKey(value) {
-    apiKey = value;
-    updateapiConfigValue('apiKey', value);
-}
-
-function setApiUrl(value) {
-    apiUrl= value;
-    updateapiConfigValue('apiUrl', value);
-}
-
-function setapp_code(value) {
-    app_code = value;
-    updateapiConfigValue('app_code', value);
-}
-
-function setmodel(value) {
-    model = value
-    updateapiConfigValue('model', value);
-}
-
-module.exports = { sendMessageToAPI, setApiKey, setApiUrl, setapp_code , setmodel}
+module.exports = {updateGPTConfig }