123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- const crypto = require('crypto');
- const config = require('./etc/config.json');
- const { default: axios } = require('axios');
- const http = require('http');
- const moment = require('moment');
- const mysql = require('mysql2/promise');
- 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)
- }
- generateCryptoNumericUUID= function(length = 10) {
- return crypto.randomInt(Math.pow(10, length - 1), Math.pow(10, length)).toString();
- }
- generateQMSignature = function(url, secretKey) {
- const urlObj = new URL(url);
- const params = Array.from(urlObj.searchParams.entries())
- .sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
- .map(([key, value]) => `${key}=${value}`)
- .join('&');
- // 拼接secret key
- const stringToSign = params + secretKey;
-
- console.log("stringToSign:",stringToSign)
- // 计算MD5
- const md5Hash = crypto.createHash('md5')
- .update(stringToSign)
- .digest('hex');
- return md5Hash;
- }
- 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)
- }
- async getYwOPENSESSID() {
- let open_sessid = '9077ffcc5ca974e2c0e78502a24c9053'
- let list = await this.redis_help.getKeyValue("all_yw_key")
- if(list == null){
- return open_sessid
- }
- list = JSON.parse(list)
- if(list.length<=0){
- return open_sessid
- }
- for (let index = 0; index < list.length; index++) {
- const yw_data = list[index];
- const old_time = yw_data.create_time;
- const current_time = moment();
- const past_time = moment(old_time);
- // 计算时间差(小时)
- const diff_hours = current_time.diff(past_time, 'hours');
- // 判断是否小于7小时
- const isLessThan7Hours = diff_hours < 7;
- if(isLessThan7Hours){
- this.redis_help.setKeyValue("OPENSESSID",yw_data.open_sessid)
- }else{
- this.redis_help.setKeyValue("OPENSESSID","")
- }
- return open_sessid
- }
- }
- getOneNewClinetBuffer(headers=null){
- return axios.create({
- timeout: 30000,
- headers:headers||{},
- responseType:"arraybuffer",
- // 使用独立的 agent,不影响其他连接
- httpAgent: new http.Agent({
- keepAlive: true,
- maxSockets: 5, // 允许适度的并发
- maxFreeSockets: 2,
- timeout: 30000
- }),
- validateStatus: function (status) {
- return status >= 200 && status < 300;
- }
- });
- }
- 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;
- }
- });
- }
- async getAppletProductDataByButlerId(butler_id,product_id,main_id){
- const taskdbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql
- let connection = null
- try{
- let connection = await mysql.createConnection({
- ...taskdbConfig,
- multipleStatements: true
- });
- const [rows] = await connection.execute(
- `SELECT * FROM video_applet_product_${butler_id} WHERE product_id = '${product_id}' AND main_id = ${main_id} LIMIT 1`
- );
- if(rows.length<=0){
- return null
- }
- return rows[0]
- }catch(e){
- return null
- }finally{
- if(connection!=null){
- await connection.end();
- }
- }
- }
- diffTime(last_d,new_d){
- const date1 = new Date(last_d);
- const date2 = new Date(new_d);
- if (date2.valueOf() > date1.valueOf()) {
- return new_d
- }
- return null
- }
- async setDzMfCookie(cookit){
- return this.redis_help.setKeyValue("DZ_MF_COOKIT",cookit)
- }
- async getDzMfCookit(){
- return this.redis_help.getKeyValue("DZ_MF_COOKIT")
- }
- async setDzCookie(cookit){
- return this.redis_help.setKeyValue("DZ_COOKIT",cookit)
- }
- async getDzCookit(){
- return this.redis_help.getKeyValue("DZ_COOKIT")
- }
- getNewBookName (bookName){
- return bookName.replace(",", ",").replace("。", "." ).replace(";", ";")
- }
- isMoreThanTwoDays(targetDate) {
- // 获取当前时间
- const now = new Date();
-
- // 确保传入的参数是有效的 Date 对象
- if (!(targetDate instanceof Date) || isNaN(targetDate.getTime())) {
- throw new Error('Invalid date provided');
- }
-
- // 计算时间差(毫秒)
- const timeDiff = now.getTime() - targetDate.getTime();
-
- // 将毫秒转换为天数
- const daysDiff = Math.abs(timeDiff / (1000 * 60 * 60 * 24));
-
- // 如果相差小于2天返回false,否则返回true
- return daysDiff >= 2;
- }
- }
- module.exports = new tools(require('./src/use_redis'));
- //123
|