vue中get和post請求

import axios from 'axios';
import router from '@/router';
import {
    setSessionStorage,
    getSessionStorage,
    removeSessionStorage
} from '@/utils/mixin';
class Http {
  constructor(){
    this.Domain = 'http://*******';
  }
  require(options) {
    if (!options.api) throw new Error('api 不能爲空');
    if (!options.param) { options.param = {} };
    if (!options.methods) { options.methods = 'POST' }; //不傳遞方法默認爲POST
  };
  return new Promise((resolve,reject) => {
    let obj={
      method: options.methods,
      url: options.api,
      baseURL: '/wap',
      headers: {
        'appname':'com.immortal.jeeqin',
        'platform':'web',
        'v':'1.0',
        'lang':'zh-CN',
        'country':'CN',
        'token':getSessionStorage('token') || ''
      },
      params: options.param,//使用get方法時參數必須使用params
      data:options.param
    }
 
    if(options.methods==='GET'){//判斷方法是get時候 參數使用params
      delete obj.data
    }else{
      delete obj.params
    }
    return axios(obj).then(response => {
      const {state} = response.data;
      //響應數據攔截處理
      if (state == 2) {
        //登陸超時或互踢致使登陸異常,跳轉至登陸頁面
        removeSessionStorage('token');
        window.location.hash = '#/login';
      } else {
        //請求成功
        return resolve(response.data);
      }
      },error => {
        return reject()
      }
    })
  }
  export default Http;
相關文章
相關標籤/搜索