提交 58141287 编写于 作者: yma16's avatar yma16

perf:改进axios使用

上级 e2e420e6
......@@ -38,6 +38,7 @@ Vue.directive('highlight', function (el) {
Vue.prototype.$cookies = cookies
// axios
Vue.prototype.$axios = axios
window.$axios_w = axios
Vue.prototype.$cookies.set('user_session', 'null')
......
'use strict'
import Vue from 'vue'
import axios from 'axios'
// Full config: https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
let config = {
// baseURL: process.env.baseURL || process.env.apiUrl || ""
// timeout: 60 * 1000, // Timeout
// withCredentials: true, // Check cross-site Access-Control
}
const _axios = axios.create(config)
_axios.interceptors.request.use(
function (config) {
// Do something before request is sent
return config
},
function (error) {
// Do something with request error
return Promise.reject(error)
}
)
// Add a response interceptor
_axios.interceptors.response.use(
function (response) {
// Do something with response data
return response
},
function (error) {
// Do something with response error
return Promise.reject(error)
}
)
Plugin.install = function (Vue, options) {
Vue.axios = _axios
window.axios = _axios
Object.defineProperties(Vue.prototype, {
axios: {
get () {
return _axios
}
},
$axios: {
get () {
return _axios
}
}
})
}
Vue.use(Plugin)
export default Plugin
// 'use strict'
//
// import Vue from 'vue'
// import axios from 'axios'
//
// // Full config: https://github.com/axios/axios#request-config
// // axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
//
// let config = {
// // baseURL: process.env.baseURL || process.env.apiUrl || ""
// // timeout: 60 * 1000, // Timeout
// // withCredentials: true, // Check cross-site Access-Control
// }
//
// const _axios = axios.create(config)
//
// _axios.interceptors.request.use(
// function (config) {
// // Do something before request is sent
// return config
// },
// function (error) {
// // Do something with request error
// return Promise.reject(error)
// }
// )
//
// // Add a response interceptor
// _axios.interceptors.response.use(
// function (response) {
// // Do something with response data
// return response
// },
// function (error) {
// // Do something with response error
// return Promise.reject(error)
// }
// )
//
// Plugin.install = function (Vue, options) {
// Vue.axios = _axios
// window.axios = _axios
// Object.defineProperties(Vue.prototype, {
// axios: {
// get () {
// return _axios
// }
// },
// $axios: {
// get () {
// return _axios
// }
// }
// })
// }
//
// Vue.use(Plugin)
//
// export default Plugin
import axios from "axios";
export function loginUser(data) {
console.log("用户登录service!", data);
let baseUrl = "/api/user/login/";
let res = null;
axios
export function loginUser (data) {
console.log('用户登录service!', data)
let baseUrl = '/api/user/login/'
let res = null
window.$axios_w && window.$axios_w
.post(baseUrl, data)
.then((o) => {
res = o;
console.log("成功认证", res);
res = o
console.log('成功认证', res)
// 只返回username
localStorage.setItem(
"yma16siteUserInfo",
'yma16siteUserInfo',
JSON.stringify({
username: data.name, //保存一个用户名
password: data.password,
})
);
)
})
.catch((r) => {
console.log("登录失败!", r);
});
return res;
console.log('登录失败!', r)
})
return res
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册