From 58141287fd87a69c662d2b245901569285becd54 Mon Sep 17 00:00:00 2001 From: yma16 <17685043634@163.com> Date: Sun, 24 Jul 2022 23:49:10 +0800 Subject: [PATCH] =?UTF-8?q?perf:=E6=94=B9=E8=BF=9Baxios=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.js | 1 + src/plugins/axios.js | 122 ++++++++++++++++++------------------ src/service/user.service.js | 26 ++++---- 3 files changed, 74 insertions(+), 75 deletions(-) diff --git a/src/main.js b/src/main.js index f028dc3..cce5968 100644 --- a/src/main.js +++ b/src/main.js @@ -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') diff --git a/src/plugins/axios.js b/src/plugins/axios.js index d5bc273..430d469 100644 --- a/src/plugins/axios.js +++ b/src/plugins/axios.js @@ -1,61 +1,61 @@ -'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 diff --git a/src/service/user.service.js b/src/service/user.service.js index a1a78ea..afbad03 100644 --- a/src/service/user.service.js +++ b/src/service/user.service.js @@ -1,25 +1,23 @@ -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 } -- GitLab