You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
1.9 KiB
JavaScript
93 lines
1.9 KiB
JavaScript
import _env from "../env";
|
|
import {
|
|
getOrigin
|
|
} from "./app";
|
|
import utils from "./utils";
|
|
|
|
let extConfig = {};
|
|
|
|
|
|
|
|
|
|
console.log(extConfig, 'extConfig')
|
|
const env = {
|
|
..._env,
|
|
...extConfig,
|
|
new_login: 1
|
|
}
|
|
|
|
/**
|
|
* 请求基类
|
|
* @param {Object} options 请求参数 { url: String 路径, [data: Object 请求携带的数据], [method: String 请求方式] }
|
|
*/
|
|
export function request(options) {
|
|
const store_id = uni.getStorageSync("storeId") || env.store_id
|
|
options.header = {
|
|
Authorization: `Bearer ${uni.getStorageSync("token_" + store_id)}`,
|
|
}
|
|
|
|
if (options.externalRequest) {
|
|
|
|
} else {
|
|
options.noclient ? (options.url = env.host + options.url) : (options.url = env.host + "/client" + options.url);
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: options.url,
|
|
timeout: env.NETWORK_TIME_OUT,
|
|
// data: { ...options.data, store_id: env.store_id, mini_id: env.mini_id, origin: getOrigin() },
|
|
data: {
|
|
...env,
|
|
...options.data,
|
|
origin: getOrigin(),
|
|
store_id: uni.getStorageSync("storeId") || env.store_id
|
|
},
|
|
header: options.header,
|
|
method: options.method,
|
|
success: (res) => {
|
|
uni.hideLoading();
|
|
let {
|
|
statusCode
|
|
} = res;
|
|
|
|
if (res?.data?.code == -1) {
|
|
setTimeout(() => {
|
|
utils.loginOut();
|
|
}, 300)
|
|
} else {
|
|
switch (statusCode) {
|
|
case 200:
|
|
resolve(res.data);
|
|
break;
|
|
case 401:
|
|
// 401 过期
|
|
setTimeout(() => {
|
|
utils.loginOut();
|
|
}, 300)
|
|
break;
|
|
default:
|
|
console.log("请求失败(" + statusCode + ")")
|
|
reject(res)
|
|
uni.$u.toast(res?.data?.message || "出错了");
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
},
|
|
|
|
fail: (res) => {
|
|
console.log(res, 'reslogin')
|
|
// #ifdef MP-ALIPAY
|
|
if (res.statusCode == 401) {
|
|
setTimeout(() => {
|
|
utils.loginOut();
|
|
}, 300)
|
|
}
|
|
// #endif
|
|
reject(res.data.data);
|
|
},
|
|
});
|
|
});
|
|
} |