dytyqx
6 天以前 4107148e5283129a52c2a1fe43dd16e6feae70ac
main.js
@@ -30,12 +30,21 @@
// uniapp 全局请求拦截器
uni.addInterceptor('request', {
    invoke(args) {
        // 获取到当前调用的页面
        const pages = getCurrentPages()
        const currentPage = pages[pages.length - 1]
        // 拿到页面定义的 modName
        const modName = currentPage?.$vm.HModName || ''
        // 请求头带上 token
        let token = uni.getStorageSync('Token')
        if (token) {
            // 先初始化,防止 undefined 报错
            args.header = args.header || {}
            args.header.Authorization = `Bearer ${token}`
            args.header["X-HModName"] = modName
        }
    },
    success(res) {
@@ -43,7 +52,7 @@
        if (res.data && res.data.token) {
            uni.setStorageSync('Token', res.data.token)
        }
        // 401 自动跳登录
        if (res.statusCode === 401) {
@@ -51,7 +60,7 @@
            uni.showModal({
                title: '温馨提示',
                content: res.data.Message,
                showCancel:false,
                showCancel: false,
                success: () => {
                    uni.reLaunch({
                        url: '/pages/index/login'
@@ -59,12 +68,12 @@
                }
            })
        }
         // 403 返回上一个页面
        // 403 返回上一个页面
        if (res.statusCode === 403) {
            uni.showModal({
                title: '温馨提示',
                content: res.data.Message,
                showCancel:false,
                showCancel: false,
                success: () => {
                    uni.navigateBack()
                }