123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- const crypto = require('crypto');
- const config = require('./etc/config.json');
- const { default: axios } = require('axios');
- const http = require('http');
- class tools {
- constructor(redis_help){
- this.redis_help = redis_help
- }
- init(){
- }
- distributorId = 1814786227164169;
- secretKey = 'CN6KQ8Bauo8JXg5fFPk86EHdRFIUVnyV';
- heiyan_config(){
- return { //黑岩配置
- chang_pian_user:{
- userName:"康帅",
- password:"Ks25666"
- },
- duan_pian_user:{
- userName:"王海泉",
- password:"My20240088"
- },
- default_user:{
- userName:"zhuoyue003",
- password:"Xuan2026@123"
- },
- }
- }
- unixTimestampToDate = function(timestamp) {
- const date = new Date(timestamp * 1000); // Unix时间戳是秒,JavaScript的Date对象需要毫秒
- return date.getTime();
- }
- dateToUnixTimestamp = function (date) {
- return Math.floor(date.getTime() / 1000); // 将毫秒转换为秒
- }
-
- calculateTimestampDifference = function(timestamp1, timestamp2) {
- return Math.abs(timestamp1 - timestamp2);
- }
- formatUnixTimestamp = function(timestamp, format = 'YYYY-MM-DD HH:mm:ss') {
- const date = new Date(timestamp * 1000);
- const year = date.getFullYear();
- const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要+1
- const day = String(date.getDate()).padStart(2, '0');
- const hours = String(date.getHours()).padStart(2, '0');
- const minutes = String(date.getMinutes()).padStart(2, '0');
- const seconds = String(date.getSeconds()).padStart(2, '0');
-
- const formattedDate = format
- .replace('YYYY', year)
- .replace('MM', month)
- .replace('DD', day)
- .replace('HH', hours)
- .replace('mm', minutes)
- .replace('ss', seconds);
-
- return formattedDate;
- }
- getCurrentUnixTimestamp = function() {
- return Math.floor(Date.now() / 1000)
- }
- getCurrentUnixTimestamp = function() {
- return Math.floor(Date.now() / 1000)
- }
- getSign = function(distributorId,secretKey) {
- const params = [distributorId, secretKey, tools.getCurrentUnixTimestamp()];
- // 将参数数组中的每个元素转换为字符串并连接成一个单一的字符串
- const paramStr = params.map(String).join('');
-
- // 使用 MD5 算法生成哈希值
- const hash = crypto.createHash('md5');
- hash.update(paramStr);
-
- // 返回哈希值的十六进制表示
- return hash.digest('hex');
- }
-
- setHyToken(token){
- this.redis_help.setKeyValue("hei_yan_token",token)
- }
- async getHyToken (){
- return await this.redis_help.getKeyValue("hei_yan_token")
- }
- async getQMToken (){
- return await this.redis_help.getKeyValue("qi_mao_token")
- }
- getSupdate(){ //插入素材域名
- return config.isDebug?config.debug_supdate_config:config.release_supdate_config
- }
- getCheckDataBaseConfig(){
- return config.isDebug?config.debug_check_mysql:config.release_chekc_mysql
- }
- getDataBaseConfig(){
- return config.isDebug?config.debug_mysql:config.release_mysql
- }
- getTaskDataBaseConfig(){
- return config.isDebug?config.debug_task_mysql:config.release_task_mysql
- }
-
- getRandomElement(array) {
- const randomIndex = Math.floor(Math.random() * array.length);
- return array[randomIndex];
- }
- async getFqSidtt() {
- let sidtt = '01e060d0cc506bf1340dcb004aea1161'
- let list = await this.redis_help.getKeyValue("all_fq_key")
- if(list == null){
- return sidtt
- }
- list = JSON.parse(list)
- if(list.length<=0){
- return sidtt
- }
- let temp = []
- for (let index = 0; index < list.length; index++) {
- const sidtt = list[index];
- if(sidtt.canUse==1){
- temp.push(sidtt.sid_tt)
- }
- }
- return this.getRandomElement(temp)
- }
- async getFqMfSidtt() {
- let sidtt = '01e060d0cc506bf1340dcb004aea1161'
- let list = await this.redis_help.getKeyValue("all_fq_mf_key")
- if(list == null){
- return sidtt
- }
- list = JSON.parse(list)
- if(list.length<=0){
- return sidtt
- }
- let temp = []
- for (let index = 0; index < list.length; index++) {
- const sidtt = list[index];
- if(sidtt.canUse==1){
- temp.push(sidtt.sid_tt)
- }
- }
- return this.getRandomElement(temp)
- }
- getOneNewClinet(headers=null){
- return axios.create({
- timeout: 30000,
- headers:headers||{},
- // 使用独立的 agent,不影响其他连接
- httpAgent: new http.Agent({
- keepAlive: true,
- maxSockets: 5, // 允许适度的并发
- maxFreeSockets: 2,
- timeout: 30000
- }),
- validateStatus: function (status) {
- return status >= 200 && status < 300;
- }
- });
- }
- }
- module.exports = new tools(require('./src/use_redis'));
|