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

perf:改进axios使用

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