| | |
| | | // 手机端:http://www.shehua56.com/wap2.0/ |
| | | // 账户:15888888888 密码:123456 |
| | | |
| | | import axios from 'axios' // 引入axios |
| | | import qs from 'qs' // 引入qs |
| | | // import store from '@/store' |
| | | // import router from '@/router' |
| | | import APIUrl from './api.url' // 引入api.url.js |
| | | import axios from "axios"; // 引入axios |
| | | import qs from "qs"; // 引入qs |
| | | import store from "@/store"; |
| | | import router from "@/router"; |
| | | import APIUrl from "./api.url"; // 引入api.url.js |
| | | |
| | | // axios 默认配置 更多配置查看Axios中文文档 |
| | | axios.defaults.timeout = 50000 // 超时默认值 |
| | | axios.defaults.baseURL = APIUrl.baseURL // 默认baseURL |
| | | axios.defaults.responseType = 'json' // 默认数据响应类型 |
| | | axios.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8' |
| | | axios.defaults.timeout = 50000; // 超时默认值 |
| | | axios.defaults.baseURL = APIUrl.baseURL; // 默认baseURL |
| | | axios.defaults.responseType = "json"; // 默认数据响应类型 |
| | | axios.defaults.headers.common["Content-Type"] = |
| | | "application/json;charset=UTF-8"; |
| | | // axios.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded'; |
| | | axios.defaults.withCredentials = true // 表示跨域请求时是否需要使用凭证 |
| | | axios.defaults.withCredentials = true; // 表示跨域请求时是否需要使用凭证 |
| | | |
| | | // http request 拦截器 |
| | | // 在ajax发送之前拦截 比如对所有请求统一添加header token |
| | | axios.interceptors.request.use( |
| | | config => { |
| | | config.headers['lang'] = localStorage.getItem('language') ? localStorage.getItem('language') : 'zh-CN' |
| | | if (window.localStorage.getItem('USERTOKEN')) { |
| | | config.headers["lang"] = localStorage.getItem("language") |
| | | ? localStorage.getItem("language") |
| | | : "zh-CN"; |
| | | if (window.localStorage.getItem("USERTOKEN")) { |
| | | // config.headers.Authorization = `USER_TOKEN = ${store.state.token}`; |
| | | config.headers['USERTOKEN'] = window.localStorage.getItem('USERTOKEN') |
| | | config.headers["USERTOKEN"] = window.localStorage.getItem("USERTOKEN"); |
| | | } |
| | | console.log(config) |
| | | return config |
| | | console.log(config); |
| | | return config; |
| | | }, |
| | | err => { |
| | | return Promise.reject(err) |
| | | return Promise.reject(err); |
| | | } |
| | | ) |
| | | ); |
| | | |
| | | // http response 拦截器 |
| | | // ajax请求回调之前拦截 对请求返回的信息做统一处理 比如error为401无权限则跳转到登陆界面 |
| | | axios.interceptors.response.use( |
| | | response => { |
| | | // switch (response.data && response.data.success) { |
| | | // case false: |
| | | // response.data.msg = '您还未登录,请先登录' |
| | | // //console.log(response.data) |
| | | // router.replace({ |
| | | // path: 'openaccount' |
| | | // }) |
| | | // break |
| | | // default: |
| | | // break |
| | | // } |
| | | return response |
| | | if (response.data && response.data.status === 401) { |
| | | store.commit("undataToken", null); |
| | | window.localStorage.setItem("USERTOKEN", null); |
| | | router.push("/login"); |
| | | return response; |
| | | } |
| | | |
| | | return response; |
| | | }, |
| | | error => { |
| | | // console.log(error,'error'); |
| | | return Promise.reject(error) |
| | | return Promise.reject(error); |
| | | } |
| | | ) |
| | | ); |
| | | |
| | | export default axios // 这句千万不能漏下!!! |
| | | export default axios; // 这句千万不能漏下!!! |
| | | |
| | | /** |
| | | * post 方法封装 |
| | |
| | | * @param data |
| | | * @returns {Promise} |
| | | */ |
| | | export function post (url, data = {}, out) { |
| | | export function post(url, data = {}, out) { |
| | | return new Promise((resolve, reject) => { |
| | | axios.post(url, qs.stringify(data)) |
| | | .then(response => { |
| | | resolve(response.data) |
| | | }, err => { |
| | | reject(err) |
| | | }) |
| | | }) |
| | | axios.post(url, qs.stringify(data)).then( |
| | | response => { |
| | | resolve(response.data); |
| | | }, |
| | | err => { |
| | | reject(err); |
| | | } |
| | | ); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param data |
| | | * @returns {Promise} |
| | | */ |
| | | export function get (url, data = {}) { |
| | | export function get(url, data = {}) { |
| | | return new Promise((resolve, reject) => { |
| | | axios.get(url, { params: data }) |
| | | .then(response => { |
| | | resolve(response.data) |
| | | }, err => { |
| | | reject(err) |
| | | }) |
| | | }) |
| | | axios.get(url, { params: data }).then( |
| | | response => { |
| | | resolve(response.data); |
| | | }, |
| | | err => { |
| | | reject(err); |
| | | } |
| | | ); |
| | | }); |
| | | } |
| | | |
| | | /** |