getUrl.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * @Author:
  3. * @Date: 2024-01-04 15:04:20
  4. * @LastEditors: Please set LastEditors
  5. * @LastEditTime: 2024-03-13 17:30:17
  6. * @Description: tel files
  7. * @filePath:
  8. */
  9. import axios from "axios";
  10. import PublicLib from "./PublicLib";
  11. import CryptoJS from "crypto-js";
  12. const request = axios.create({
  13. // 请求超时时间
  14. timeout: 5000
  15. });
  16. // 加密函数
  17. const encryptByDES = function (message) {
  18. const keyHex = CryptoJS.enc.Utf8.parse("&L^kg4N9");
  19. const ivHex = CryptoJS.enc.Utf8.parse("&L^kg4N9");
  20. const encrypted = CryptoJS.DES.encrypt(message, keyHex, {
  21. iv: ivHex,
  22. mode: CryptoJS.mode.CBC,
  23. padding: CryptoJS.pad.Pkcs7
  24. });
  25. return encrypted.toString();
  26. };
  27. // 请求方法
  28. const postRequest = (url, params) => {
  29. console.log("请求参数:", params);
  30. const param = new URLSearchParams();
  31. param.append("value", encryptByDES(JSON.stringify(params)));
  32. return request({
  33. url,
  34. method: "post",
  35. headers: {
  36. "Content-Type": "application/x-www-form-urlencoded"
  37. },
  38. data: param
  39. });
  40. };
  41. // 更新本地URL列表
  42. const putURL = list => {
  43. PublicLib.putCookieInfo({
  44. Key: "URL_INTERFACE",
  45. Value: list,
  46. Type: 3,
  47. Conf: { dbname: "PROJECT_INTERFACE", version: 1, table: "INTERFACE_List" }
  48. });
  49. };
  50. // 获取本地URL列表
  51. const getURL = () => {
  52. return PublicLib.getCookieInfo({
  53. Key: "URL_INTERFACE",
  54. Type: 3,
  55. Conf: { dbname: "PROJECT_INTERFACE", version: 1, table: "INTERFACE_List" }
  56. });
  57. };
  58. // URL列表资源
  59. const getAllPlate = () => {
  60. return new Promise(async (resolve, reject) => {
  61. postRequest(
  62. "http://localhost:6101/noauth/api/groupsforadmin",
  63. // "https://logic-executor-api.kexiaoshuang.com/noauth/api/groupsforadmin",
  64. {
  65. key: "tel#2024"
  66. }
  67. )
  68. .then(async res => {
  69. console.log(res);
  70. resolve(res.data.data);
  71. })
  72. .catch(err => {
  73. throw Error(err);
  74. });
  75. });
  76. };
  77. // 比对版本号且做更新
  78. const getGroupUrl = async (checkPlate = []) => {
  79. return new Promise(async (resolve, reject) => {
  80. const URLLIST = (await getURL()) || {};
  81. const parameters = { userId: 1, groups: [] };
  82. if (checkPlate.length == 0) {
  83. checkPlate = await getAllPlate();
  84. }
  85. console.log('123123', checkPlate);
  86. parameters.groups = checkPlate ? checkPlate.map(item => {
  87. return {
  88. name: item,
  89. version: URLLIST && URLLIST[item] ? URLLIST[item].groupVersion : 0
  90. };
  91. }) : [];
  92. const timer = setTimeout(() => {
  93. resolve(URLLIST);
  94. throw Error(
  95. "GET URLLIST FAIL, BECAUSE INTERFACE IS TIMEOUT, NOW RETURN LOCAL URLLIST"
  96. );
  97. }, 5000);
  98. postRequest(
  99. "http://localhost:6101/noauth/api/listforadmin",
  100. // "https://logic-executor-api.kexiaoshuang.com/noauth/api/listforadmin",
  101. parameters
  102. )
  103. .then(async res => {
  104. clearTimeout(timer);
  105. if (Object.keys(URLLIST).length === 0) {
  106. putURL(res.data.data);
  107. resolve(res.data.data);
  108. } else {
  109. if (res.data.status == "1") {
  110. // 全部接口循环读取更新
  111. const onlineurl = res.data.data;
  112. for (const key in onlineurl) {
  113. if (
  114. URLLIST[key] &&
  115. onlineurl[key].groupVersion > URLLIST[key].groupVersion
  116. ) {
  117. URLLIST[key] = onlineurl[key];
  118. console.log("更新列表:", key);
  119. putURL(URLLIST);
  120. }
  121. }
  122. resolve(URLLIST);
  123. } else {
  124. resolve(URLLIST);
  125. throw Error(
  126. "GET URLLIST FAIL, BECAUSE INTERFACE STATUS IS ERROR, NOW RETURN LOCAL URLLIST"
  127. );
  128. }
  129. }
  130. })
  131. .catch(err => {
  132. resolve(URLLIST);
  133. throw Error(err);
  134. });
  135. });
  136. };
  137. export { getGroupUrl };