tools.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. const crypto = require('crypto');
  2. const config = require('./etc/config.json');
  3. const { default: axios } = require('axios');
  4. const http = require('http');
  5. class tools {
  6. constructor(redis_help){
  7. this.redis_help = redis_help
  8. }
  9. init(){
  10. }
  11. distributorId = 1814786227164169;
  12. secretKey = 'CN6KQ8Bauo8JXg5fFPk86EHdRFIUVnyV';
  13. heiyan_config(){
  14. return { //黑岩配置
  15. chang_pian_user:{
  16. userName:"康帅",
  17. password:"Ks25666"
  18. },
  19. duan_pian_user:{
  20. userName:"王海泉",
  21. password:"My20240088"
  22. },
  23. default_user:{
  24. userName:"zhuoyue003",
  25. password:"Xuan2026@123"
  26. },
  27. }
  28. }
  29. unixTimestampToDate = function(timestamp) {
  30. const date = new Date(timestamp * 1000); // Unix时间戳是秒,JavaScript的Date对象需要毫秒
  31. return date.getTime();
  32. }
  33. dateToUnixTimestamp = function (date) {
  34. return Math.floor(date.getTime() / 1000); // 将毫秒转换为秒
  35. }
  36. calculateTimestampDifference = function(timestamp1, timestamp2) {
  37. return Math.abs(timestamp1 - timestamp2);
  38. }
  39. formatUnixTimestamp = function(timestamp, format = 'YYYY-MM-DD HH:mm:ss') {
  40. const date = new Date(timestamp * 1000);
  41. const year = date.getFullYear();
  42. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要+1
  43. const day = String(date.getDate()).padStart(2, '0');
  44. const hours = String(date.getHours()).padStart(2, '0');
  45. const minutes = String(date.getMinutes()).padStart(2, '0');
  46. const seconds = String(date.getSeconds()).padStart(2, '0');
  47. const formattedDate = format
  48. .replace('YYYY', year)
  49. .replace('MM', month)
  50. .replace('DD', day)
  51. .replace('HH', hours)
  52. .replace('mm', minutes)
  53. .replace('ss', seconds);
  54. return formattedDate;
  55. }
  56. getCurrentUnixTimestamp = function() {
  57. return Math.floor(Date.now() / 1000)
  58. }
  59. getCurrentUnixTimestamp = function() {
  60. return Math.floor(Date.now() / 1000)
  61. }
  62. getSign = function(distributorId,secretKey) {
  63. const params = [distributorId, secretKey, tools.getCurrentUnixTimestamp()];
  64. // 将参数数组中的每个元素转换为字符串并连接成一个单一的字符串
  65. const paramStr = params.map(String).join('');
  66. // 使用 MD5 算法生成哈希值
  67. const hash = crypto.createHash('md5');
  68. hash.update(paramStr);
  69. // 返回哈希值的十六进制表示
  70. return hash.digest('hex');
  71. }
  72. setHyToken(token){
  73. this.redis_help.setKeyValue("hei_yan_token",token)
  74. }
  75. async getHyToken (){
  76. return await this.redis_help.getKeyValue("hei_yan_token")
  77. }
  78. async getQMToken (){
  79. return await this.redis_help.getKeyValue("qi_mao_token")
  80. }
  81. getSupdate(){ //插入素材域名
  82. return config.isDebug?config.debug_supdate_config:config.release_supdate_config
  83. }
  84. getCheckDataBaseConfig(){
  85. return config.isDebug?config.debug_check_mysql:config.release_chekc_mysql
  86. }
  87. getDataBaseConfig(){
  88. return config.isDebug?config.debug_mysql:config.release_mysql
  89. }
  90. getTaskDataBaseConfig(){
  91. return config.isDebug?config.debug_task_mysql:config.release_task_mysql
  92. }
  93. getRandomElement(array) {
  94. const randomIndex = Math.floor(Math.random() * array.length);
  95. return array[randomIndex];
  96. }
  97. async getFqSidtt() {
  98. let sidtt = '01e060d0cc506bf1340dcb004aea1161'
  99. let list = await this.redis_help.getKeyValue("all_fq_key")
  100. if(list == null){
  101. return sidtt
  102. }
  103. list = JSON.parse(list)
  104. if(list.length<=0){
  105. return sidtt
  106. }
  107. let temp = []
  108. for (let index = 0; index < list.length; index++) {
  109. const sidtt = list[index];
  110. if(sidtt.canUse==1){
  111. temp.push(sidtt.sid_tt)
  112. }
  113. }
  114. return this.getRandomElement(temp)
  115. }
  116. async getFqMfSidtt() {
  117. let sidtt = '01e060d0cc506bf1340dcb004aea1161'
  118. let list = await this.redis_help.getKeyValue("all_fq_mf_key")
  119. if(list == null){
  120. return sidtt
  121. }
  122. list = JSON.parse(list)
  123. if(list.length<=0){
  124. return sidtt
  125. }
  126. let temp = []
  127. for (let index = 0; index < list.length; index++) {
  128. const sidtt = list[index];
  129. if(sidtt.canUse==1){
  130. temp.push(sidtt.sid_tt)
  131. }
  132. }
  133. return this.getRandomElement(temp)
  134. }
  135. getOneNewClinet(headers=null){
  136. return axios.create({
  137. timeout: 30000,
  138. headers:headers||{},
  139. // 使用独立的 agent,不影响其他连接
  140. httpAgent: new http.Agent({
  141. keepAlive: true,
  142. maxSockets: 5, // 允许适度的并发
  143. maxFreeSockets: 2,
  144. timeout: 30000
  145. }),
  146. validateStatus: function (status) {
  147. return status >= 200 && status < 300;
  148. }
  149. });
  150. }
  151. }
  152. module.exports = new tools(require('./src/use_redis'));