暂存code
This commit is contained in:
+34
-15
@@ -1,27 +1,30 @@
|
||||
import {getStorageFun,TOKEN_NAME} from './auth.js';
|
||||
import qs from "qs";
|
||||
import { BASE_URL } from '@/utils/config.js'
|
||||
import { BASE_URL,Authorization } from '@/utils/config.js'
|
||||
|
||||
// utils/http.js (修改后的版本)
|
||||
// const baseUrl = 'http://192.168.0.108:8087';
|
||||
|
||||
function request(options) {
|
||||
const { url, method = 'GET', data = {}, headers = {} } = options;
|
||||
const { url, method = 'GET', data = {}, headers = {},isShowLoading=true } = options;
|
||||
|
||||
// const token = uni.getStorageSync(tokenKey); // 获取token, 根据实际情况调整获取方式
|
||||
const token = getStorageFun(TOKEN_NAME); // 获取token, 根据实际情况调整获取方式
|
||||
const defaultHeaders = {
|
||||
// 'HSM-AUTH': token ? token : '', // 如果存在token, 则添加到请求头中
|
||||
"Authorization":Authorization,
|
||||
'HSM-AUTH': token ? token : '', // 如果存在token, 则添加到请求头中
|
||||
'content-type': ['post','POST'].includes(method) ? 'application/x-www-form-urlencoded' : 'application/json',
|
||||
};
|
||||
if(token){
|
||||
defaultHeaders['HSM-AUTH'] = token;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
// 请求之前做一些事
|
||||
uni.showLoading({
|
||||
title: "加载中...",
|
||||
})
|
||||
if(isShowLoading){
|
||||
// 请求之前做一些事
|
||||
uni.showLoading({
|
||||
title: "加载中...",
|
||||
})
|
||||
}
|
||||
uni.request({
|
||||
url: BASE_URL + url,
|
||||
method,
|
||||
@@ -31,15 +34,30 @@ function request(options) {
|
||||
...headers, // 使用展开运算符合并默认头和自定义头
|
||||
},
|
||||
success: (res) => {
|
||||
uni.hideLoading();
|
||||
const data = res.data;
|
||||
if(isShowLoading){
|
||||
// 请求之前做一些事
|
||||
uni.hideLoading();
|
||||
}
|
||||
|
||||
let data = res.data;
|
||||
if(data && typeof(data) === "string"){
|
||||
data = JSON.parse(data);
|
||||
}
|
||||
if(data.status === 500){
|
||||
uni.showToast({
|
||||
title: "系统异常,请稍后再试",
|
||||
icon: 'none', // 可选 success/loading/none
|
||||
duration: 2000, // 持续时长,单位ms
|
||||
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (data.bizcode === 100) {
|
||||
resolve(data); // 成功时返回数据
|
||||
} else if (data.bizcode === 201) {
|
||||
if([201,200].includes(data.errcode)){
|
||||
uni.showToast({
|
||||
title: '登录失效,请从新登录',
|
||||
title: '登录失效,请重新登录',
|
||||
icon: 'none', // 可选 success/loading/none
|
||||
duration: 2000, // 持续时长,单位ms
|
||||
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
||||
@@ -68,20 +86,21 @@ function request(options) {
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: data.msg,
|
||||
icon: 'error', // 可选 success/loading/none
|
||||
icon: 'none', // 可选 success/loading/none
|
||||
duration: 2000, // 持续时长,单位ms
|
||||
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
||||
});
|
||||
reject(data); // 其他错误情况返回错误信息
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
uni.hideLoading();
|
||||
if(isShowLoading){
|
||||
// 请求之前做一些事
|
||||
uni.hideLoading();
|
||||
}
|
||||
uni.showToast({
|
||||
title: "服务器异常,稍后再试",
|
||||
icon: 'error', // 可选 success/loading/none
|
||||
icon: 'none', // 可选 success/loading/none
|
||||
duration: 2000, // 持续时长,单位ms
|
||||
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
||||
});
|
||||
// reject(error); // 请求失败时返回错误信息
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user