Skip to content

Commit 400d285

Browse files
author
Riley Ren
committed
update
1 parent c3e477b commit 400d285

File tree

13 files changed

+114
-16
lines changed

13 files changed

+114
-16
lines changed

config/dev.env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ const merge = require('webpack-merge')
33
const prodEnv = require('./prod.env')
44

55
module.exports = merge(prodEnv, {
6-
NODE_ENV: '"development"'
6+
NODE_ENV: '"development"',
77
})

config/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ module.exports = {
1010
// Paths
1111
assetsSubDirectory: 'static',
1212
assetsPublicPath: '/',
13-
proxyTable: {},
13+
proxyTable: {
14+
'/vue-test-api': {
15+
target: 'http://localhost', // 接口的域名
16+
// secure: false, // 如果是https接口,需要配置这个参数
17+
changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
18+
pathRewrite: {
19+
// '^/vue-test-api': ''
20+
}
21+
}
22+
},
1423

1524
// Various Dev Server settings
1625
host: 'localhost', // can be overwritten by process.env.HOST

config/prod.env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
'use strict'
22
module.exports = {
3-
NODE_ENV: '"production"'
3+
NODE_ENV: '"production"',
44
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
"iview": "^2.11.0",
1616
"js-cookie": "^2.2.0",
1717
"localforage": "^1.6.0",
18-
"node-sass": "^4.7.2",
1918
"qs": "^6.5.1",
20-
"sass-loader": "^6.0.7",
2119
"vue": "^2.5.2",
2220
"vue-router": "^3.0.1",
2321
"vuex": "^3.0.1"
2422
},
2523
"devDependencies": {
24+
"sass-loader": "^6.0.7",
25+
"node-sass": "^4.7.2",
2626
"autoprefixer": "^7.1.2",
2727
"babel-core": "^6.22.1",
2828
"babel-eslint": "^8.2.1",

src/api/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as request from './request';
22
import sign from './sign';
33
import todo from './todo';
4-
import { API_URL } from 'config';
5-
64

75

86
export default {

src/api/request.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,59 @@ import axios from 'axios';
22
import qs from 'qs';
33
import { API_BASE_URL } from 'config';
44

5-
axios.interceptors.request.use((config) => {
6-
config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
5+
6+
// 创建axios实例
7+
const service = axios.create({
8+
// baseURL: process.env.BASE_API_URL, // node环境的不同,对应不同的baseURL
9+
baseURL: API_BASE_URL, // node环境的不同,对应不同的baseURL
10+
timeout: 5000, // 请求的超时时间
11+
//设置默认请求头,使post请求发送的是formdata格式数据// axios的header默认的Content-Type好像是'application/json;charset=UTF-8',我的项目都是用json格式传输,如果需要更改的话,可以用这种方式修改
12+
headers: {
13+
"Content-Type": "application/x-www-form-urlencoded"
14+
},
15+
withCredentials: true // 允许携带cookie
16+
})
17+
18+
// 发送请求前处理request的数据
19+
axios.defaults.transformRequest = [function (data) {
20+
let newData = ''
21+
for (let k in data) {
22+
newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&'
23+
}
24+
return newData
25+
}]
26+
27+
// request拦截器
28+
// service.interceptors.request.use(
29+
// config => {
30+
// // 发送请求之前,要做的业务
31+
// return config
32+
// },
33+
// error => {
34+
// // 错误处理代码
35+
36+
// return Promise.reject(error)
37+
// }
38+
// )
39+
40+
// response拦截器
41+
service.interceptors.response.use(
42+
response => {
43+
// 数据响应之后,要做的业务
44+
return response
45+
},
46+
error => {
47+
return Promise.reject(error)
48+
}
49+
)
50+
51+
// export default service
52+
53+
54+
55+
56+
service.interceptors.request.use((config) => {
57+
// config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
758
if (config.method === 'post') {
859
config.data = qs.stringify({
960
...config.data,
@@ -15,7 +66,8 @@ axios.interceptors.request.use((config) => {
1566

1667
function baseRequest(url, params, method = 'get') {
1768
return new Promise((resolve, reject) => {
18-
axios[method](`${API_BASE_URL}${url}`, params).then((response) => {
69+
// axios[method](`${API_BASE_URL}${url}`, params).then((response) => {
70+
service[method](`${url}`, params).then((response) => {
1971
const json = response.data;
2072
if (parseInt(json.code, 10) === 0) {
2173
resolve(json.data);

src/api/sign.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { get, post } from './request';
2-
import * as apis from '@/conf/apis';
2+
import * as apis from '@/constants/apis';
33

44
export default {
55
signIn: (user, password) => post(apis.SIGN_IN, {

src/api/todo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { get, post } from './request';
2-
import * as apis from '@/conf/apis';
2+
import * as apis from '@/constants/apis';
33

44
export default {
55
fetch: () => get(apis.FETCH_TODO),

src/conf/config-dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const API_BASE_URL = 'http://127.0.0.1/vue-test-api/';
1+
export const API_BASE_URL = 'http://127.0.0.1:8080/vue-test-api/';

src/conf/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const API_BASE_URL = 'http://127.0.0.1/vue-test-api/';
1+
export const API_BASE_URL = 'http://127.0.0.1:8080/vue-test-api/';

0 commit comments

Comments
 (0)