Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mould-vuecli3
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lhfe
mould-vuecli3
Commits
befd1250
Commit
befd1250
authored
Sep 01, 2020
by
lipengcheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: axios
parent
f626eca7
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
138 additions
and
26 deletions
+138
-26
App.vue
src/App.vue
+1
-1
baseUrl.js
src/config/baseUrl.js
+0
-0
proxy.config.js
src/config/proxy.config.js
+32
-0
main.js
src/main.js
+1
-4
axios.js
src/plugins/axios.js
+74
-8
axiosBackup.js
src/plugins/axiosBackup.js
+20
-0
global.js
src/store/global.js
+3
-3
home.vue
src/views/home.vue
+2
-1
vue.config.js
vue.config.js
+5
-9
No files found.
src/App.vue
View file @
befd1250
...
@@ -19,7 +19,7 @@ export default {
...
@@ -19,7 +19,7 @@ export default {
},
},
methods
:
{
methods
:
{
async
request
()
{
async
request
()
{
let
result
=
await
this
.
$http
.
get
(
'/
webapi/
home/banner?type=1&category=18'
)
let
result
=
await
this
.
$http
.
get
(
'/home/banner?type=1&category=18'
)
console
.
log
(
'result:'
,
result
)
console
.
log
(
'result:'
,
result
)
}
}
}
}
...
...
config/baseUrl.js
→
src/
config/baseUrl.js
View file @
befd1250
File moved
src/config/proxy.config.js
0 → 100644
View file @
befd1250
module
.
exports
=
{
proxyapi
:
{
target
:
'http://127.0.0.1:3008'
,
changeOrigin
:
true
,
pathRewrite
:
{
'^/proxyapi'
:
'/'
}
},
webapi
:
{
target
:
'https://test2.laihua.com/'
,
changeOrigin
:
true
,
pathRewrite
:
{
'^/webapi'
:
'/'
}
},
mockapi16
:
{
target
:
'http://y-api.laihua.com/mock/16/'
,
changeOrigin
:
true
,
pathRewrite
:
{
'^/mockapi16'
:
'/'
}
},
appapi
:
{
target
:
'http://test.api.laihua.com/'
,
changeOrigin
:
true
,
pathRewrite
:
{
'^/appapi'
:
'/'
}
},
collectapi
:
{
target
:
'https://collection.laihua.com/'
,
changeOrigin
:
true
,
pathRewrite
:
{
'^/collectapi'
:
'/'
}
},
weiboavatar
:
{
target
:
'https://tva4.sinaimg.cn/'
,
changeOrigin
:
true
,
pathRewrite
:
{
'^/weiboavatar'
:
'/'
}
}
}
src/main.js
View file @
befd1250
...
@@ -13,7 +13,7 @@ import globalComponents from '@/components/global/index.js'
...
@@ -13,7 +13,7 @@ import globalComponents from '@/components/global/index.js'
import
filters
from
'@/filters/index.js'
import
filters
from
'@/filters/index.js'
import
directives
from
'@/directives/index.js'
import
directives
from
'@/directives/index.js'
import
utils
from
'@/utils/index.js'
import
utils
from
'@/utils/index.js'
console
.
log
(
'
axios:'
,
axios
)
console
.
log
(
'
process.env:'
,
process
.
env
)
// 全局注册layout组件
// 全局注册layout组件
Vue
.
use
(
layoutComponents
)
Vue
.
use
(
layoutComponents
)
// 注册自定义全局组件
// 注册自定义全局组件
...
@@ -26,15 +26,12 @@ Object.keys(directives).forEach((key) => Vue.directive(key, directives[key]))
...
@@ -26,15 +26,12 @@ Object.keys(directives).forEach((key) => Vue.directive(key, directives[key]))
Vue
.
use
(
VueBus
)
Vue
.
use
(
VueBus
)
Vue
.
use
(
VModal
)
Vue
.
use
(
VModal
)
import
baseURL
from
'../config/baseUrl.js'
console
.
log
(
'baseURL:'
,
baseURL
)
Vue
.
config
.
productionTip
=
false
Vue
.
config
.
productionTip
=
false
Vue
.
prototype
.
$http
=
axios
Vue
.
prototype
.
$http
=
axios
Vue
.
prototype
.
$mt
=
moment
Vue
.
prototype
.
$mt
=
moment
Vue
.
prototype
.
$
=
utils
Vue
.
prototype
.
$
=
utils
new
Vue
({
new
Vue
({
router
,
router
,
store
,
store
,
render
:
(
h
)
=>
h
(
App
)
render
:
(
h
)
=>
h
(
App
)
}).
$mount
(
'#app'
)
}).
$mount
(
'#app'
)
src/plugins/axios.js
View file @
befd1250
import
axios
from
'axios'
import
axios
from
'axios'
console
.
log
(
'axios:'
,
axios
)
// import store from '@/store/index'
// import router from '@/router/index.js'
/**
/**
* 默认配置
* 默认配置
*/
*/
axios
.
defaults
.
baseURL
=
''
console
.
log
(
'axios-process.env:'
,
process
.
env
)
axios
.
defaults
.
baseURL
=
process
.
env
.
VUE_APP_BASEURL
axios
.
defaults
.
timeout
=
10000
axios
.
defaults
.
headers
.
common
[
'a-laihua-defined-key'
]
=
'a customized value'
axios
.
defaults
.
headers
.
common
[
'a-laihua-defined-key'
]
=
'a customized value'
// 请求拦截
// 请求拦截
...
@@ -20,15 +23,78 @@ axios.interceptors.request.use(
...
@@ -20,15 +23,78 @@ axios.interceptors.request.use(
}
}
)
)
// 处理响应错误码
const
checkStatus
=
(
response
)
=>
{
const
status
=
response
.
status
// 如果http响应状态码response.status正常,则直接返回数据
if
((
status
>=
200
&&
status
<
300
)
||
status
===
304
)
{
return
response
.
data
}
else
{
// 判断后端返回的code
let
errMsg
=
''
const
code
=
parseInt
(
response
.
data
&&
response
.
data
.
code
)
console
.
log
(
'code:'
,
code
)
switch
(
code
)
{
case
400
:
break
case
4001
:
if
(
process
.
server
)
return
errMsg
=
'登录设备数量超出限制'
// store.commit('savehttpResult', { res: response.data })
break
case
403
:
errMsg
=
'未登录'
// store.commit('changeLogin', {})
break
case
412
:
errMsg
=
'未找到有效session'
break
case
408
:
errMsg
=
'请求超时'
break
case
500
:
errMsg
=
'服务器内部错误'
break
case
501
:
errMsg
=
'服务未实现'
break
case
502
:
errMsg
=
'网关错误'
break
case
503
:
errMsg
=
'服务不可用'
break
case
504
:
errMsg
=
'网关超时'
break
case
505
:
errMsg
=
'HTTP版本不受支持'
break
default
:
// errMsg = err.response.data.msg
break
}
console
.
log
(
'errMsg:'
,
errMsg
)
return
{
code
,
errMsg
}
}
}
// 响应拦截
// 响应拦截
axios
.
interceptors
.
response
.
use
(
axios
.
interceptors
.
response
.
use
(
(
response
)
=>
{
(
response
)
=>
Promise
.
resolve
(
checkStatus
(
response
)),
// 对响应数据做点什么
(
err
)
=>
{
return
response
console
.
log
(
'响应错误拦截err:'
,
err
)
},
function
(
error
)
{
// 对响应错误做点什么
// 对响应错误做点什么
return
Promise
.
reject
(
error
)
if
(
axios
.
isCancel
(
err
))
{
throw
new
axios
.
Cancel
(
'cancel request'
)
}
else
{
console
.
log
(
'网络请求失败,请重试:'
)
}
return
Promise
.
reject
(
err
)
}
}
)
)
...
...
src/plugins/axiosBackup.js
0 → 100644
View file @
befd1250
import
axios
from
'axios'
// let baseurl = require('../../static/js/config.js').default; // 引入接口地址
const
myHttp
=
{}
myHttp
.
install
=
function
(
Vue
)
{
const
http
=
axios
.
create
({
// 使用create创建了一个实例对象
timeout
:
1000
*
600
,
// 请求超时时间(毫秒)
withCredentials
:
true
,
// 是否携带cookie信息
headers
:
{
'Content-Type'
:
'application/json; charset=utf-8'
}
// baseURL: baseurl // 请求的接口地址ip + 端口号
})
Vue
.
prototype
.
$http
=
http
// 这样配置完就可以全局调用了
}
export
default
myHttp
src/store/global.js
View file @
befd1250
// 全局vuex信息
// 全局vuex信息
// getter调用示例:this.$store.getters['
test
']
// getter调用示例:this.$store.getters['
authed
']
const
state
=
{
const
state
=
{
_
test
:
1
_
authed
:
false
}
}
const
getters
=
{
const
getters
=
{
test
:
(
state
)
=>
state
.
_test
authed
:
(
state
)
=>
state
.
_authed
}
}
const
mutations
=
{}
const
mutations
=
{}
...
...
src/views/home.vue
View file @
befd1250
...
@@ -17,7 +17,8 @@ export default {
...
@@ -17,7 +17,8 @@ export default {
return
{}
return
{}
},
},
created
()
{},
created
()
{},
mounted
()
{}
mounted
()
{},
methods
:
{}
}
}
</
script
>
</
script
>
...
...
vue.config.js
View file @
befd1250
console
.
log
(
'process.env.NODE_ENV1:'
,
process
.
env
.
NODE_ENV
)
const
proxyConf
=
require
(
'./src/config/proxy.config.js'
)
console
.
log
(
'process.env.NODE_ENV1:'
,
process
.
env
)
const
LodashModuleReplacementPlugin
=
require
(
'lodash-webpack-plugin'
)
const
LodashModuleReplacementPlugin
=
require
(
'lodash-webpack-plugin'
)
...
@@ -12,14 +14,7 @@ module.exports = {
...
@@ -12,14 +14,7 @@ module.exports = {
port
:
3020
,
port
:
3020
,
https
:
false
,
https
:
false
,
hotOnly
:
false
,
hotOnly
:
false
,
proxy
:
{
proxy
:
proxyConf
webapi
:
{
target
:
'https://test2.laihua.com/'
,
ws
:
true
,
changeOrigin
:
true
,
pathRewrite
:
{
'^/webapi'
:
'/'
}
}
}
},
},
chainWebpack
:
(
config
)
=>
{
chainWebpack
:
(
config
)
=>
{
config
.
when
(
process
.
env
.
NODE_ENV
!==
'development'
,
(
config
)
=>
{
config
.
when
(
process
.
env
.
NODE_ENV
!==
'development'
,
(
config
)
=>
{
...
@@ -41,6 +36,7 @@ module.exports = {
...
@@ -41,6 +36,7 @@ module.exports = {
return
options
return
options
})
})
},
},
// 使用全局scss变量。但会导致打包变慢
css
:
{
css
:
{
loaderOptions
:
{
loaderOptions
:
{
sass
:
{
sass
:
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment