tools.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. generateCryptoNumericUUID= function(length = 10) {
  64. return crypto.randomInt(Math.pow(10, length - 1), Math.pow(10, length)).toString();
  65. }
  66. generateQMSignature = function(url, secretKey) {
  67. const urlObj = new URL(url);
  68. const params = Array.from(urlObj.searchParams.entries())
  69. .sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
  70. .map(([key, value]) => `${key}=${value}`)
  71. .join('&');
  72. // 拼接secret key
  73. const stringToSign = params + secretKey;
  74. console.log("stringToSign:",stringToSign)
  75. // 计算MD5
  76. const md5Hash = crypto.createHash('md5')
  77. .update(stringToSign)
  78. .digest('hex');
  79. return md5Hash;
  80. }
  81. getSign = function(distributorId,secretKey) {
  82. const params = [distributorId, secretKey, tools.getCurrentUnixTimestamp()];
  83. // 将参数数组中的每个元素转换为字符串并连接成一个单一的字符串
  84. const paramStr = params.map(String).join('');
  85. // 使用 MD5 算法生成哈希值
  86. const hash = crypto.createHash('md5');
  87. hash.update(paramStr);
  88. // 返回哈希值的十六进制表示
  89. return hash.digest('hex');
  90. }
  91. setHyToken(token){
  92. this.redis_help.setKeyValue("hei_yan_token",token)
  93. }
  94. async getHyToken (){
  95. return await this.redis_help.getKeyValue("hei_yan_token")
  96. }
  97. async getQMToken (){
  98. return await this.redis_help.getKeyValue("qi_mao_token")
  99. }
  100. async getQMMFToken (){
  101. return await this.redis_help.getKeyValue("qi_mao_mf_token")
  102. }
  103. getSupdate(){ //插入素材域名
  104. return config.isDebug?config.debug_supdate_config:config.release_supdate_config
  105. }
  106. getCheckDataBaseConfig(){
  107. return config.isDebug?config.debug_check_mysql:config.release_chekc_mysql
  108. }
  109. getDataBaseConfig(){
  110. return config.isDebug?config.debug_mysql:config.release_mysql
  111. }
  112. getTaskDataBaseConfig(){
  113. return config.isDebug?config.debug_task_mysql:config.release_task_mysql
  114. }
  115. getRandomElement(array) {
  116. const randomIndex = Math.floor(Math.random() * array.length);
  117. return array[randomIndex];
  118. }
  119. async getFqSidtt() {
  120. let sidtt = '01e060d0cc506bf1340dcb004aea1161'
  121. let list = await this.redis_help.getKeyValue("all_fq_key")
  122. if(list == null){
  123. return sidtt
  124. }
  125. list = JSON.parse(list)
  126. if(list.length<=0){
  127. return sidtt
  128. }
  129. let temp = []
  130. for (let index = 0; index < list.length; index++) {
  131. const sidtt = list[index];
  132. if(sidtt.canUse==1){
  133. temp.push(sidtt.sid_tt)
  134. }
  135. }
  136. return this.getRandomElement(temp)
  137. }
  138. async getFqMfSidtt() {
  139. let sidtt = '01e060d0cc506bf1340dcb004aea1161'
  140. let list = await this.redis_help.getKeyValue("all_fq_mf_key")
  141. if(list == null){
  142. return sidtt
  143. }
  144. list = JSON.parse(list)
  145. if(list.length<=0){
  146. return sidtt
  147. }
  148. let temp = []
  149. for (let index = 0; index < list.length; index++) {
  150. const sidtt = list[index];
  151. if(sidtt.canUse==1){
  152. temp.push(sidtt.sid_tt)
  153. }
  154. }
  155. return this.getRandomElement(temp)
  156. }
  157. async getYwOPENSESSID() {
  158. let open_sessid = '9077ffcc5ca974e2c0e78502a24c9053'
  159. let list = await this.redis_help.getKeyValue("all_yw_key")
  160. if(list == null){
  161. return open_sessid
  162. }
  163. list = JSON.parse(list)
  164. if(list.length<=0){
  165. return open_sessid
  166. }
  167. for (let index = 0; index < list.length; index++) {
  168. const yw_data = list[index];
  169. const old_time = yw_data.create_time;
  170. const current_time = moment();
  171. const past_time = moment(old_time);
  172. // 计算时间差(小时)
  173. const diff_hours = current_time.diff(past_time, 'hours');
  174. // 判断是否小于7小时
  175. const isLessThan7Hours = diff_hours < 7;
  176. if(isLessThan7Hours){
  177. this.redis_help.setKeyValue("OPENSESSID",yw_data.open_sessid)
  178. }else{
  179. this.redis_help.setKeyValue("OPENSESSID","")
  180. }
  181. return open_sessid
  182. }
  183. }
  184. getOneNewClinetBuffer(headers=null){
  185. return axios.create({
  186. timeout: 30000,
  187. headers:headers||{},
  188. responseType:"arraybuffer",
  189. // 使用独立的 agent,不影响其他连接
  190. httpAgent: new http.Agent({
  191. keepAlive: true,
  192. maxSockets: 5, // 允许适度的并发
  193. maxFreeSockets: 2,
  194. timeout: 30000
  195. }),
  196. validateStatus: function (status) {
  197. return status >= 200 && status < 300;
  198. }
  199. });
  200. }
  201. getOneNewClinet(headers=null){
  202. return axios.create({
  203. timeout: 30000,
  204. headers:headers||{},
  205. // 使用独立的 agent,不影响其他连接
  206. httpAgent: new http.Agent({
  207. keepAlive: true,
  208. maxSockets: 5, // 允许适度的并发
  209. maxFreeSockets: 2,
  210. timeout: 30000
  211. }),
  212. validateStatus: function (status) {
  213. return status >= 200 && status < 300;
  214. }
  215. });
  216. }
  217. }
  218. module.exports = new tools(require('./src/use_redis'));