diff --git a/src/api/api.js b/src/api/api.js index b3aa6aa..a31c6d4 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -1,56 +1,25 @@ -//vxvdvdsvsdvvds3 -import router from "@/router" -import axios from "axios" -// import Promise from 'promise' -import {yztoken} from "@/utlis/istoken" +import request from '@/api/request' import mm from "@/utlis/mm"; -import { Message } from 'element-ui'; -const instance = axios.create({ - baseURL: mm.apiurl, - timeout:5000 -}); - -//请求拦截 -instance.interceptors.request.use( - - function (config) { - yztoken() - let token = localStorage.getItem('token') - let tokentype = localStorage.getItem('tokentype') - config.headers['Access-Control-Allow-Origin'] = '*' - // config.headers.ContentType = 'application/x-www-form-urlencoded' - config.headers.Authorization = `${tokentype} ${token}` - return config - }, - function (err) { - console.log('666') - return Promise.reject(err) - } -) -// let urls='http://81.70.217.145:9529' -// const urls='http://81.70.217.145:9529' -//axios中get的封装 -// const toatalpath = 'http://81.70.217.145:9529' -export function getapi(url, params = {}, config) { - return new Promise((resolve, reject) => { - instance.get(`${mm.apiurl}${url}`, { - params: params, - ...config - }).then(res => { - resolve(res.data) - }).catch(err => { - reject(err.data) + export function getapi(url, params = {}, config) { + return new Promise((resolve, reject) => { + request.get(`${mm.apiurl}${url}`, { + params: params, + ...config + }).then(res => { + resolve(res) + }).catch(err => { + reject(err.data) + }) }) - }) -} - -//axios封装post + } + + //axios封装post export function postapi(url, paramsdata = {}, config) { return new Promise((resolve, reject) => { - instance.post(`${mm.apiurl}${url}`, paramsdata, config).then(res => { - resolve(res.data) + request.post(`${mm.apiurl}${url}`, paramsdata, config).then(res => { + resolve(res) }, err => { reject(err.data) }) @@ -59,11 +28,11 @@ export function postapi(url, paramsdata = {}, config) { //axios封装deleteapi export function deletapi(url, params = {}, config) { return new Promise((resolve, reject) => { - instance.delete(`${mm.apiurl}${url}`, { + request.delete(`${mm.apiurl}${url}`, { params: params, ...config }).then(res => { - resolve(res.data) + resolve(res) }).catch(err => { reject(err.data) }) @@ -72,59 +41,10 @@ export function deletapi(url, params = {}, config) { //axios封装Put方法 export function putapi(url, params = {}, config) { return new Promise((resolve, reject) => { - instance.put(`${mm.apiurl}${url}`, params, config).then(res => { - resolve(res.data) + request.put(`${mm.apiurl}${url}`, params, config).then(res => { + resolve(res) }).catch(err => { reject(err.data) }) }) -} -//响应拦截 -instance.interceptors.response.use( - (res) => { - //console.log(res); - if (res.data.code == 1) { - // const { data } = res; - return res - } else if (res.Code == -1) { - // console.log(res.data.message); - // this.$message({ - // message: res.data.message, - // type: 'warning' - // }); - Message.error(res.Message); - } - }, - (err) => { - console.log(err); - if (response.data.code == -1) { - router.push({ path: '/login' }) - // this.$router.push({path:'/login'}) - // this.$message.success('状态失效') - Message.error('状态失效请重新登录') - console.log('token失效返回登录'); - } - console.log(err); - return Promise.reject(err) - } - // function (response) { - // console.log(response,10010) - // const {data} = response - // if (data.status == 401) { - - // window.location.href = '#/login' - // this.$message.error('状态失效'); - // } - // return data - // }, - // function (err) { - // if (err.response.status === 401) { - // window.location.href = '#/login' - // this.$router.push({ path: '/login' }) - // this.$message.error('登录失效请重新登录'); - // } - // return err - // } -) - -export default instance \ No newline at end of file +} \ No newline at end of file diff --git a/src/api/request.js b/src/api/request.js new file mode 100644 index 0000000..68da5f5 --- /dev/null +++ b/src/api/request.js @@ -0,0 +1,83 @@ +//vxvdvdsvsdvvds3 +import router from "@/router" +import axios from "axios" +// import Promise from 'promise' +import {yztoken} from "@/utlis/istoken" +import mm from "@/utlis/mm"; + +import { Message } from 'element-ui'; +const instance = axios.create({ + baseURL: mm.apiurl, + timeout:5000 +}); + + +//请求拦截 +instance.interceptors.request.use( + + function (config) { + yztoken() + let token = localStorage.getItem('token') + let tokentype = localStorage.getItem('tokentype') + config.headers['Access-Control-Allow-Origin'] = '*' + // config.headers.ContentType = 'application/x-www-form-urlencoded' + config.headers.Authorization = `${tokentype} ${token}` + return config + }, + function (err) { + console.log('666') + return Promise.reject(err) + } +) + +//响应拦截 +instance.interceptors.response.use( + (res) => { + //console.log(res); + if (res.data.code == 1) { + // const { data } = res; + console.log(9527); + console.log(res); + return res.data + } else if (res.Code == -1) { + // console.log(res.data.message); + // this.$message({ + // message: res.data.message, + // type: 'warning' + // }); + Message.error(res.Message); + } + }, + (err) => { + console.log(err); + if (response.data.code == -1) { + router.push({ path: '/login' }) + // this.$router.push({path:'/login'}) + // this.$message.success('状态失效') + Message.error('状态失效请重新登录') + console.log('token失效返回登录'); + } + console.log(err); + return Promise.reject(err) + } + // function (response) { + // console.log(response,10010) + // const {data} = response + // if (data.status == 401) { + + // window.location.href = '#/login' + // this.$message.error('状态失效'); + // } + // return data + // }, + // function (err) { + // if (err.response.status === 401) { + // window.location.href = '#/login' + // this.$router.push({ path: '/login' }) + // this.$message.error('登录失效请重新登录'); + // } + // return err + // } +) + +export default instance \ No newline at end of file