From 0657bdb33ee0accb8525c9ad1dc9fe29890e1711 Mon Sep 17 00:00:00 2001 From: yyds Date: Sat, 5 Mar 2022 22:13:31 +0800 Subject: [PATCH] fix --- function/ql.js | 149 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 function/ql.js diff --git a/function/ql.js b/function/ql.js new file mode 100644 index 0000000..db832c9 --- /dev/null +++ b/function/ql.js @@ -0,0 +1,149 @@ +'use strict'; + +const got = require('got'); +require('dotenv').config(); +const { readFile } = require('fs/promises'); +const path = require('path'); + +const qlDir = '/ql'; +const authFile = path.join(qlDir, 'config/auth.json'); + +const api = got.extend({ + prefixUrl: 'http://localhost:5600', + retry: { limit: 0 }, +}); + +async function getToken() { + const authConfig = JSON.parse(await readFile(authFile)); + return authConfig.token; +} + +module.exports.getEnvs = async () => { + 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; +}; + +module.exports.getEnvsCount = async () => { + const data = await this.getEnvs(); + return data.length; +}; + +module.exports.addEnv = async (cookie, remarks) => { + 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; +}; + +module.exports.updateEnv = async (cookie, eid, remarks) => { + 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; +}; + +module.exports.DisableCk = async (eid) => { + 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; +}; + +module.exports.EnableCk = async (eid) => { + 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; +}; + +module.exports.getstatus = async (eid) => { + const envs = await this.getEnvs(); + for (let i = 0; i < envs.length; i++) { + if(envs[i]._id==eid){ + return envs[i].status; + } + } + return 99; +}; + +module.exports.getEnvById = async (eid) => { + const envs = await this.getEnvs(); + for (let i = 0; i < envs.length; i++) { + if(envs[i]._id==eid){ + return envs[i].value; + } + } + return ""; +}; + +module.exports.delEnv = async (eid) => { + 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; +};