vue+axios请求头封装

import {
  mapMutations
} from 'vuex'
import axios from 'axios'
import {
  Toast
} from 'mint-ui';
import i18n from 'assets/js/vi18n/i18n.js'
export const mixins = {
  data() {
    return {
      istimeout: true
    }
  },
  methods: {
    ajaxSend(urlSuffix, paramsData) {
      // 公共请求数据的方法。
      let vm = this;
      let storage = window.localStorage;
      let langCode='zh-CN';
      let langChange=storage.langCode
      if(langChange){
        langCode=langChange;
      }
      let zksysReqParams = {
        "lang": langCode,
        "tz": "+8:00",
        "agent": "zkteco",
        "platform": "app",
        "sys": "zks-platform",
        "sessionId": storage.sessionId,
        "intfVer": "1.0.0",
        "payload": {
          "datafmt": 1,
          "params": {}
        }
      };
      var prefixUrl = "http://192.168.12.52:8181";
      // var prefixUrl = "";
      // var prefixUrl=window.localStorage.prefixUrl;
      storage.prefixUrl=prefixUrl;

      var reqParams = Object.assign({}, zksysReqParams);
      reqParams.payload.params = paramsData;
      let url = prefixUrl + urlSuffix;
      return axios.post(url, reqParams).then((res) => {
        if (res.data.code !== '00000000') {
          if (res.data.code == 'ET000001') {
            // Toast('登录过期,请重新登录');
            vm._chang(false); //过期的话,把show设为false。在login时进行判断。
            vm.$router.push({path:'/login'});
            return false;
           }else{
            Toast(vm.$t(res.data.code));
           }
         }else{
            storage.sessionId=res.data.sessionId;
         }
         return Promise.resolve(res.data);
      }).catch((err) => {
        //  Toast(err);
        console.log(err);
      })
    },
    getCookie: function (name) {
      var cookieName = encodeURIComponent(name) + "=";
      var cookieStart = document.cookie.indexOf(cookieName);
      var cookieValue = null;
      if (cookieStart > -1) {
        var cookieEnd = document.cookie.indexOf(";", cookieStart);
        if (cookieEnd == -1) {
          cookieEnd = document.cookie.length;
        }
        cookieValue = encodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
      }
      return decodeURIComponent(decodeURIComponent(cookieValue));
    },
    setCookie: function (name, value, days, path, domain, secure) {
      var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
      if (days>0) {
        var expires = new Date();
        expires.setTime(expires.getTime() + days * 3600000 * 24);
        //expires.setTime(expires.getTime() + days * 120000);
        var _expires = (typeof days) == "string" ? "" : ";expires=" + expires.toUTCString();
        cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value)+ _expires + path;
      }
      if (path) {
        cookieText += "; path=" + path;
      }
      if (domain) {
        cookieText += "; domain=" + domain;
      }
      if (secure) {
        cookieText += "; secure"
      }
      document.cookie = cookieText;
    },
    unsetCookie: function (name,days, path, domain, secure) {
      this.setCookie(name, "", days, path, domain, secure);
    },
    // 去掉多余的应该隐藏的东西。
    queryTextArea() {
      let list = document.querySelectorAll('textarea');
      list.forEach(el => {
        el.setAttribute("readonly", 'readonly');
      })
      let editTable = document.querySelectorAll('.editTableList');
      editTable.forEach(el => {
        el.style.display = 'none';
      })
      let spanid = document.querySelectorAll('.spanid');
      spanid.forEach(el => {
        el.style.display = 'none';
      })
      let imp_delete = document.querySelectorAll('.ipm_deletetr');
      imp_delete.forEach(el => {
        el.style.display = 'none';
      })
      let hoverx = document.querySelectorAll('.hoverx');
      hoverx.forEach(el => {
        el.style.display = 'none';
      })
      let hover = document.querySelectorAll('.hover');
      hover.forEach(el => {
        el.style.display = 'none';
      })
      let markRowStyle = document.querySelectorAll('.markRowStyle');
      markRowStyle.forEach(el => {
        el.style.display = 'none';
      })
    },
    // 去拿到对应的keyList;
    _getKeyList() {
      let msgdata = this.$i18n.messages["zh-CN"].message;
      let arr = [];
      this.flagdata.forEach((el, index) => {
        arr.push(el.appMenuName);
      });
      let keydata = [];
      for (let key in msgdata) {
        if (arr.indexOf(msgdata[key]) > -1) {
          keydata.push(key);
        }
      }
      this.keydata = keydata;
    },
    ...mapMutations({
      _chang: 'CHANG_SHOW'
    })
  },
  filters: {
    timerYear(value){
      var oDate = new Date(value);
      return oDate.getFullYear();
    },
    timer(value){
      var oDate = new Date(value);
      return oDate.getFullYear() + '/' + (oDate.getMonth() + 1 > 9 ? oDate.getMonth() + 1 : '0' + (oDate.getMonth() + 1)) + '/' + (oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate());
    },
    timernew(value) {
      var oDate = new Date(value);
      return oDate.getFullYear() + '-' + (oDate.getMonth() + 1 > 9 ? oDate.getMonth() + 1 : '0' + (oDate.getMonth() + 1)) + '-' + (oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate());
    },
    timerHen: function (input) {
      var oDate = new Date(input);
      return oDate.getFullYear() + '/' + (oDate.getMonth() + 1 > 9 ? oDate.getMonth() + 1 : '0' + (oDate.getMonth() + 1)) + '/' + (oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate()) + ' ' + (oDate.getHours() > 9 ? oDate.getHours() : '0' + oDate.getHours()) + ':' + (oDate.getMinutes() > 9 ? oDate.getMinutes() : '0' + oDate.getMinutes());
    },
    timerover: function (input) {
      var oDate = new Date(input);
      return oDate.getFullYear() + '-' + (oDate.getMonth() + 1 > 9 ? oDate.getMonth() + 1 : '0' + (oDate.getMonth() + 1)) + '-' + (oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate()) + ' ' + (oDate.getHours() > 9 ? oDate.getHours() : '0' + oDate.getHours()) + ':' + (oDate.getMinutes() > 9 ? oDate.getMinutes() : '0' + oDate.getMinutes());
    },
  }
}
原文地址:https://www.cnblogs.com/cx709452428/p/10148345.html