first commit
This commit is contained in:
99
request/index.js
Normal file
99
request/index.js
Normal file
@@ -0,0 +1,99 @@
|
||||
import store from '@/store/index.js';
|
||||
|
||||
|
||||
import {
|
||||
API_URL
|
||||
} from '@/config'
|
||||
import {
|
||||
toast
|
||||
} from '@/uni_modules/uview-plus'
|
||||
|
||||
export const http = (url, params, method, Headers) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let Authorization = store.state.token || ''
|
||||
let header = {
|
||||
Authorization,
|
||||
...Headers
|
||||
}
|
||||
uni.request({
|
||||
url: API_URL + url,
|
||||
data: params || {},
|
||||
method: method || 'POST',
|
||||
header: header,
|
||||
success: (res) => {
|
||||
const data = res.data
|
||||
console.log(res);
|
||||
if (data.code == 401 || res.statusCode == 401) {
|
||||
uni.clearStorageSync();
|
||||
store.commit('setToken', '');
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: `${Authorization ? '登录失效' : '你还未登录'},是否前往登录?`,
|
||||
success: function(res) {
|
||||
if (res.confirm) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
} else if (res.cancel) {
|
||||
console.log('用户点击取消');
|
||||
}
|
||||
}
|
||||
});
|
||||
reject(data)
|
||||
return
|
||||
}
|
||||
if (data.code == 10 && !data.success) {
|
||||
reject(data)
|
||||
return
|
||||
}
|
||||
if (data.code !== 1 && !data.success) { // 服务端返回的状态码不等于200,则reject()
|
||||
toast(data.msg)
|
||||
reject(data)
|
||||
return
|
||||
}
|
||||
resolve(data.data || null)
|
||||
},
|
||||
fail: (err) => {
|
||||
if (err.code == 401 || err.statusCode == 401) {
|
||||
uni.clearStorageSync();
|
||||
store.commit('setToken', '');
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: `${Authorization ? '登录失效' : '你还未登录'},是否前往登录?`,
|
||||
success: function(res) {
|
||||
if (res.confirm) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
} else if (res.cancel) {
|
||||
console.log('用户点击取消');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// import {requestInterceptors,responseInterceptors} from './interceptors.js'
|
||||
// // 引入luch-request
|
||||
// import { http } from '@/uni_modules/uview-plus'
|
||||
// // 初始化请求配置
|
||||
// const initRequest=(vm)=>{
|
||||
// http.setConfig((defaultConfig) => {
|
||||
// /* defaultConfig 为默认全局配置 */
|
||||
// defaultConfig.baseURL = config.baseUrl /* 根域名 */
|
||||
// return defaultConfig
|
||||
// })
|
||||
// requestInterceptors()
|
||||
// responseInterceptors()
|
||||
// }
|
||||
// export {
|
||||
// initRequest
|
||||
// }
|
||||
Reference in New Issue
Block a user