Files
hnxdcount-uniapp/request/index.js
PC-202306242200\Administrator dac42e3b0c first commit
2026-03-28 23:09:02 +08:00

99 lines
2.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
// }