diff --git a/package-lock.json b/package-lock.json index ec43f2f..c808fb1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "myshop-vue_2", "version": "1.0.0", "dependencies": { + "axios": "^1.5.0", "vue": "^2.5.2", "vue-router": "^3.0.1" }, @@ -1176,8 +1177,7 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "dev": true + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/atob": { "version": "2.1.2", @@ -1235,6 +1235,29 @@ "integrity": "sha512-NmWvPnx0F1SfrQbYwOi7OeaNGokp9XhzNioJ/CSBs8Qa4vxug81mhJEAVZwxXuBmYB5KDRfMq/F3RR0BIU7sWg==", "dev": true }, + "node_modules/axios": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.5.0.tgz", + "integrity": "sha512-D4DdjDo5CY50Qms0qGQTTw6Q44jl7zRwY7bthds06pUGfChBCTcQs+N743eFWGEd6pRTMd6A+I87aWyFV5wiZQ==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/axios/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -3459,7 +3482,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -5249,7 +5271,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "dev": true, "engines": { "node": ">=0.4.0" } @@ -7043,7 +7064,6 @@ "version": "1.15.2", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", - "dev": true, "funding": [ { "type": "individual", @@ -10891,7 +10911,6 @@ "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "dev": true, "engines": { "node": ">= 0.6" } @@ -10900,7 +10919,6 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dev": true, "dependencies": { "mime-db": "1.52.0" }, @@ -15832,6 +15850,11 @@ "integrity": "sha512-a07BiTXhWFUBH0aXOQyW94p13FTDfbxotxWoPmuaUuNAqBQ3kXzgk7XanGiAkx5j9x1MBOM3Yjzf5Selm69D6A==", "dev": true }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/prr": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", diff --git a/package.json b/package.json index 744968f..f797c12 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "myshop-vue_2", "version": "1.0.0", "description": "A Vue.js project", - "author": "RogerWork ", + "author": "roger_home_pc ", "private": true, "scripts": { "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js", @@ -14,6 +14,7 @@ "build": "node build/build.js" }, "dependencies": { + "axios": "^1.5.0", "vue": "^2.5.2", "vue-router": "^3.0.1" }, diff --git a/src/utils/requests.js b/src/utils/requests.js new file mode 100644 index 0000000..c9ec73f --- /dev/null +++ b/src/utils/requests.js @@ -0,0 +1,69 @@ +import axios from 'axios' + +// 官方文档 https://www.axios-http.cn/ + +/* +// GET请求 +axios.get('http://www.baidu.com').then(function (response) { + console.log(response) +}).catch(function (errors) { + console.log(errors) +}) + +// POST请求 +axios.post('http://www.baidu.com', {kw: 'haha'}).then(function (response) { + console.log(response) +}).catch(function (errors) { + console.log(errors) +}) + +// GET请求二 +axios('http://www.baidu.com', { + params: { + id: 1 + } +}).then(res => {console.log(res)}) + +// POST请求二 +axios({ + url: 'http://www.baidu.com', + method: 'post', + data: { + id: 1, + } +}).then(res => { + console.log(res) +}) +*/ + +var URL = 'http://localhost:8000/' + +// 创建axios实例 +const service = axios.create({ + baseURL: URL, timeout: 6000 // 6m秒超时 +}) + +// request拦截器 +service.interceptors.request.use(config => { + const {url} = config // 解构赋值 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment + //指定页面访问需要JWT认证。 + if (url == '/cart/' || url == '/order/' || url == '/checkout/' || url == '/myorder/' || url.indexOf('address/') > 0 || url == 'profile/' || url.indexOf('users/') > 0) { + var jwt = localStorage.getItem('token') + config.headers.Authorization = 'JWT ' + jwt + } + return config +}) + +service.interceptors.response.use(response => { + return response +}, error => { + console.log(error.response) + // JWT鉴权失败 + if (error.response.status === 401) { + alert('请先登录!') + } + return Promise.reject(error.response.data) +}) + +export default service +