| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import ENV_CONFIG from "@/.env.js";
- import { silentLogin } from "./auth";
- // api前缀
- export const HTTP_URL_DEV_PREFIX = "/api";
- export const HTTP_URL_PROD_PREFIX = "/api";
- let envVersion = "";
- let info = uni.getAccountInfoSync();
- // 微信小程序:获取环境版本(开发版、体验版、正式版)
- envVersion = info.miniProgram.envVersion;
- // #ifdef MP-ALIPAY
- // 支付宝小程序:使用 my.getRunMode 获取环境
- my.getRunMode && my.getRunMode({
- success: (res) => {
- envVersion = res.runMode;
- console.log(envVersion, "envVersion");
- },
- });
- // #endif
- const env = ENV_CONFIG[envVersion == "release" ? "production" : "development"];
- // 基于uview-ui的http配置
- export const UVIEWUI_HTTP_CONFIG = {
- // 地址
- baseUrl: env.apiUrl + env.apiUrlPrefix,
- // 请求方式
- method: "post",
- // 参数类型
- dataType: "json",
- // 是否显示请求中的loading
- showLoading: true,
- // 请求loading中的文字提示
- loadingText: "请求中...",
- // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
- loadingTime: 800,
- // 是否在拦截器中返回服务端的原始数据
- originalData: true,
- // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
- loadingMask: true,
- // header:{
- // token:uni.getStorageSync('token')
- // },
- };
- // 此处配置请求拦截器
- export const httpRequest = (config) => {
- const token = uni.getStorageSync("token");
- config.header.Authorization = token ? `Bearer ${token}` : "";
- return config;
- };
- // 此处配置响应拦截器
- export const httpResponse = (res) => {
- return new Promise(function (resolve, reject) {
- if (res.statusCode == 200) {
- if (res.data.code !== 200) {
- uni.showToast({
- icon: "none",
- title: res.data.msg || "服务器异常,请联系客服",
- });
- return resolve(res.data);
- }
- if (res.data.code == 401) {
- console.log("接口未登录>>>>4011",res.data.code);
- // 尝试静默登录
- silentLogin()
- .then((data) => {
- // 登录成功,重新发起之前失败的请求
- uni.$u.http.request(res.config).then(resolve).catch(reject);
- })
- .catch(() => {
- // 登录失败,跳转首页
- uni.switchTab({
- url: "/pages/home/index",
- });
- reject(res.data);
- });
- return resolve(res.data);
- }
- return resolve(res.data);
- }
- if (res.statusCode == 401) {
- console.log("接口未登录>>>>401",res.statusCode);
- silentLogin()
- .then((data) => {
- // 登录成功,重新发起之前失败的请求
- uni.$u.http.request(res.config).then(resolve).catch(reject);
- })
- .catch(() => {
- // 登录失败,跳转首页
- uni.switchTab({
- url: "/pages/home/index",
- });
- reject(res.data);
- });
- return resolve(res.data);
- }
- if (res.statusCode == 500) {
- console.log("服务器异常>>>>500", res.config);
- uni.showToast({
- title: "服务器异常,请联系客服!",
- icon: "none",
- duration: 3000,
- });
- return resolve(res.data);
- }
- return resolve(res.data);
- });
- };
- // http安装方法
- export const installHttpConfig = (Vue, vm) => {
- Vue.prototype.$u.http.setConfig(UVIEWUI_HTTP_CONFIG);
- Vue.prototype.$u.http.interceptor.request = httpRequest;
- Vue.prototype.$u.http.interceptor.response = httpResponse;
- };
|