|
|
@@ -2,17 +2,23 @@ import store from '@/store'
|
|
|
import config from './config'
|
|
|
import utils from './utils'
|
|
|
|
|
|
-const request = (method, url, data) => {
|
|
|
+const request = (method, url, data, opt) => {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
+ opt.showLoading && uni.showLoading({
|
|
|
+ title: opt.loadingTitle
|
|
|
+ })
|
|
|
+
|
|
|
uni.request({
|
|
|
method,
|
|
|
url: utils.isHttp(url) ? url : config.apiBaseUrl + url,
|
|
|
header: {
|
|
|
- Authorization: store.state.token
|
|
|
+ Authorization: store.state.token,
|
|
|
+ ...opt.header
|
|
|
},
|
|
|
data
|
|
|
}).then(res => {
|
|
|
- if (res.code === 201) {
|
|
|
+ const data = res.data
|
|
|
+ if (data.code === 201) {
|
|
|
store.commit('setUserInfo', {})
|
|
|
|
|
|
// opt.isAuth && uni.navigateTo({
|
|
|
@@ -20,20 +26,19 @@ const request = (method, url, data) => {
|
|
|
// })
|
|
|
}
|
|
|
|
|
|
- if (res.code === 200 || res.success) {
|
|
|
- resolve(res)
|
|
|
+ if (data.code === 200 || data.success) {
|
|
|
+ resolve(data)
|
|
|
} else{
|
|
|
uni.showToast({
|
|
|
- title: res.msg,
|
|
|
+ title: data.msg,
|
|
|
icon: 'none'
|
|
|
})
|
|
|
- reject(res)
|
|
|
+ reject(data)
|
|
|
}
|
|
|
-
|
|
|
}).catch(err => {
|
|
|
reject(err)
|
|
|
}).finally(() => {
|
|
|
- uni.hideLoading()
|
|
|
+ opt.showLoading && uni.hideLoading()
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
@@ -44,8 +49,17 @@ const methods = ['GET', 'POST', 'PUT', 'DELETE']
|
|
|
methods.forEach(method => {
|
|
|
http[method.toLowerCase()] = (url, data, options) => {
|
|
|
const loadingTitle = method === 'GET' ? '加载中...' : '提交中...'
|
|
|
- const opt = Object.assign({}, config.httpDefaultOption, { loadingTitle }, options)
|
|
|
-
|
|
|
+ const opt = Object.assign(
|
|
|
+ {
|
|
|
+ loadingTitle,
|
|
|
+ header: {
|
|
|
+ 'content-type': 'application/x-www-form-urlencoded'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ config.httpDefaultOption,
|
|
|
+ options
|
|
|
+ )
|
|
|
+ console.log(opt);
|
|
|
if (opt.isAuth) {
|
|
|
if (!store.state.token) {
|
|
|
// uni.navigateTo({
|
|
|
@@ -57,11 +71,7 @@ methods.forEach(method => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- opt.showLoading && uni.showLoading({
|
|
|
- title: opt.loadingTitle
|
|
|
- })
|
|
|
-
|
|
|
- return request(method, url, data)
|
|
|
+ return request(method, url, data, opt)
|
|
|
}
|
|
|
})
|
|
|
|