faker3/ql.js

218 lines
5.6 KiB
JavaScript
Raw Normal View History

2025-03-10 07:30:59 +08:00
'use strict'
2024-11-12 11:17:55 +08:00
2025-03-10 07:30:59 +08:00
const got = require('got')
require('dotenv').config()
const { readFile } = require('fs/promises')
const path = require('path')
2024-11-12 11:17:55 +08:00
2025-03-10 07:30:59 +08:00
const qlDir = '/ql'
const fs = require('fs')
const tokenFileList = ['/ql/data/db/keyv.sqlite', '/ql/data/config/auth.json', '/ql/config/auth.json']
2024-11-12 11:17:55 +08:00
2025-03-10 07:30:59 +08:00
//const authFile = path.join(qlDir, 'config/auth.json');
let authFile = getLatestFile(tokenFileList)
2024-11-12 11:17:55 +08:00
const api = got.extend({
2025-03-10 07:30:59 +08:00
prefixUrl: 'http://127.0.0.1:5600',
retry: { limit: 0 },
})
function getLatestFile(files) {
let latestFile = null
let latestMtime = 0
for (const file of files) {
try {
const stats = fs.statSync(file)
const mtime = stats.mtimeMs
if (mtime > latestMtime) {
latestMtime = mtime
latestFile = file
2025-03-04 14:40:41 +08:00
}
2025-03-10 07:30:59 +08:00
} catch (e) {}
}
return latestFile
2025-03-04 14:40:41 +08:00
}
async function getToken() {
2025-03-10 07:30:59 +08:00
const authConfig = await readFile(authFile)
// console.log(authConfig.toString().match(/"token":"(.*?)",/)[1])
return authConfig.toString().match(/"token":"(.*?)",/)[1]
2025-03-06 09:27:58 +08:00
}
2025-03-04 14:40:41 +08:00
2025-03-06 09:27:58 +08:00
module.exports.getEnvs = async () => {
2025-03-10 07:30:59 +08:00
const token = await getToken()
const body = await api({
url: 'api/envs',
searchParams: {
searchValue: 'JD_COOKIE',
t: Date.now(),
},
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
},
}).json()
return body.data
}
2024-11-12 11:17:55 +08:00
module.exports.getEnvsCount = async () => {
2025-03-10 07:30:59 +08:00
const data = await this.getEnvs()
return data.length
}
2024-11-12 11:17:55 +08:00
module.exports.addEnv = async (cookie, remarks) => {
2025-03-10 07:30:59 +08:00
const token = await getToken()
const body = await api({
method: 'post',
url: 'api/envs',
params: { t: Date.now() },
json: [
{
name: 'JD_COOKIE',
value: cookie,
remarks,
},
],
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json()
return body
}
2024-11-12 11:17:55 +08:00
module.exports.updateEnv = async (cookie, eid, remarks) => {
2025-03-10 07:30:59 +08:00
const token = await getToken()
const body = await api({
method: 'put',
url: 'api/envs',
params: { t: Date.now() },
json: {
name: 'JD_COOKIE',
value: cookie,
_id: eid,
remarks,
},
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json()
return body
}
2024-11-12 11:17:55 +08:00
module.exports.updateEnv11 = async (cookie, eid, remarks) => {
2025-03-10 07:30:59 +08:00
const token = await getToken()
const body = await api({
method: 'put',
url: 'api/envs',
params: { t: Date.now() },
json: {
name: 'JD_COOKIE',
value: cookie,
id: eid,
remarks,
},
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json()
return body
}
2024-11-12 11:17:55 +08:00
module.exports.DisableCk = async (eid) => {
2025-03-10 07:30:59 +08:00
const token = await getToken()
const body = await api({
method: 'put',
url: 'api/envs/disable',
params: { t: Date.now() },
body: JSON.stringify([eid]),
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json()
return body
}
2024-11-12 11:17:55 +08:00
module.exports.EnableCk = async (eid) => {
2025-03-10 07:30:59 +08:00
const token = await getToken()
const body = await api({
method: 'put',
url: 'api/envs/enable',
params: { t: Date.now() },
body: JSON.stringify([eid]),
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json()
return body
}
2024-11-12 11:17:55 +08:00
2025-03-06 09:27:58 +08:00
module.exports.getstatus = async (eid) => {
2025-03-10 07:30:59 +08:00
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
}
2025-03-06 09:27:58 +08:00
}
2025-03-10 07:30:59 +08:00
return 99
}
2024-11-12 11:17:55 +08:00
2025-03-06 09:27:58 +08:00
module.exports.getEnvById = async (eid) => {
2025-03-10 07:30:59 +08:00
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
}
2024-11-12 11:17:55 +08:00
}
2025-03-10 07:30:59 +08:00
return ''
}
2024-11-12 11:17:55 +08:00
module.exports.getEnvByPtPin = async (Ptpin) => {
2025-03-10 07:30:59 +08:00
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]
}
2025-03-06 09:27:58 +08:00
}
2025-03-10 07:30:59 +08:00
return ''
}
2024-11-12 11:17:55 +08:00
module.exports.delEnv = async (eid) => {
2025-03-10 07:30:59 +08:00
const token = await getToken()
const body = await api({
method: 'delete',
url: 'api/envs',
params: { t: Date.now() },
body: JSON.stringify([eid]),
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json()
return body
}