getUrl.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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: kxs 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://test.config.kexiaoshuang.com/api/apiinfo/groupsforadmin",
  63. {
  64. key: "kxs#2024"
  65. }
  66. )
  67. .then(async res => {
  68. resolve(res.data.data);
  69. })
  70. .catch(err => {
  71. throw Error(err);
  72. });
  73. });
  74. };
  75. // 比对版本号且做更新
  76. const getGroupUrl = async (checkPlate = []) => {
  77. return new Promise(async (resolve, reject) => {
  78. const URLLIST = (await getURL()) || {};
  79. const parameters = { userId: 1, groups: [] };
  80. if (checkPlate.length == 0) {
  81. checkPlate = await getAllPlate();
  82. }
  83. parameters.groups = checkPlate.map(item => {
  84. return {
  85. name: item,
  86. version: URLLIST && URLLIST[item] ? URLLIST[item].groupVersion : 0
  87. };
  88. });
  89. const timer = setTimeout(() => {
  90. resolve(URLLIST);
  91. throw Error(
  92. "GET URLLIST FAIL, BECAUSE INTERFACE IS TIMEOUT, NOW RETURN LOCAL URLLIST"
  93. );
  94. }, 5000);
  95. postRequest(
  96. "http://test.config.kexiaoshuang.com/api/apiinfo/listforadmin",
  97. parameters
  98. )
  99. .then(async res => {
  100. clearTimeout(timer);
  101. if (Object.keys(URLLIST).length === 0) {
  102. putURL(res.data.data);
  103. resolve(res.data.data);
  104. } else {
  105. if (res.data.status == "1") {
  106. // 全部接口循环读取更新
  107. const onlineurl = res.data.data;
  108. for (const key in onlineurl) {
  109. if (
  110. URLLIST[key] &&
  111. onlineurl[key].groupVersion > URLLIST[key].groupVersion
  112. ) {
  113. URLLIST[key] = onlineurl[key];
  114. console.log("更新列表:", key);
  115. putURL(URLLIST);
  116. }
  117. }
  118. resolve(URLLIST);
  119. } else {
  120. resolve(URLLIST);
  121. throw Error(
  122. "GET URLLIST FAIL, BECAUSE INTERFACE STATUS IS ERROR, NOW RETURN LOCAL URLLIST"
  123. );
  124. }
  125. }
  126. })
  127. .catch(err => {
  128. resolve(URLLIST);
  129. throw Error(err);
  130. });
  131. });
  132. };
  133. export { getGroupUrl };