whyour / qinglong

支持 Python3、JavaScript、Shell、Typescript 的定时任务管理平台(Timed task management platform supporting Python3, JavaScript, Shell, Typescript)
https://qinglong.online
Apache License 2.0
15.66k stars 2.91k forks source link

请问如何进行可变的状态存储? #2329

Open aqiu9 opened 5 months ago

aqiu9 commented 5 months ago

Clear and concise description of the problem

感谢您的付出!

想请教您一个问题:我希望脚本A在不断定时执行时,可以查询并更新某一个脚本外的状态变量,就像环境变量一样,但是我没有找到在代码里动态更新环境变量值的方法。这种情况您更推荐怎么处理呢?(比如自行编写工具类来操作db目录里的数据库?写为一个文件?还是自己随便想办法?)

因为没有找到青龙关于”脚本编写时的trick“之类的文档,但又希望找到一个简洁的解决办法,不得已来打扰您。万分感谢。

祝您生活开心!

Suggested solution

提供动态更新环境变量值的魔法命令或简易API

Alternative

No response

Additional context

No response

Validations

ZiDuNet commented 5 months ago

直接调用青龙API接口,操作变量,如: --获取Token-- const axios = require('axios');

class QinglongAuth { constructor(panelAddress, clientId, clientSecret) { this.panelAddress = panelAddress; this.clientId = clientId; this.clientSecret = clientSecret; }

async getToken() {
    const getTokenUrl = `http://${this.panelAddress}/open/auth/token`;
    const params = new URLSearchParams();
    params.append('client_id', this.clientId);
    params.append('client_secret', this.clientSecret);

    try {
        const response = await axios.get(getTokenUrl, { params });
        console.log(response.data.data.token);
        return response.data.data.token; // 假设返回JSON中token字段存放的是access_token

    } catch (error) {
        console.error('获取Token失败:', error);
        throw new Error('Failed to fetch Qinglong token');
    }
}

} module.exports = QinglongAuth;` ---变量修改-- /**

const qinglongAuth = new QinglongAuth('127.0.0.1:5700', 'PXrKv4U-FZP2', 'rLliZeBywuYjaHUcf30l5I');

async function updateEnvironmentVariable(newValue, varName, remarks, varId) { const updateEnvUrl = http://127.0.0.1:5700/open/envs; let headers = {};

try {
    const token = await qinglongAuth.getToken();
    if (token) {
        headers = {
            Authorization: `Bearer ${token}`,
            'User-Agent': 'WuShuo/1.0.0 (http://www.4dbim.ren/)',
            'Content-Type': 'application/json'
        };

        const envVarUpdateData = {
            value: newValue,
            name: varName,
            remarks: remarks,
            id: varId
        };

        try {
            const response = await axios.put(updateEnvUrl, envVarUpdateData, { headers });
            return response.data; // 返回更新结果
        } catch (error) {
            console.error('更新环境变量时出错:', error);
            throw new Error('Failed to update environment variable');
        }
    } else {
        throw new Error('Unable to fetch token, cannot update environment variable');
    }
} catch (getTokenError) {
    console.error('获取Token时出错:', getTokenError);
    throw new Error('Failed to fetch token');
}

}

module.exports = updateEnvironmentVariable; //调用青龙API更新Token /*const updateEnvironmentVariable = require('./qinglongEnvironmentUpdater'); (async () => { const newValue = '变量值'; const varName = '变量名'; const remarks = '备注'; const varId = '1'; // 替换为实际的环境变量ID

try {
    const result = await updateEnvironmentVariable(newValue, varName, remarks, varId);
    console.log('环境变量更新结果:', result);
} catch (error) {
    console.error('更新环境变量过程中出现错误:', error.message);
}

})();*/

ZiDuNet commented 5 months ago

再不行你写出到JSON到本地不就完了。。。。

aqiu9 commented 5 months ago

直接调用青龙API接口,操作变量,如: --获取Token-- const axios = require('axios');

class QinglongAuth { constructor(panelAddress, clientId, clientSecret) { this.panelAddress = panelAddress; this.clientId = clientId; this.clientSecret = clientSecret; }

async getToken() {
    const getTokenUrl = `http://${this.panelAddress}/open/auth/token`;
    const params = new URLSearchParams();
    params.append('client_id', this.clientId);
    params.append('client_secret', this.clientSecret);

    try {
        const response = await axios.get(getTokenUrl, { params });
        console.log(response.data.data.token);
        return response.data.data.token; // 假设返回JSON中token字段存放的是access_token

    } catch (error) {
        console.error('获取Token失败:', error);
        throw new Error('Failed to fetch Qinglong token');
    }
}

} module.exports = QinglongAuth;` ---变量修改-- /**

  • @description 更新面板环境变量
  • 引入:const updateEnvironmentVariable = require('../青龙API/环境变量/updateEnvironmentVariable');
  • 调用:const result = new updateEnvironmentVariable(值, 变量名, 备注, ID); */ const axios = require('axios'); const QinglongAuth = require('../Token/qinglongAuth');

const qinglongAuth = new QinglongAuth('127.0.0.1:5700', 'PXrKv4U-FZP2', 'rLliZeBywuYjaHUcf30l5I');

async function updateEnvironmentVariable(newValue, varName, remarks, varId) { const updateEnvUrl = http://127.0.0.1:5700/open/envs; let headers = {};

try {
    const token = await qinglongAuth.getToken();
    if (token) {
        headers = {
            Authorization: `Bearer ${token}`,
            'User-Agent': 'WuShuo/1.0.0 (http://www.4dbim.ren/)',
            'Content-Type': 'application/json'
        };

        const envVarUpdateData = {
            value: newValue,
            name: varName,
            remarks: remarks,
            id: varId
        };

        try {
            const response = await axios.put(updateEnvUrl, envVarUpdateData, { headers });
            return response.data; // 返回更新结果
        } catch (error) {
            console.error('更新环境变量时出错:', error);
            throw new Error('Failed to update environment variable');
        }
    } else {
        throw new Error('Unable to fetch token, cannot update environment variable');
    }
} catch (getTokenError) {
    console.error('获取Token时出错:', getTokenError);
    throw new Error('Failed to fetch token');
}

}

module.exports = updateEnvironmentVariable; //调用青龙API更新Token /*const updateEnvironmentVariable = require('./qinglongEnvironmentUpdater'); (async () => { const newValue = '变量值'; const varName = '变量名'; const remarks = '备注'; const varId = '1'; // 替换为实际的环境变量ID

try {
    const result = await updateEnvironmentVariable(newValue, varName, remarks, varId);
    console.log('环境变量更新结果:', result);
} catch (error) {
    console.error('更新环境变量过程中出现错误:', error.message);
}

})();*/

谢谢!请问clientid和clientsecret怎么获取?

aqiu9 commented 4 months ago

今天看了看源码,算是解决了这个问题。 期待官方在未来的大版本里提供一个简洁优雅的解决方案!