Browse Source

封装网络请求方式

master
spirit2020 3 years ago
parent
commit
df118b1795
  1. 41
      api/http.js
  2. 5
      api/index.js
  3. 15
      api/modules/Login.js
  4. 5
      app.config.js

41
api/http.js

@ -0,0 +1,41 @@
import Request from '@/common/libs/luch-request/index.js'
import App from '@/app.config.js'
const http = new Request()
http.setConfig(config => {
config.baseURL = App.BASE_URL
return config
})
function Token () {
return uni.getStorageSync('token') || ''
}
http.interceptors.request.use(config => {
config.header = {
...config.header,
token: Token
}
return config
}, config => {
return Promise.reject(config)
})
http.interceptors.response.use((response) => {
const data = response.data
if(/([1345]\d*)/.test(data.code)) {
uni.showToast({
icon: 'none',
title: data.msg
})
return Promise.reject(response)
}
return response
}, (response) => {
return Promise.reject(response)
})
export default http

5
api/index.js

@ -0,0 +1,5 @@
import LoginApi from '@/api/modules/Login.js'
export {
LoginApi
}

15
api/modules/Login.js

@ -0,0 +1,15 @@
import http from '../http.js'
const BASE_PREFIX = '/test/login'
class Login {
constructor() {
this.api = {
login: BASE_PREFIX + '/loign'
}
}
login(data) {
return http.post(this.api.login, data).then(r => r.data)
}
}

5
app.config.js

@ -0,0 +1,5 @@
const BASE_URL = 'http://baidu.com'
export default {
BASE_URL
}
Loading…
Cancel
Save