tools.js 6.9 KB

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