tools.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. const mysql = require('mysql2/promise');
  7. class tools {
  8. constructor(redis_help){
  9. this.redis_help = redis_help
  10. }
  11. init(){
  12. }
  13. distributorId = 1814786227164169;
  14. secretKey = 'CN6KQ8Bauo8JXg5fFPk86EHdRFIUVnyV';
  15. heiyan_config(){
  16. return { //黑岩配置
  17. chang_pian_user:{
  18. userName:"康帅",
  19. password:"Ks25666"
  20. },
  21. duan_pian_user:{
  22. userName:"王海泉",
  23. password:"My20240088"
  24. },
  25. default_user:{
  26. userName:"zhuoyue003",
  27. password:"Xuan2026@123"
  28. },
  29. }
  30. }
  31. unixTimestampToDate = function(timestamp) {
  32. const date = new Date(timestamp * 1000); // Unix时间戳是秒,JavaScript的Date对象需要毫秒
  33. return date.getTime();
  34. }
  35. dateToUnixTimestamp = function (date) {
  36. return Math.floor(date.getTime() / 1000); // 将毫秒转换为秒
  37. }
  38. calculateTimestampDifference = function(timestamp1, timestamp2) {
  39. return Math.abs(timestamp1 - timestamp2);
  40. }
  41. formatUnixTimestamp = function(timestamp, format = 'YYYY-MM-DD HH:mm:ss') {
  42. const date = new Date(timestamp * 1000);
  43. const year = date.getFullYear();
  44. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要+1
  45. const day = String(date.getDate()).padStart(2, '0');
  46. const hours = String(date.getHours()).padStart(2, '0');
  47. const minutes = String(date.getMinutes()).padStart(2, '0');
  48. const seconds = String(date.getSeconds()).padStart(2, '0');
  49. const formattedDate = format
  50. .replace('YYYY', year)
  51. .replace('MM', month)
  52. .replace('DD', day)
  53. .replace('HH', hours)
  54. .replace('mm', minutes)
  55. .replace('ss', seconds);
  56. return formattedDate;
  57. }
  58. getCurrentUnixTimestamp = function() {
  59. return Math.floor(Date.now() / 1000)
  60. }
  61. getCurrentUnixTimestamp = function() {
  62. return Math.floor(Date.now() / 1000)
  63. }
  64. generateCryptoNumericUUID= function(length = 10) {
  65. return crypto.randomInt(Math.pow(10, length - 1), Math.pow(10, length)).toString();
  66. }
  67. generateQMSignature = function(url, secretKey) {
  68. const urlObj = new URL(url);
  69. const params = Array.from(urlObj.searchParams.entries())
  70. .sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
  71. .map(([key, value]) => `${key}=${value}`)
  72. .join('&');
  73. // 拼接secret key
  74. const stringToSign = params + secretKey;
  75. console.log("stringToSign:",stringToSign)
  76. // 计算MD5
  77. const md5Hash = crypto.createHash('md5')
  78. .update(stringToSign)
  79. .digest('hex');
  80. return md5Hash;
  81. }
  82. getSign = function(distributorId,secretKey) {
  83. const params = [distributorId, secretKey, tools.getCurrentUnixTimestamp()];
  84. // 将参数数组中的每个元素转换为字符串并连接成一个单一的字符串
  85. const paramStr = params.map(String).join('');
  86. // 使用 MD5 算法生成哈希值
  87. const hash = crypto.createHash('md5');
  88. hash.update(paramStr);
  89. // 返回哈希值的十六进制表示
  90. return hash.digest('hex');
  91. }
  92. setHyToken(token){
  93. this.redis_help.setKeyValue("hei_yan_token",token)
  94. }
  95. async getHyToken (){
  96. return await this.redis_help.getKeyValue("hei_yan_token")
  97. }
  98. async getQMToken (){
  99. return await this.redis_help.getKeyValue("qi_mao_token")
  100. }
  101. getSupdate(){ //插入素材域名
  102. return config.isDebug?config.debug_supdate_config:config.release_supdate_config
  103. }
  104. getCheckDataBaseConfig(){
  105. return config.isDebug?config.debug_check_mysql:config.release_chekc_mysql
  106. }
  107. getDataBaseConfig(){
  108. return config.isDebug?config.debug_mysql:config.release_mysql
  109. }
  110. getTaskDataBaseConfig(){
  111. return config.isDebug?config.debug_task_mysql:config.release_task_mysql
  112. }
  113. getRandomElement(array) {
  114. const randomIndex = Math.floor(Math.random() * array.length);
  115. return array[randomIndex];
  116. }
  117. async getFqSidtt() {
  118. let sidtt = '01e060d0cc506bf1340dcb004aea1161'
  119. let list = await this.redis_help.getKeyValue("all_fq_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 getFqMfSidtt() {
  137. let sidtt = '01e060d0cc506bf1340dcb004aea1161'
  138. let list = await this.redis_help.getKeyValue("all_fq_mf_key")
  139. if(list == null){
  140. return sidtt
  141. }
  142. list = JSON.parse(list)
  143. if(list.length<=0){
  144. return sidtt
  145. }
  146. let temp = []
  147. for (let index = 0; index < list.length; index++) {
  148. const sidtt = list[index];
  149. if(sidtt.canUse==1){
  150. temp.push(sidtt.sid_tt)
  151. }
  152. }
  153. return this.getRandomElement(temp)
  154. }
  155. async getYwOPENSESSID() {
  156. let open_sessid = '9077ffcc5ca974e2c0e78502a24c9053'
  157. let list = await this.redis_help.getKeyValue("all_yw_key")
  158. if(list == null){
  159. return open_sessid
  160. }
  161. list = JSON.parse(list)
  162. if(list.length<=0){
  163. return open_sessid
  164. }
  165. for (let index = 0; index < list.length; index++) {
  166. const yw_data = list[index];
  167. const old_time = yw_data.create_time;
  168. const current_time = moment();
  169. const past_time = moment(old_time);
  170. // 计算时间差(小时)
  171. const diff_hours = current_time.diff(past_time, 'hours');
  172. // 判断是否小于7小时
  173. const isLessThan7Hours = diff_hours < 7;
  174. if(isLessThan7Hours){
  175. this.redis_help.setKeyValue("OPENSESSID",yw_data.open_sessid)
  176. }else{
  177. this.redis_help.setKeyValue("OPENSESSID","")
  178. }
  179. return open_sessid
  180. }
  181. }
  182. getOneNewClinetBuffer(headers=null){
  183. return axios.create({
  184. timeout: 30000,
  185. headers:headers||{},
  186. responseType:"arraybuffer",
  187. // 使用独立的 agent,不影响其他连接
  188. httpAgent: new http.Agent({
  189. keepAlive: true,
  190. maxSockets: 5, // 允许适度的并发
  191. maxFreeSockets: 2,
  192. timeout: 30000
  193. }),
  194. validateStatus: function (status) {
  195. return status >= 200 && status < 300;
  196. }
  197. });
  198. }
  199. getOneNewClinet(headers=null){
  200. return axios.create({
  201. timeout: 30000,
  202. headers:headers||{},
  203. // 使用独立的 agent,不影响其他连接
  204. httpAgent: new http.Agent({
  205. keepAlive: true,
  206. maxSockets: 5, // 允许适度的并发
  207. maxFreeSockets: 2,
  208. timeout: 30000
  209. }),
  210. validateStatus: function (status) {
  211. return status >= 200 && status < 300;
  212. }
  213. });
  214. }
  215. async getAppletProductDataByButlerId(butler_id,product_id,main_id){
  216. const taskdbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql
  217. let connection = null
  218. try{
  219. let connection = await mysql.createConnection({
  220. ...taskdbConfig,
  221. multipleStatements: true
  222. });
  223. const [rows] = await connection.execute(
  224. `SELECT * FROM video_applet_product_${butler_id} WHERE product_id = '${product_id}' AND main_id = ${main_id} LIMIT 1`
  225. );
  226. if(rows.length<=0){
  227. return null
  228. }
  229. return rows[0]
  230. }catch(e){
  231. return null
  232. }finally{
  233. if(connection!=null){
  234. await connection.end();
  235. }
  236. }
  237. }
  238. diffTime(last_d,new_d){
  239. const date1 = new Date(last_d);
  240. const date2 = new Date(new_d);
  241. if (date2.valueOf() > date1.valueOf()) {
  242. return new_d
  243. }
  244. return null
  245. }
  246. async setDzMfCookie(cookit){
  247. return this.redis_help.setKeyValue("DZ_MF_COOKIT",cookit)
  248. }
  249. async getDzMfCookit(){
  250. return this.redis_help.getKeyValue("DZ_MF_COOKIT")
  251. }
  252. async setDzCookie(cookit){
  253. return this.redis_help.setKeyValue("DZ_COOKIT",cookit)
  254. }
  255. async getDzCookit(){
  256. return this.redis_help.getKeyValue("DZ_COOKIT")
  257. }
  258. getNewBookName (bookName){
  259. return bookName.replace(",", ",").replace("。", "." ).replace(";", ";")
  260. }
  261. isMoreThanTwoDays(targetDate) {
  262. // 获取当前时间
  263. const now = new Date();
  264. // 确保传入的参数是有效的 Date 对象
  265. if (!(targetDate instanceof Date) || isNaN(targetDate.getTime())) {
  266. throw new Error('Invalid date provided');
  267. }
  268. // 计算时间差(毫秒)
  269. const timeDiff = now.getTime() - targetDate.getTime();
  270. // 将毫秒转换为天数
  271. const daysDiff = Math.abs(timeDiff / (1000 * 60 * 60 * 24));
  272. // 如果相差小于2天返回false,否则返回true
  273. return daysDiff >= 2;
  274. }
  275. }
  276. module.exports = new tools(require('./src/use_redis'));
  277. //123