123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- // 配置请求根路径
- const BASE_URL = process.env.UNI_BASE_URL;
- import { showToast } from "@/utils/index";
- const request = (requesUrl, data, method = "GET") => {
- let url = `${BASE_URL}${requesUrl}`;
- return new Promise((resolve, reject) => {
- let header = {
- "Content-Type": "application/json; charset=UTF-8",
- "Authorization-Business": uni.getStorageSync("storage_key"),
- };
- uni.request({
- url: url,
- data: data,
- method: method,
- header: header,
- success: (res) => {
- const { data } = res;
- console.log(res);
- if (data.code !== "") {
- // 判断是不是 token 过期了
- const tokenerr = [20003, "20003", 20004, "20004", 20005, "20005"];
- if (tokenerr.includes(data.code)) {
- // 清除本地缓存所有的数据
- uni.clearStorageSync();
- setTimeout(() => {
- // 跳转回登陆页面
- uni.redirectTo({
- url: "/pages/login/index"
- });
- }, 1000);
- showToast("登陆超时,请重新登陆", "none", 1000);
- return;
- }
- // showToast(data.message || "系统突然出差了,请稍后再试","none",2000)
- return resolve(data);
- }
- return resolve(data);
- },
- fail: (res) => {
- reject(res);
- showToast("系统突然出差了,请稍后再试", "none", 2000);
- },
- });
- });
- };
- //不带token接口请求,
- const request1 = (requesUrl, data, method = "GET") => {
- let url = `${BASE_URL}${requesUrl}`;
- return new Promise((resolve, reject) => {
- let header = {
- "Content-Type": "application/json; charset=UTF-8",
- };
- uni.request({
- url: url,
- data: data,
- method: method,
- header: header,
- success: (res) => {
- const { data } = res;
- if (data.code !== "") {
- // 判断是不是 token 过期了
- const tokenerr = [20003, "20003", 20004, "20004", 20005, "20005"];
- if (tokenerr.includes(data.code)) {
- // 清除本地缓存所有的数据
- uni.clearStorageSync();
- setTimeout(() => {
- // 跳转回登陆页面
- uni.redirectTo({
- url: "/pages/login/index",
- });
- }, 1000);
- showToast("登陆超时,请重新登陆", "none", 1000);
- return;
- }
- // showToast(data.message || "系统突然出差了,请稍后再试","none",2000)
- return resolve(data);
- }
- return resolve(data);
- },
- fail: (res) => {
- reject(res);
- showToast("系统突然出差了,请稍后再试", "none", 2000);
- },
- });
- });
- };
- export { request, request1 };
|