MAIN_SERVE_FACTORY.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const CMD = {}
  2. const PROT = 9111
  3. var http = require('http');
  4. const redis_help = require('../src/use_redis');
  5. const video_applet_product_controllers = require('../src/data_manager/Controllers/video_applet_product_controllers');
  6. CMD.init = function(){
  7. redis_help.connect(()=>{
  8. })
  9. }
  10. CMD.start_task = async function(msgBody){
  11. let PlatformConfig = JSON.parse(await redis_help.getKeyValue("PlatformConfig"))
  12. let main_info = msgBody.main_info
  13. let list = msgBody.list
  14. let PlatformInfo = null
  15. if(main_info.running_status==0){
  16. console.log("创建了一个关闭的主体:",main_info)
  17. return
  18. }
  19. console.log("main_info:",main_info)
  20. console.log("list:",list)
  21. for (let index = 0; index < PlatformConfig.length; index++) {
  22. const element = PlatformConfig[index];
  23. if(element.tg_platform_id == main_info.tg_platform_id){
  24. PlatformInfo = element
  25. break
  26. }
  27. }
  28. if(PlatformInfo==null){
  29. console.log("平台配置错误:",msgBody)
  30. }else{
  31. redis_help.setKeyValue("isPauseTask","true")
  32. console.log("list::",list)
  33. for (let index = 0; index < list.length; index++) {
  34. const element = list[index];
  35. let n_data = {book_id:element.product_id,
  36. book_name:element.product_name,
  37. tg_platform_id:element.book_platform,
  38. app_id:element.dy_small_applet_app_id,
  39. main_id:main_info.id
  40. }
  41. const result = await video_applet_product_controllers.createAppletProductData({
  42. book_platform:n_data.tg_platform_id,
  43. product_name:n_data.book_name,
  44. product_id:n_data.book_id,
  45. dy_small_applet_app_id:n_data.app_id,
  46. status:0,
  47. main_id:n_data.main_id,
  48. promotion_id:'',
  49. dy_small_program_start:'',
  50. dy_small_program_start_data:'',
  51. wait_status:0,
  52. })
  53. }
  54. redis_help.setKeyValue("isPauseTask","false")
  55. }
  56. }
  57. var server = http.createServer(function(req,res){
  58. res.setHeader('Access-Control-Allow-Origin', '*'); // 允许所有域的请求,注意:在生产环境中应该限制为特定的域
  59. res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); // 允许的方法
  60. res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // 允许的头部
  61. let body = '';
  62. // 监听数据块
  63. req.on('data', chunk => {
  64. body += chunk.toString(); // 将接收到的数据块转换为字符串并拼接
  65. });
  66. // 监听请求结束
  67. req.on('end', async () => {
  68. if (req.method === 'POST') {
  69. if (req.headers['content-type'] === 'application/json') {
  70. try {
  71. // 解析 JSON 数据
  72. const jsonData = JSON.parse(body);
  73. console.log("recv msg:",jsonData)
  74. if(jsonData['fun']=="addMain"&&jsonData['cmd']=="filter_task"){
  75. CMD.start_task(jsonData['data']['data'])
  76. res.writeHead(200, {'Content-Type': 'application/json'});
  77. res.end(JSON.stringify({ msg: 'success!',code:10000,data:{}}));
  78. }else{
  79. res.writeHead(200, {'Content-Type': 'application/json'});
  80. res.end(JSON.stringify({ message: 'fun 错误',code:100}));
  81. }
  82. } catch (error) {
  83. // 处理解析错误
  84. console.error('Error parsing JSON:', error);
  85. res.writeHead(200, {'Content-Type': 'application/json'});
  86. res.end(JSON.stringify({ error: 'Invalid JSON' }));
  87. }
  88. } else {
  89. // 如果不是 JSON 内容类型,返回错误
  90. res.writeHead(200, {'Content-Type': 'text/plain'});
  91. res.end('Unsupported Media Type. Please send data as JSON.');
  92. }
  93. }else{
  94. res.writeHead(200, {'Content-Type': 'text/plain'});
  95. res.end(' Please Use Post.');
  96. }
  97. });
  98. })
  99. server.listen(PROT,()=>{
  100. });
  101. CMD.init()