123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import {
- getToken
- } from '@/utils/auth'
- import {
- store
- } from '@/store/index.js'
- // import globalModal from "@/components/dialog-notification/dialog-notification.vue";
- // import baseURL from '@/api/base/path.js'
- function behindRequist({
- url,
- data,
- method = "GET"
- }) {
- let token = 'Bearer ' + getToken();
- let header = {
- Authorization: token
- }
- return new Promise((resolve, reject) => {
- const urlList = uni.getStorageSync('baseUrl')
- uni.request({
- url: JSON.parse(urlList).baseUrl + url,
- data,
- method,
- header,
- timeout: 600000,
- sslVerify: false,
- success: (res) => {
- if (res.statusCode === 200) {
- //请求成功
- resolve(res.data);
- } else if (res.statusCode === 401) {
- uni.showToast({
- icon: 'none',
- title: "未登录或登录状态已超时",
- duration: 1500
- });
- } else if (res.statusCode === 405) {
- uni.showToast({
- icon: 'none',
- title: "请求方法错误",
- duration: 1500
- });
- } else {
- uni.showToast({
- icon: 'none',
- title: "请求错误:" + res.statusCode,
- duration: 1500
- });
- }
- },
- fail: (err) => {
- uni.showToast({
- icon: 'none',
- title: err.errMsg,
- duration: 1500
- });
- reject(err);
- }
- })
- })
- }
- // 拦截请求的函数
- function request(options) {
- return preRequest().then(() => {
- // 前置请求成功,继续执行原请求
- return behindRequist(options);
- }).catch((error) => {
- // 前置请求失败,处理错误
- uni.showToast({
- icon: 'none',
- title: error.message || '前置请求失败',
- duration: 1500
- });
- return Promise.reject(error);
- });
- }
- // 前置请求函数
- function preRequest() {
- let token = 'Bearer ' + getToken();
- let header = {
- Authorization: token
- };
- return new Promise((resolve, reject) => {
- const urlList = uni.getStorageSync('baseUrl')
- console.log(urlList)
- uni.request({
- url: JSON.parse(urlList).baseUrl + '/business/notification/getHasNotification', // 前置请求的URL
- header,
- method: 'GET',
- success: (res) => {
- console.log(res)
- if (res.data.code == 200 && !store.isNotification) {
- uni.navigateTo({
- url: "/pages/notification/index",
- success: (resqust) => {
- // 通过eventChannel向被打开页面传送数据
- resqust.eventChannel.emit("notification", {
- data: res.data.data[0]
- })
- }
- })
- } else {
- // 前置请求成功,继续执行原请求
- resolve(true);
- }
- },
- fail: (err) => {
- // 前置请求失败,不执行原请求
- reject(err);
- }
- });
- });
- }
- export default {
- request
- }
|