http.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. "use strict";
  2. import axios from "axios";
  3. import bus from "./bus.js";
  4. axios.defaults.withCredentials = true;
  5. axios.defaults.timeout = 0;
  6. axios.interceptors.request.use(
  7. (config) => {
  8. // 请求拦截
  9. let auth = localStorage.getItem("auth");
  10. if (auth) {
  11. config.headers.Authorization = auth;
  12. }
  13. const uuid = localStorage.getItem("uuid") || "";
  14. config.headers.Uuid = uuid;
  15. config.headers.AccessToken = uuid + "--zheshiyigename";
  16. return config;
  17. },
  18. (error) => {
  19. return Promise.reject(error);
  20. }
  21. );
  22. axios.interceptors.response.use(
  23. (response) => {
  24. //响应拦截
  25. return response;
  26. },
  27. (error) => {
  28. return Promise.reject(error);
  29. }
  30. );
  31. function postData(val, url) {
  32. //处理请求数据
  33. return val;
  34. }
  35. function getData(val, url) {
  36. //处理请求数据
  37. return val;
  38. }
  39. function checkStatus(response) {
  40. //处理响应数据
  41. if (response && response.status === 200) {
  42. // loading,如果http状态码正常,则直接返回数据
  43. let res = bus.$parseData(response.data);
  44. if (!res) {
  45. bus.$message.error("服务器开了个小差");
  46. return false;
  47. } else if (res.Ret == 403) {
  48. bus.$message.error(res.Msg);
  49. } else if (res.Ret === 408) {
  50. localStorage.setItem("auth", "")
  51. localStorage.setItem("loginTime", "")
  52. bus
  53. .$alert(res.Msg, "提示", {
  54. showClose: false,
  55. })
  56. .then(() => {
  57. window.location.href = window.location.origin + '/login';
  58. });
  59. }
  60. return res;
  61. } else {
  62. bus.$message.error("网络异常");
  63. return false;
  64. }
  65. }
  66. function checkCode(res) {
  67. bus.$message.error("网络异常");
  68. }
  69. // 请求时间需要过长,取消限制
  70. const cancelTimeoutUrlPost = ["/cloud_disk/resource/upload"];
  71. export default {
  72. post(url, data) {
  73. //post请求方式
  74. let timeout = cancelTimeoutUrlPost.includes(url) ? 0 : 100000;
  75. return axios({
  76. method: "post",
  77. url: url,
  78. baseURL: process.env.VUE_APP_API_ROOT,
  79. data: postData(data, url),
  80. timeout,
  81. headers: { "Content-Type": "application/json; charset=utf-8" },
  82. })
  83. .then((response) => {
  84. return checkStatus(response);
  85. })
  86. .catch((res) => {
  87. return checkCode(res);
  88. });
  89. },
  90. get(url, data, responseType = "json") {
  91. //get请求方式
  92. return axios({
  93. method: "get",
  94. baseURL: process.env.VUE_APP_API_ROOT,
  95. url: url,
  96. params: getData(data, url),
  97. timeout: 100000,
  98. headers: {
  99. "Content-type": "application/x-www-form-urlencoded; charset=utf-8",
  100. },
  101. responseType: responseType,
  102. transformRequest: [
  103. function (data) {
  104. let ret = "";
  105. for (let it in data) {
  106. ret +=
  107. encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
  108. }
  109. return ret;
  110. },
  111. ],
  112. })
  113. .then((response) => {
  114. return checkStatus(response);
  115. })
  116. .catch((res) => {
  117. return checkCode(res);
  118. });
  119. },
  120. Base64,
  121. dateFormatter,
  122. getTotalMonth,
  123. Decrypt,
  124. Encrypt,
  125. isMobileNo,
  126. };
  127. //解密方法
  128. function Decrypt(word) {
  129. let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  130. let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  131. let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  132. let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  133. return decryptedStr.toString();
  134. }
  135. //加密方法
  136. function Encrypt(word) {
  137. let srcs = CryptoJS.enc.Utf8.parse(word);
  138. let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  139. return encrypted.ciphertext.toString().toUpperCase();
  140. }
  141. function isMobileNo(account) {
  142. // 手机号正则
  143. var isChinaMobile = new RegExp(
  144. "(^1(3[4-9]|4[78]|5[0-27-9]|7[28]|8[2-478]|98)\\d{8}$)"
  145. ); // 中国移动
  146. // 手机段:134,135,136,137,138,139,147,148[卫星通信],150,151,152,157,158,159,172,178,182,183,184,187,188,198
  147. var isChinaUnicom = new RegExp(
  148. "(^1(3[0-2]|4[56]|5[56]|66|7[156]|8[56])\\d{8}$)"
  149. ); // 中国联通
  150. // 手机段:130,131,132,145,146[卫星通信],155,156,166,171,175,176,185,186
  151. var isChinaTelcom = new RegExp("(^1(33|49|53|7[347]|8[019]|99)\\d{8}$)"); // 中国电信
  152. // 手机段:133,149,153,173,174,177,180,181,189,199
  153. var isOtherTelphone = new RegExp("(^170\\d{8}$)");
  154. // 虚拟运营商170号段
  155. if (isChinaMobile.test(account)) {
  156. return true;
  157. } else if (isChinaUnicom.test(account)) {
  158. return true;
  159. } else if (isChinaTelcom.test(account)) {
  160. return true;
  161. } else return isOtherTelphone.test(account);
  162. }
  163. function Base64() {
  164. // private property
  165. var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  166. // public method for encoding
  167. this.encode = function (input) {
  168. var output = "";
  169. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  170. var i = 0;
  171. input = this._utf8_encode(input);
  172. while (i < input.length) {
  173. chr1 = input.charCodeAt(i++);
  174. chr2 = input.charCodeAt(i++);
  175. chr3 = input.charCodeAt(i++);
  176. enc1 = chr1 >> 2;
  177. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  178. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  179. enc4 = chr3 & 63;
  180. if (isNaN(chr2)) {
  181. enc3 = enc4 = 64;
  182. } else if (isNaN(chr3)) {
  183. enc4 = 64;
  184. }
  185. output = output +
  186. _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  187. _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  188. }
  189. return output;
  190. }
  191. // public method for decoding
  192. this.decode = function (input) {
  193. var output = "";
  194. var chr1, chr2, chr3;
  195. var enc1, enc2, enc3, enc4;
  196. var i = 0;
  197. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  198. while (i < input.length) {
  199. enc1 = _keyStr.indexOf(input.charAt(i++));
  200. enc2 = _keyStr.indexOf(input.charAt(i++));
  201. enc3 = _keyStr.indexOf(input.charAt(i++));
  202. enc4 = _keyStr.indexOf(input.charAt(i++));
  203. chr1 = (enc1 << 2) | (enc2 >> 4);
  204. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  205. chr3 = ((enc3 & 3) << 6) | enc4;
  206. output = output + String.fromCharCode(chr1);
  207. if (enc3 != 64) {
  208. output = output + String.fromCharCode(chr2);
  209. }
  210. if (enc4 != 64) {
  211. output = output + String.fromCharCode(chr3);
  212. }
  213. }
  214. output = this._utf8_decode(output);
  215. return output;
  216. }
  217. // private method for UTF-8 encoding
  218. this._utf8_encode = function (string) {
  219. string = string.replace(/\r\n/g, "\n");
  220. var utftext = "";
  221. for (var n = 0; n < string.length; n++) {
  222. var c = string.charCodeAt(n);
  223. if (c < 128) {
  224. utftext += String.fromCharCode(c);
  225. } else if ((c > 127) && (c < 2048)) {
  226. utftext += String.fromCharCode((c >> 6) | 192);
  227. utftext += String.fromCharCode((c & 63) | 128);
  228. } else {
  229. utftext += String.fromCharCode((c >> 12) | 224);
  230. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  231. utftext += String.fromCharCode((c & 63) | 128);
  232. }
  233. }
  234. return utftext;
  235. }
  236. // private method for UTF-8 decoding
  237. this._utf8_decode = function (utftext) {
  238. var string = "";
  239. var i = 0;
  240. var c = 0;
  241. var c1 = 0;
  242. var c2 = 0;
  243. while (i < utftext.length) {
  244. c = utftext.charCodeAt(i);
  245. if (c < 128) {
  246. string += String.fromCharCode(c);
  247. i++;
  248. } else if ((c > 191) && (c < 224)) {
  249. c2 = utftext.charCodeAt(i + 1);
  250. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  251. i += 2;
  252. } else {
  253. c2 = utftext.charCodeAt(i + 1);
  254. var c3 = utftext.charCodeAt(i + 2);
  255. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  256. i += 3;
  257. }
  258. }
  259. return string;
  260. }
  261. }
  262. function dateFormatter(str) {
  263. //默认返回yyyy-MM-dd HH-mm-ss
  264. var hasTime = arguments[1] != false ? true : false; //可传第二个参数false,返回yyyy-MM-dd
  265. var hasType = arguments[2]; //可传第三个参数,tru 返回yyyy年MM月dd日
  266. var dateStr = String(str);
  267. if (!(dateStr.indexOf("-") > -1 && dateStr.indexOf("T") > -1)) {
  268. dateStr = dateStr.replace(/\-/g, "/");
  269. }
  270. var d = new Date(dateStr);
  271. var year = d.getFullYear();
  272. var month =
  273. d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
  274. var day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
  275. var hour = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
  276. var minute = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
  277. var second = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
  278. if (hasTime) {
  279. if (hasType) {
  280. return `${month}月${day}日 ${hour}时${minute}分${second}秒`;
  281. }
  282. return (
  283. [year, month, day].join(".") + " " + [hour, minute, second].join(":")
  284. );
  285. } else {
  286. if (hasType) {
  287. return `${month}月${day}日`;
  288. }
  289. return [year, month, day].join(".");
  290. }
  291. }
  292. function getTotalMonth(date, hasTime = false) {
  293. //默认返回 [yyyy-MM-01,yyyy-MM-dd] 整月时间
  294. let newDate = new Date(date);
  295. let year = newDate.getFullYear();
  296. let month =
  297. newDate.getMonth() + 1 < 10
  298. ? "0" + (newDate.getMonth() + 1)
  299. : newDate.getMonth() + 1;
  300. var hour =
  301. newDate.getHours() < 10 ? "0" + newDate.getHours() : newDate.getHours();
  302. var minute =
  303. newDate.getMinutes() < 10
  304. ? "0" + newDate.getMinutes()
  305. : newDate.getMinutes();
  306. var second =
  307. newDate.getSeconds() < 10
  308. ? "0" + newDate.getSeconds()
  309. : newDate.getSeconds();
  310. let timestamp =
  311. new Date(`${year}/${newDate.getMonth() + 2}/01`).getTime() -
  312. 24 * 60 * 60 * 1000;
  313. if (hasTime) {
  314. return [
  315. `${year}-${month}-01 ${hour}:${minute}:${second}`,
  316. dateFormatter(timestamp, true),
  317. ];
  318. } else {
  319. return [`${year}-${month}-01`, dateFormatter(timestamp, false)];
  320. }
  321. }
  322. export async function downloadFileByUrl(dataUrl) {
  323. return (await fetch(dataUrl)).blob();
  324. }