hejianhao
2025-02-24 08ac49dedf1161c00aa83616653774ac9d910366
request
1个文件已修改
2个文件已添加
202 ■■■■■ 已修改文件
H5/main.js 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
H5/store/index.js 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
H5/utils/request.js 175 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
H5/main.js
@@ -2,23 +2,27 @@
// #ifndef VUE3
import Vue from 'vue'
import store from '@/store/index.js'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
Vue.prototype.$store = store
import uView from '@/uni_modules/uview-ui'
App.mpType = 'app'
const app = new Vue({
  ...App
    ...App
})
app.$mount()
// #endif
Vue.use(uView)
// #ifdef VUE3
import { createSSRApp } from 'vue'
import {
    createSSRApp
} from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
    const app = createSSRApp(App)
    return {
        app
    }
}
// #endif
H5/store/index.js
New file
@@ -0,0 +1,11 @@
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
    state: {},
    mutations: {},
    actions: {}
})
export default store
H5/utils/request.js
New file
@@ -0,0 +1,175 @@
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';
import config from '@/config/index.js'
const baseURL = config.BASE_URL;
const waitingList = [] //等待队列
const excutingList = [] //执行队列
let showLogoutModal = true //是否显示冻结弹窗
function request(method, url, data, type, lodingFlag) {
    if (lodingFlag) {
        uni.showLoading({
            mask: true,
        })
    }
    return new Promise(function(resolve, reject) {
        let token = uni.getStorageSync('token')
        let header = {
            'content-type': type ? 'application/x-www-form-urlencoded;charset=UTF-8' : 'application/json',
            'Authorization': token,
            'lang': uni.getStorageSync('locale') === 'zh-Hans' ? 'zh_CN' : 'zh-tw',
        }
        const requestTask = uni.request({
            url: baseURL + url,
            method: method,
            data: data,
            header: header,
            success: (res) => {
                //判断状态码
                if (lodingFlag) {
                    uni.hideLoading()
                }
                if (res.data.code == 200) {
                    resolve(res.data);
                    return
                }
                if (res.data.code == 502) {
                    resolve(res.data);
                    uni.showToast({
                        title: res.data.msg,
                        duration: 2000,
                        icon: 'none',
                        mask: true,
                    })
                    return
                }
                if (res.data.code == 500 && url.includes('/applet/user/check')) {
                    resolve(res.data);
                    return
                }
                if (res.data.code == 1 || res.data.code == 500) {
                    uni.showToast({
                        title: res.data.msg || res.data.data || '服务器错误',
                        duration: 2000,
                        icon: 'none',
                        mask: true,
                    })
                    return
                }
                if (res.data.code == 401 || res.data.code == 510 || res.data.code == 504 || res.data
                    .code == 505) {
                    uni.removeStorageSync('token')
                    if (res.data.code == 504) {
                        uni.showToast({
                            title: res.data.msg,
                            duration: 2000,
                            icon: 'none',
                            mask: true,
                        })
                    } else if (res.data.code == 505) {
                        handleLogout('当前登录账号在其他设备登录')
                    } else {
                        handleLogout('登录失效,请重新登录')
                    }
                    return
                }
                if (res.data.code == 501) {
                    uni.showToast({
                        title: res.data.msg,
                        duration: 2000,
                        icon: 'none',
                        mask: true,
                    })
                    return
                }
                if (res.data.code == 506) {
                    resolve(res.data);
                    return
                }
                uni.showToast({
                    title: res.data.msg || '服务器错误',
                    duration: 2000,
                    icon: 'none',
                    mask: true,
                })
            },
            fail(err) {
                if (lodingFlag) {
                    uni.hideLoading()
                }
                reject(err)
            },
            complete: () => {
                const excutingIndex = excutingList.findIndex(item => item === requestTask)
                excutingList.splice(excutingIndex, 1)
                const [apiFn] = waitingList
                if (excutingList.length === 0 && typeof apiFn === 'function') {
                    apiFn()
                    waitingList.splice(0, 1)
                }
            },
        })
        excutingList.push(requestTask)
    })
}
// 被冻结跳转到登录页并取消后续请求
function handleLogout(str) {
    let routeStr = getCurrentPages()[getCurrentPages().length - 1].route
    if (excutingList.length >= 1) {
        showLogoutModal = true
    }
    if (showLogoutModal) {
        showLogoutModal = false
        uni.showModal({
            title: '提示',
            content: str,
            showCancel: false,
            success: function(res) {
                if (res.confirm) {
                    if (routeStr == 'pages/login/index') {
                        uni.reLaunch({
                            url: '/pages/login/index'
                        })
                        return
                    }
                    uni.reLaunch({
                        url: '/pages/login/index'
                    })
                }
            }
        });
        clearAllRequest()
    }
}
// 清除请求队列
function clearAllRequest() {
    if (excutingList.length > 0) {
        for (var i = 0; i < excutingList.length; i++) {
            const item = excutingList[i]
            if (item && item.abort) {
                item.abort()
            }
        }
        excutingList.length = 0
        waitingList.length = 0
    }
}
export default {
    get: (url, data, type = false, lodingFlag = true) => request(GET, url, data, type, lodingFlag),
    post: (url, data, type = false, lodingFlag = true) => request(POST, url, data, type, lodingFlag),
    put: (url, data, type = false, lodingFlag = true) => request(PUT, url, data, type, lodingFlag),
    FORM: (url, data, type = false, lodingFlag = true) => request(FORM, url, data, type, lodingFlag),
    delete: (url, data, type = false, lodingFlag = true) => request(DELETE, url, data, type, lodingFlag),
}