123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- import * as db from "./db.js";
- import { baseUrl } from "./config.js";
- import { User } from "@/config/api.js";
- import store from "../store/index.js";
- const showError = (error) => {
- let errorMsg = "";
- switch (error.status) {
- case 400:
- errorMsg = "请求参数错误";
- break;
- case 401:
- errorMsg = "未授权,请登录";
- break;
- case 403:
- errorMsg = "跨域拒绝访问";
- break;
- case 404:
- errorMsg = `请求地址出错: ${error.config.url}`;
- break;
- case 408:
- errorMsg = "请求超时";
- break;
- case 500:
- errorMsg = "服务器内部错误";
- break;
- case 501:
- errorMsg = "服务未实现";
- break;
- case 502:
- errorMsg = "网关错误";
- break;
- case 503:
- errorMsg = "服务不可用";
- break;
- case 504:
- errorMsg = "网关超时";
- break;
- case 505:
- errorMsg = "HTTP版本不受支持";
- break;
- default:
- errorMsg = error.msg;
- break;
- }
- uni.showToast({
- title: errorMsg,
- icon: "none",
- duration: 1000,
- complete: function () {
- setTimeout(() => {
- uni.hideToast();
- }, 1000);
- },
- });
- };
- // get请求
- export const getHttp = (url, data, status = 1) => {
- // 获取用户token和用户信息
- let token = db.get("access_token");
- let authHeader = token || "";
- if (status) {
- store.commit("requestShowLoading");
- }
- return new Promise((resolve, reject) => {
- uni.request({
- url: baseUrl + url,
- data: data,
- header: {
- // 'Accept': 'application/json',
- // 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
- Authorization: authHeader,
- },
- method: "GET",
- success: (res) => {
- status && store.commit("requestHideLoading");
- // 408 token失效
- if (res.data.Ret === 408) {
- refreshToken(getHttp, url, data, resolve);
- return;
- }
- res.data.Ret === 403 &&
- uni.showModal({
- title: "",
- confirmColor: "#376cbb",
- content: res.data.Msg,
- showCancel: false,
- });
- resolve(res.data);
- },
- fail: (error) => {
- status && store.commit("requestHideLoading");
- if (error && error.response) {
- showError(error.response);
- }
- },
- complete: () => {
- setTimeout(() => {
- status && store.commit("requestHideLoading");
- }, 250);
- },
- });
- });
- };
- // post请求
- export const postHttp = (url, data, status = 1) => {
- // 获取用户token和用户信息
- let token = db.get("access_token");
- let authHeader = token || "";
- if (status) {
- store.commit("requestShowLoading");
- }
- return new Promise((resolve, reject) => {
- uni.request({
- url: baseUrl + url,
- data: data,
- header: {
- "Content-Type": "application/json",
- Authorization: authHeader,
- },
- method: "POST",
- success: (res) => {
- status && store.commit("requestHideLoading");
- // 408 token失效
- if (res.data.Ret === 408) {
- refreshToken(postHttp, url, data, resolve);
- return;
- }
- res.data.Ret === 403 &&
- uni.showModal({
- title: "",
- confirmColor: "#376cbb",
- content: res.data.Msg,
- showCancel: false,
- });
- resolve(res.data);
- },
- fail: (error) => {
- status && store.commit("requestHideLoading");
- if (error && error.response) {
- showError(error.response);
- }
- },
- complete: () => {
- setTimeout(() => {
- status && store.commit("requestHideLoading");
- }, 250);
- },
- });
- });
- };
- let requestList = []; //存放token失效时请求队列
- let isRefreshing = false; //是否正在刷新token
- const refreshToken = async (type, url, data, resolve) => {
- requestList.push(() => {
- resolve(type(url, data));
- });
- if (!isRefreshing) {
- isRefreshing = true;
- uni.login({
- success: async (result) => {
- const res = await User.wechatLog({ Code: result.code });
- if (res.Ret === 200) {
- let token = res.Data.Authorization;
- db.set("access_token", token);
- // 重新请求队列
- requestList.map((MT) => {
- MT();
- });
- requestList = [];
- isRefreshing = false;
- }
- },
- });
- }
- };
|