MAIN_SERVE_FACTORY.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. const CMD = {}
  2. const redis_help = require('../src/use_redis');
  3. const rabbitMq = require('../src/mq/rabbit-mq');
  4. const config = require('../etc/config.json')
  5. const axios = require('axios');
  6. const mysql = require('mysql2/promise');
  7. const http = require('http');
  8. const tools = require('../tools');
  9. const dbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql
  10. async function processTask(tg_platform_id, main_info) {
  11. try {
  12. console.log(`处理第 ${cur_page} 页任务`);
  13. const response = await CMD.syncMain(tg_platform_id, main_info);
  14. if (response !== null) {
  15. cur_page++;
  16. total = response.total;
  17. task_que.push(response);
  18. // 使用 Promise 延迟执行下一页
  19. await new Promise(resolve => setTimeout(resolve, 1000));
  20. return processTask(tg_platform_id, main_info);
  21. } else {
  22. console.log("完成所有创建:", main_info, cur_page);
  23. cur_page = -1;
  24. isStartTask = false;
  25. await redis_help.setKeyValue("isPauseTask", "false");
  26. }
  27. } catch (e) {
  28. console.error("processTask 错误:", e);
  29. isStartTask = false;
  30. await redis_help.setKeyValue("isPauseTask", "false");
  31. // if (cur_page !== -1) {
  32. // console.log("5秒后重试当前页...");
  33. // await new Promise(resolve => setTimeout(resolve, 5000));
  34. // return processTask(tg_platform_id, main_info);
  35. // }
  36. }
  37. }
  38. async function processSyncMainTask(){
  39. try{
  40. if(task_que.length>0){
  41. let sync_main_task_item = task_que.shift()
  42. let product_list = sync_main_task_item.data;
  43. let main_info = sync_main_task_item.main_info;
  44. let main_id = main_info.id
  45. const connection = await mysql.createConnection({
  46. ...dbConfig,
  47. multipleStatements: true
  48. });
  49. const values = product_list.map(item => [
  50. item.product_name,
  51. item.product_id,
  52. item.book_platform,
  53. main_id,
  54. main_info.app_id,
  55. 0,
  56. 0
  57. ]);
  58. // let table_name = "video_applet_product"
  59. // const insertSQL = `
  60. // INSERT INTO ${table_name}
  61. // (product_name, product_id, book_platform, main_id, dy_small_applet_app_id,status,
  62. // wait_status)
  63. // VALUES ?
  64. // `;
  65. const insertSQL = `
  66. INSERT INTO video_applet_product
  67. (product_name, product_id, book_platform, main_id, dy_small_applet_app_id, status, wait_status)
  68. VALUES ?
  69. ON DUPLICATE KEY UPDATE
  70. product_name = VALUES(product_name),
  71. dy_small_applet_app_id = VALUES(dy_small_applet_app_id),
  72. status = VALUES(status),
  73. wait_status = VALUES(wait_status)
  74. `;
  75. await connection.query(insertSQL, [values]);
  76. await connection.end();
  77. }
  78. }catch(e){
  79. console.error("processSyncMainTask:",e)
  80. }finally{
  81. setTimeout(processSyncMainTask, 1000);
  82. }
  83. }
  84. const messageHandler = async (msg) => {
  85. // CMD.start_task(msg['data']['data'])
  86. if(isStartTask){
  87. return
  88. }
  89. cur_page = 1;
  90. total = 0;
  91. isStartTask = true
  92. let main_info = msg['data']['data'].main_info
  93. console.log("main_info:",main_info)
  94. if(main_info.parent_platform_id!=0){
  95. tg_platform_id = main_info.parent_platform_id
  96. }else{
  97. tg_platform_id = main_info.tg_platform_id
  98. }
  99. await redis_help.setKeyValue("isPauseTask","true")
  100. await processTask(tg_platform_id,main_info)
  101. };
  102. // // 启动消费者
  103. // async function startConsumer() {
  104. // try {
  105. // await rabbitMq.consumerDirectMsg(messageHandler,"exchange_system","addMain");
  106. // } catch (error) {
  107. // console.error('启动消费者失败:', error);
  108. // }
  109. // }
  110. // 启动消费者
  111. async function startConsumer() {
  112. try {
  113. await rabbitMq.consumerDirectMsg(messageHandler,"exchange_system","syncMain");
  114. } catch (error) {
  115. console.error('启动消费者失败:', error);
  116. }
  117. processSyncMainTask()
  118. }
  119. CMD.init = async function(){
  120. redis_help.connect(()=>{
  121. })
  122. await startConsumer();
  123. }
  124. let task_que = []
  125. let cur_page = 1;
  126. let total = 0;
  127. let isStartTask = false
  128. CMD.syncMain = async function(tg_platform_id, main_info) {
  129. console.log(`====== 开始第 ${cur_page} 页请求 =====`);
  130. await new Promise(resolve => setTimeout(resolve, 500));
  131. try {
  132. let postData = {
  133. cmd: "video_product",
  134. fun: "search_book_data",
  135. data: {
  136. "product_name": "",
  137. "product_id": "",
  138. "tg_platform_id": tg_platform_id,
  139. "oce_material_id": "",
  140. "page_size": 500,
  141. "page_number": cur_page,
  142. "is_auto": "",
  143. "is_store": "",
  144. "genre": "",
  145. "alias_name": ""
  146. }
  147. };
  148. console.log("发送请求数据:", postData);
  149. let client = tools.getOneNewClinet()
  150. let response = await client.post('http://127.0.0.1:9100/tg/back/api', postData, {
  151. headers: {
  152. 'Content-Type': 'application/json',
  153. 'X-Request-Type': 'API', // 标记 API 请求
  154. }
  155. });
  156. response = response.data
  157. // 检查响应数据结构
  158. if (!response || !response.data || !Array.isArray(response.data)) {
  159. console.log("响应数据无效或格式错误");
  160. return null;
  161. }
  162. // 检查是否有数据
  163. if (response.data.length <= 0) {
  164. console.log("没有更多数据");
  165. return null;
  166. }
  167. // 返回正确的数据结构
  168. return {
  169. data: response.data,
  170. main_info: main_info,
  171. total: response.total || 0
  172. };
  173. } catch (e) {
  174. console.error("请求错误:", {
  175. message: e.message,
  176. code: e.code,
  177. stack: e.stack
  178. });
  179. return null;
  180. }
  181. };
  182. // CMD.start_task = async function(msgBody){
  183. // let PlatformConfig = JSON.parse(await redis_help.getKeyValue("PlatformConfig"))
  184. // let main_info = msgBody.main_info
  185. // let list = msgBody.list
  186. // let PlatformInfo = null
  187. // if(main_info.running_status==0){
  188. // console.log("创建了一个关闭的主体:",main_info)
  189. // return
  190. // }
  191. // console.log("main_info:",main_info)
  192. // console.log("list:",list)
  193. // for (let index = 0; index < PlatformConfig.length; index++) {
  194. // const element = PlatformConfig[index];
  195. // if(element.tg_platform_id == main_info.tg_platform_id){
  196. // PlatformInfo = element
  197. // break
  198. // }
  199. // }
  200. // if(PlatformInfo==null){
  201. // console.log("平台配置错误:",msgBody)
  202. // }else{
  203. // redis_help.setKeyValue("isPauseTask","true")
  204. // console.log("list::",list)
  205. // for (let index = 0; index < list.length; index++) {
  206. // const element = list[index];
  207. // let n_data = {book_id:element.product_id,
  208. // book_name:element.product_name,
  209. // tg_platform_id:element.book_platform,
  210. // app_id:element.dy_small_applet_app_id,
  211. // main_id:main_info.id
  212. // }
  213. // const result = await video_applet_product_controllers.createAppletProductData({
  214. // book_platform:n_data.tg_platform_id,
  215. // product_name:n_data.book_name,
  216. // product_id:n_data.book_id,
  217. // dy_small_applet_app_id:n_data.app_id,
  218. // status:0,
  219. // main_id:n_data.main_id,
  220. // promotion_id:'',
  221. // dy_small_program_start:'',
  222. // dy_small_program_start_data:'',
  223. // wait_status:0,
  224. // })
  225. // }
  226. // redis_help.setKeyValue("isPauseTask","false")
  227. // }
  228. // }
  229. CMD.init()