diff --git a/api/http.js b/api/http.js index e69de29..0d622b4 100644 --- a/api/http.js +++ b/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 diff --git a/api/index.js b/api/index.js index e69de29..574bbab 100644 --- a/api/index.js +++ b/api/index.js @@ -0,0 +1,5 @@ +import LoginApi from '@/api/modules/Login.js' + +export { + LoginApi +} diff --git a/api/modules/Login.js b/api/modules/Login.js index e69de29..59ce5c9 100644 --- a/api/modules/Login.js +++ b/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) + } +} diff --git a/app.config.js b/app.config.js new file mode 100644 index 0000000..579a1d7 --- /dev/null +++ b/app.config.js @@ -0,0 +1,5 @@ +const BASE_URL = 'http://baidu.com' + +export default { + BASE_URL +}