| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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("Before, making request to ", config.url, config.baseURL);
- 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("After, making request to ", config.url, config.baseURL);
- return config;
- });
- $axios.onResponse(res => {
- const data = res.data;
- console.log("res:::::::::");
- console.log(app.$deviceType);
- // console.log(res);
- console.log(res.config.url);
- console.log(JSON.stringify(data));
- // req && req.url && needLogin(req.url)
- let needLogin = 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;
- }
- }
- }
- 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";
- }
- }
- console.log(req && req.url);
- } else {
- console.log(JSON.stringify(data));
- Vue.prototype.$message.closeAll();
- Vue.prototype.$message.error(data.info || "");
- }
- return res;
- });
- $axios.onError(error => {
- console.log("err", error);
- });
- }
|