| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import Vue from "vue";
- import qs from "qs";
- export default function ({
- $axios,
- redirect,
- req,
- store,
- app,
- ...args
- }) {
- $axios.onRequest(config => {
- const contentType = config.headers["Content-Type"];
- const isUpload =
- contentType === "multipart/form-data" ||
- contentType === "application/json";
- if (!isUpload) {
- config.headers["Content-Type"] = "application/x-www-form-urlencoded";
- }
- console.log("nuxtAxios...........req.............req");
- config.baseURL = store.state.domainConfig.api;
- console.log("Before, making request to ", config.url, config.baseURL);
- // stringify post data
- if (config.method === "post" && !isUpload) {
- const data = config.data;
- console.log("stringify req data............");
- console.log(JSON.stringify(data));
- if (typeof data != "string") {
- let urlParams = [];
- for (const key in data) {
- if (data.hasOwnProperty(key)) {
- const element = encodeURIComponent(data[key]);
- urlParams.push(`${key}=${element}`);
- }
- }
- config.data = urlParams.join("&");
- }
- }
- console.log("stringify req env............",process.env.NODE_ENV);
- if(process.env.NODE_ENV=="development" && config.baseURL=="http://local.proginn.com")
- {
- config.baseURL="https://dev.test.proginn.com";
- config.headers.host="dev.test.proginn.com";
- }
- console.log("After, making request to ", config.url, config.baseURL);
- return config;
- });
- $axios.onResponse(res => {
- const data = res.data;
- let needLogin = false;
- let notShowError = false;
- if (res.config && res.config.data && typeof res.config.data === "string") {
- const queryData = res.config.data;
- // 将请求入参重新解析
- const arrQuery = queryData.split("&");
- // 遍历每个入参,看有没有needLogin参数,并且参数是true, 表示该接口如果服务器端返回未登录,则强制跳到登录页面
- for (let i = 0; i < arrQuery.length; i++) {
- const arr = arrQuery[i].split("=");
- //找到名称needLogin
- if ("needLogin" === arr[0] && (arr[1] === "true" || arr[1] === true)) {
- needLogin = true;
- break;
- }
- //找到名称 notShowError,如果为 true,则不做 $message.error() 展示
- if ("notShowError" === arr[0] && (arr[1] === "true" || arr[1] === true)) {
- notShowError = true;
- break;
- }
- }
- }
- if (data.status === 1 || data.filename || data.sign) {} else if (needLogin && data.status === -99) {
- if (app.$deviceType.app) {
- if (process.server) {
- redirect("proginn://login?backToPage=true");
- } else {
- location.href = "proginn://login?backToPage=true";
- }
- } else {
- if (process.server) {
- redirect(store.state.domainConfig.siteUrl + "/?loginbox=show");
- } else {
- location.href = store.state.domainConfig.siteUrl + "/?loginbox=show&next=" + encodeURIComponent(location.href);
- }
- }
- console.log(req && req.url);
- } else {
- console.log(JSON.stringify(data));
- if (!notShowError) {
- Vue.prototype.$message.closeAll();
- Vue.prototype.$message.error(data.info || data.error || "");
- }
- }
- return res;
- });
- $axios.onError(error => {
- console.log("err", error);
- });
- }
|