MESSAGE_DISPATCH.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. const CMD = {}
  2. const time_count = 60000*5 //60秒更新一次配置
  3. const config = require('../etc/config.json')
  4. const redis_help = require('../src/use_redis');
  5. var axios = require('axios');
  6. const HttpClient = require('../src/HttpClient');
  7. const WebSocketClient = require('../src/WebSocketClient');
  8. const rabbitMq = require('../src/mq/rabbit-mq');
  9. console.log("运行 MESSAGE_DISPATCH")
  10. CMD.updateFilterConfig = async function(){
  11. const postData = {
  12. cmd:"db_filter_config",
  13. fun:"getConfig",
  14. data:{}
  15. };
  16. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  17. .then(response => {
  18. redis_help.setKeyValue("FilterConfig",JSON.stringify(response.data.data))
  19. })
  20. .catch(error => {
  21. console.error('updateFilterConfig error!', error);
  22. });
  23. }
  24. CMD.updateMainConfig = async function(){
  25. const postData = {
  26. cmd:"tg_main",
  27. fun:"getOpenMainList",
  28. data:{}
  29. };
  30. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  31. .then(response => {
  32. redis_help.setKeyValue("MainConfig",JSON.stringify(response.data.data))
  33. })
  34. .catch(error => {
  35. console.error('updateFilterConfig error!', error);
  36. });
  37. }
  38. CMD.updateAppConfig = async function(){
  39. const postData = {
  40. cmd:"tg_app",
  41. fun:"getAppList",
  42. data:{}
  43. };
  44. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  45. .then(response => {
  46. redis_help.setKeyValue("AppConfig",JSON.stringify(response.data.data))
  47. })
  48. .catch(error => {
  49. console.error('updateFilterConfig error!', error);
  50. });
  51. }
  52. CMD.updatePlatformConfig = async function(){
  53. const postData = {
  54. cmd:"tg_platform",
  55. fun:"getPlatformList",
  56. data:{}
  57. };
  58. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  59. .then(response => {
  60. redis_help.setKeyValue("PlatformConfig",JSON.stringify(response.data.data))
  61. })
  62. .catch(error => {
  63. console.error('updateFilterConfig error!', error);
  64. });
  65. }
  66. CMD.updateFqKeyList = async function(){
  67. const postData = {
  68. cmd:"fq_book",
  69. fun:"get_all_fq_key",
  70. data:{}
  71. };
  72. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  73. .then(response => {
  74. redis_help.setKeyValue("all_fq_key",JSON.stringify(response.data.data))
  75. })
  76. .catch(error => {
  77. console.error('all_fq_key error!', error);
  78. });
  79. }
  80. CMD.updateBlackBooks = async function(){
  81. const postData = {
  82. cmd:"book_black_list",
  83. fun:"get_all_black_books",
  84. data:{}
  85. };
  86. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  87. .then(response => {
  88. redis_help.setKeyValue("all_black_books",JSON.stringify(response.data.data))
  89. })
  90. .catch(error => {
  91. console.error('all_black_books error!', error);
  92. });
  93. }
  94. CMD.getPullDataConfig = async function() {
  95. const postData = {
  96. cmd:"pull_data_config",
  97. fun:"get_all_pull_data_list",
  98. data:{}
  99. };
  100. axios.post('http://127.0.0.1:9100/tg/back/api', postData)
  101. .then(response => {
  102. redis_help.setKeyValue("PullDataConfig",JSON.stringify(response.data.data))
  103. })
  104. .catch(error => {
  105. console.error('getPullDataConfig error!', error);
  106. });
  107. }
  108. var ws_isOpen = false
  109. CMD.myListener = {
  110. onOpen: function (webSocket, response) {
  111. ws_isOpen = true
  112. //打开链接后,想服务器端发送一条消息
  113. sendMessage("status_task",{work:1})
  114. },
  115. onMessage: function (webSocket, msg) { //msg可能是字符串,也可能是byte数组,取决于服务器送的内容
  116. recvMessage(msg)
  117. },
  118. onClosing: function (webSocket, code, response) {
  119. ws_isOpen = false
  120. },
  121. onClosed: function (webSocket, code, response) {
  122. ws_isOpen = false
  123. },
  124. onFailure: function (webSocket, t, response) {
  125. }
  126. }
  127. function sendMessage(cmd,data){
  128. if(ws_isOpen==true){
  129. var json = {};
  130. json.cmd=cmd; //方法
  131. json.data= data; //参数
  132. var msg=JSON.stringify(json);
  133. if(CMD.wsClient!=undefined&&CMD.wsClient!=null){
  134. CMD.wsClient.sendMsg(msg);
  135. }
  136. }
  137. }
  138. async function sendMQMessage(message,routingKey = null) {
  139. try {
  140. if(routingKey!=null){
  141. await rabbitMq.producerDirectMsg( message,"exchange_system",routingKey);
  142. }else{
  143. await rabbitMq.producerDirectMsg( message,"exchange_system");
  144. }
  145. console.log('消息发送成功');
  146. } catch (error) {
  147. console.error('发送消息失败:', error);
  148. }
  149. }
  150. async function sendPullMQMessage(message,routingKey = null) {
  151. try {
  152. if(routingKey!=null){
  153. await rabbitMq.producerDirectMsg( message,"exchange_update_pull_config",routingKey);
  154. }else{
  155. await rabbitMq.producerDirectMsg( message,"exchange_update_pull_config");
  156. }
  157. console.log('消息发送成功');
  158. } catch (error) {
  159. console.error('发送消息失败:', error);
  160. }
  161. }
  162. async function recvMessage(data){
  163. try {
  164. // 单个任务
  165. let json_msg = JSON.parse(data)
  166. console.log("recvMessage:",json_msg.cmd)
  167. switch (json_msg.cmd) {
  168. case "updateFilterConfig":
  169. CMD.updateFilterConfig()
  170. break;
  171. case "updateMainConfig":
  172. CMD.updateMainConfig()
  173. break;
  174. case "updateAppConfig":
  175. CMD.updateAppConfig()
  176. break;
  177. case "updatePlatformConfig":
  178. CMD.updatePlatformConfig()
  179. break;
  180. case "updateFqKeyList":
  181. CMD.updateFqKeyList()
  182. break;
  183. case "updateBlackBooks":
  184. CMD.updateBlackBooks()
  185. break;
  186. case "addMain":
  187. {
  188. const postData = {
  189. cmd:"filter_task",
  190. fun:json_msg.cmd,
  191. data:json_msg
  192. };
  193. sendMQMessage(postData,json_msg.cmd)
  194. }
  195. break;
  196. case "getPullDataConfig": //获取拉取数据类配置
  197. {
  198. CMD.getPullDataConfig()
  199. }
  200. break;
  201. case "updatePullConig": //同步拉取配置
  202. {
  203. await CMD.getPullDataConfig()
  204. const postData = {
  205. cmd:"filter_task",
  206. fun:json_msg.cmd,
  207. data:json_msg
  208. };
  209. sendPullMQMessage(postData,json_msg.cmd)
  210. }
  211. break;
  212. default:
  213. // const client = new HttpClient({
  214. // timeout: 5000,
  215. // // headers: {
  216. // // 'User-Agent': 'Custom User Agent'
  217. // // }
  218. // });
  219. // const response = await client.post('http://127.0.0.1:9111',postData);
  220. break;
  221. }
  222. }catch(e){
  223. console.error("recvMessage error1!:",e)
  224. }
  225. }
  226. CMD.init = function(){
  227. redis_help.connect((results)=>{
  228. if(results){
  229. CMD.wsClient = new WebSocketClient(config.ws_config.host, {
  230. reconnectDelay: 5000, // 5 seconds
  231. heartbeatInterval: 30000, // 30 seconds
  232. heartbeatTimeout: 5000 // 5 seconds
  233. },CMD.myListener);
  234. }
  235. })
  236. CMD.updateConfig()
  237. CMD.updateTimeConfig()
  238. }
  239. CMD.updateTimeConfig = function(){
  240. let create_heiyan_book_link = require('../src/api/hy/create_heiyan_book_link')
  241. create_heiyan_book_link.login((data)=>{
  242. console.log("hei_yan_token:",data['token'])
  243. redis_help.setKeyValue("hei_yan_token",data['token'])
  244. })
  245. let qm_login = require('../src/api/qm/qm_login')
  246. qm_login.login((data,headers)=>{
  247. console.log("qi_mao_token:",headers['n-token'])
  248. redis_help.setKeyValue("qi_mao_token",headers['n-token'])
  249. })
  250. }
  251. //定时更新配置
  252. CMD.updateConfig = function(){
  253. setInterval(()=>{
  254. CMD.updateTimeConfig()
  255. },time_count)
  256. }
  257. CMD.init()