"use strict"; import axios from "axios"; import bus from "./bus.js"; axios.defaults.withCredentials = true; axios.defaults.timeout = 0; axios.interceptors.request.use( (config) => { // 请求拦截 let auth = localStorage.getItem("auth"); if (auth) { config.headers.Authorization = auth; } const uuid = localStorage.getItem("uuid") || ""; config.headers.Uuid = uuid; config.headers.AccessToken = uuid + "--zheshiyigename"; return config; }, (error) => { return Promise.reject(error); } ); axios.interceptors.response.use( (response) => { //响应拦截 return response; }, (error) => { return Promise.reject(error); } ); function postData(val, url) { //处理请求数据 return val; } function getData(val, url) { //处理请求数据 return val; } function checkStatus(response) { //处理响应数据 if (response && response.status === 200) { // loading,如果http状态码正常,则直接返回数据 let res = bus.$parseData(response.data); if (!res) { bus.$message.error("服务器开了个小差"); return false; } else if (res.Ret == 403) { bus.$message.error(res.Msg); } else if (res.Ret === 408) { localStorage.setItem("auth", "") localStorage.setItem("loginTime", "") bus .$alert(res.Msg, "提示", { showClose: false, }) .then(() => { window.location.href = window.location.origin + '/login'; }); } return res; } else { bus.$message.error("网络异常"); return false; } } function checkCode(res) { bus.$message.error("网络异常"); } // 请求时间需要过长,取消限制 const cancelTimeoutUrlPost = ["/cloud_disk/resource/upload"]; export default { post(url, data) { //post请求方式 let timeout = cancelTimeoutUrlPost.includes(url) ? 0 : 100000; return axios({ method: "post", url: url, baseURL: process.env.VUE_APP_API_ROOT, data: postData(data, url), timeout, headers: { "Content-Type": "application/json; charset=utf-8" }, }) .then((response) => { return checkStatus(response); }) .catch((res) => { return checkCode(res); }); }, get(url, data, responseType = "json") { //get请求方式 return axios({ method: "get", baseURL: process.env.VUE_APP_API_ROOT, url: url, params: getData(data, url), timeout: 100000, headers: { "Content-type": "application/x-www-form-urlencoded; charset=utf-8", }, responseType: responseType, transformRequest: [ function (data) { let ret = ""; for (let it in data) { ret += encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&"; } return ret; }, ], }) .then((response) => { return checkStatus(response); }) .catch((res) => { return checkCode(res); }); }, Base64, dateFormatter, getTotalMonth, Decrypt, Encrypt, isMobileNo, }; //解密方法 function Decrypt(word) { let encryptedHexStr = CryptoJS.enc.Hex.parse(word); let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); } //加密方法 function Encrypt(word) { let srcs = CryptoJS.enc.Utf8.parse(word); let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString().toUpperCase(); } function isMobileNo(account) { // 手机号正则 var isChinaMobile = new RegExp( "(^1(3[4-9]|4[78]|5[0-27-9]|7[28]|8[2-478]|98)\\d{8}$)" ); // 中国移动 // 手机段:134,135,136,137,138,139,147,148[卫星通信],150,151,152,157,158,159,172,178,182,183,184,187,188,198 var isChinaUnicom = new RegExp( "(^1(3[0-2]|4[56]|5[56]|66|7[156]|8[56])\\d{8}$)" ); // 中国联通 // 手机段:130,131,132,145,146[卫星通信],155,156,166,171,175,176,185,186 var isChinaTelcom = new RegExp("(^1(33|49|53|7[347]|8[019]|99)\\d{8}$)"); // 中国电信 // 手机段:133,149,153,173,174,177,180,181,189,199 var isOtherTelphone = new RegExp("(^170\\d{8}$)"); // 虚拟运营商170号段 if (isChinaMobile.test(account)) { return true; } else if (isChinaUnicom.test(account)) { return true; } else if (isChinaTelcom.test(account)) { return true; } else return isOtherTelphone.test(account); } function Base64() { // private property var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; // public method for encoding this.encode = function (input) { var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; input = this._utf8_encode(input); while (i < input.length) { chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); chr3 = input.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4); } return output; } // public method for decoding this.decode = function (input) { var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); while (i < input.length) { enc1 = _keyStr.indexOf(input.charAt(i++)); enc2 = _keyStr.indexOf(input.charAt(i++)); enc3 = _keyStr.indexOf(input.charAt(i++)); enc4 = _keyStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) { output = output + String.fromCharCode(chr2); } if (enc4 != 64) { output = output + String.fromCharCode(chr3); } } output = this._utf8_decode(output); return output; } // private method for UTF-8 encoding this._utf8_encode = function (string) { string = string.replace(/\r\n/g, "\n"); var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if ((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; } // private method for UTF-8 decoding this._utf8_decode = function (utftext) { var string = ""; var i = 0; var c = 0; var c1 = 0; var c2 = 0; while (i < utftext.length) { c = utftext.charCodeAt(i); if (c < 128) { string += String.fromCharCode(c); i++; } else if ((c > 191) && (c < 224)) { c2 = utftext.charCodeAt(i + 1); string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); i += 2; } else { c2 = utftext.charCodeAt(i + 1); var c3 = utftext.charCodeAt(i + 2); string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; } } function dateFormatter(str) { //默认返回yyyy-MM-dd HH-mm-ss var hasTime = arguments[1] != false ? true : false; //可传第二个参数false,返回yyyy-MM-dd var hasType = arguments[2]; //可传第三个参数,tru 返回yyyy年MM月dd日 var dateStr = String(str); if (!(dateStr.indexOf("-") > -1 && dateStr.indexOf("T") > -1)) { dateStr = dateStr.replace(/\-/g, "/"); } var d = new Date(dateStr); var year = d.getFullYear(); var month = d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1; var day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate(); var hour = d.getHours() < 10 ? "0" + d.getHours() : d.getHours(); var minute = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes(); var second = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds(); if (hasTime) { if (hasType) { return `${month}月${day}日 ${hour}时${minute}分${second}秒`; } return ( [year, month, day].join(".") + " " + [hour, minute, second].join(":") ); } else { if (hasType) { return `${month}月${day}日`; } return [year, month, day].join("."); } } function getTotalMonth(date, hasTime = false) { //默认返回 [yyyy-MM-01,yyyy-MM-dd] 整月时间 let newDate = new Date(date); let year = newDate.getFullYear(); let month = newDate.getMonth() + 1 < 10 ? "0" + (newDate.getMonth() + 1) : newDate.getMonth() + 1; var hour = newDate.getHours() < 10 ? "0" + newDate.getHours() : newDate.getHours(); var minute = newDate.getMinutes() < 10 ? "0" + newDate.getMinutes() : newDate.getMinutes(); var second = newDate.getSeconds() < 10 ? "0" + newDate.getSeconds() : newDate.getSeconds(); let timestamp = new Date(`${year}/${newDate.getMonth() + 2}/01`).getTime() - 24 * 60 * 60 * 1000; if (hasTime) { return [ `${year}-${month}-01 ${hour}:${minute}:${second}`, dateFormatter(timestamp, true), ]; } else { return [`${year}-${month}-01`, dateFormatter(timestamp, false)]; } } export async function downloadFileByUrl(dataUrl) { return (await fetch(dataUrl)).blob(); }