diff --git a/ql.js b/ql.js index 22e5559..8317c82 100644 --- a/ql.js +++ b/ql.js @@ -1,7 +1,6 @@ 'use strict'; const got = require('got'); -const sqlite3 = require('sqlite3').verbose(); require('dotenv').config(); const { readFile } = require('fs/promises'); const path = require('path'); @@ -23,8 +22,6 @@ module.exports.getVersion = () => { Accept: 'application/json', }, }).then(response => { - //console.log('Response Status Code:', response.statusCode); - //console.log('Response Body:', response.body); return response.body.data.version; }).catch(error => { console.error('Error fetching version:', error.response ? error.response.body : error.message); @@ -37,20 +34,19 @@ let authFile = ""; (function initialize() { global.versionPromise = module.exports.getVersion(); global.versionPromise.then(version => { - console.log('当前青龙版本:', version+"\n"); - if (version >= '2.18.0') { - - authFile = "/ql/data/db/keyv.sqlite"; - - } else if(version < '2.12.0'){ - - authFile = "/ql/config/auth.json"; - - }else { - - authFile = "/ql/data/config/auth.json"; - - } + console.log('当前青龙版本:', version + "\n"); + if (version) { + if (version >= '2.18.0') { + authFile = "/ql/data/db/keyv.sqlite"; + } else if (version < '2.12.0') { + authFile = "/ql/config/auth.json"; + } else { + authFile = "/ql/data/config/auth.json"; + } + } else { + // 当检测不到版本号时,采用 version < '2.12.0' 的操作 + authFile = "/ql/config/auth.json"; + } }).catch(error => { console.error('Error after initialization:', error); }); @@ -59,9 +55,11 @@ let authFile = ""; async function getAuthFile() { await global.versionPromise; return authFile; + } async function getTokenFromSqlite(dbPath) { + const sqlite3 = require('sqlite3').verbose(); return new Promise((resolve, reject) => { const db = new sqlite3.Database(dbPath, (err) => { if (err) { @@ -105,24 +103,17 @@ async function getTokenFromSqlite(dbPath) { } async function getToken() { + const authFilePath = await getAuthFile(); + if (authFilePath.endsWith('keyv.sqlite')) { + return getTokenFromSqlite(authFilePath); + } else { + const authConfig = JSON.parse(await readFile(authFilePath)); + return authConfig.token; + } +} - const authFilePath = await getAuthFile(); - if (authFilePath.endsWith('keyv.sqlite')) { - - return getTokenFromSqlite(authFilePath); - - - } else { - - const authConfig = JSON.parse(await readFile(authFilePath)); - return authConfig.token; - - } - } - -module.exports.getEnvs = async () => { +module.exports.getEnvs = async () => { const token = await getToken(); - //console.log('当前token:', token); const body = await api({ url: 'api/envs', searchParams: { @@ -209,7 +200,7 @@ module.exports.DisableCk = async (eid) => { const body = await api({ method: 'put', url: 'api/envs/disable', - params: { t: Date.now() }, + params: { t: Date.now() }, body: JSON.stringify([eid]), headers: { Accept: 'application/json', @@ -225,7 +216,7 @@ module.exports.EnableCk = async (eid) => { const body = await api({ method: 'put', url: 'api/envs/enable', - params: { t: Date.now() }, + params: { t: Date.now() }, body: JSON.stringify([eid]), headers: { Accept: 'application/json', @@ -236,50 +227,50 @@ module.exports.EnableCk = async (eid) => { return body; }; -module.exports.getstatus = async(eid) => { - const envs = await this.getEnvs(); - var tempid = 0; - for (let i = 0; i < envs.length; i++) { - tempid = 0; - if (envs[i]._id) { - tempid = envs[i]._id; - } - if (envs[i].id) { - tempid = envs[i].id; - } - if (tempid == eid) { - return envs[i].status; - } +module.exports.getstatus = async (eid) => { + const envs = await this.getEnvs(); + var tempid = 0; + for (let i = 0; i < envs.length; i++) { + tempid = 0; + if (envs[i]._id) { + tempid = envs[i]._id; } - return 99; + if (envs[i].id) { + tempid = envs[i].id; + } + if (tempid == eid) { + return envs[i].status; + } + } + return 99; }; -module.exports.getEnvById = async(eid) => { - const envs = await this.getEnvs(); - var tempid = 0; - for (let i = 0; i < envs.length; i++) { - tempid = 0; - if (envs[i]._id) { - tempid = envs[i]._id; - } - if (envs[i].id) { - tempid = envs[i].id; - } - if (tempid == eid) { - return envs[i].value; - } +module.exports.getEnvById = async (eid) => { + const envs = await this.getEnvs(); + var tempid = 0; + for (let i = 0; i < envs.length; i++) { + tempid = 0; + if (envs[i]._id) { + tempid = envs[i]._id; } - return ""; + if (envs[i].id) { + tempid = envs[i].id; + } + if (tempid == eid) { + return envs[i].value; + } + } + return ""; }; module.exports.getEnvByPtPin = async (Ptpin) => { const envs = await this.getEnvs(); - for (let i = 0; i < envs.length; i++) { - var tempptpin = decodeURIComponent(envs[i].value.match(/pt_pin=([^; ]+)(?=;?)/) && envs[i].value.match(/pt_pin=([^; ]+)(?=;?)/)[1]); - if(tempptpin==Ptpin){ - return envs[i]; - } - } + for (let i = 0; i < envs.length; i++) { + var tempptpin = decodeURIComponent(envs[i].value.match(/pt_pin=([^; ]+)(?=;?)/) && envs[i].value.match(/pt_pin=([^; ]+)(?=;?)/)[1]); + if (tempptpin == Ptpin) { + return envs[i]; + } + } return ""; }; @@ -297,4 +288,4 @@ module.exports.delEnv = async (eid) => { }, }).json(); return body; -}; \ No newline at end of file +};