| | |
| | | // 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) { |
| | |
| | | if (res.data && res.data.token) { |
| | | uni.setStorageSync('Token', res.data.token) |
| | | } |
| | | |
| | | |
| | | |
| | | // 401 自动跳登录 |
| | | if (res.statusCode === 401) { |
| | |
| | | uni.showModal({ |
| | | title: '温馨提示', |
| | | content: res.data.Message, |
| | | showCancel:false, |
| | | showCancel: false, |
| | | success: () => { |
| | | uni.reLaunch({ |
| | | url: '/pages/index/login' |
| | |
| | | } |
| | | }) |
| | | } |
| | | // 403 返回上一个页面 |
| | | // 403 返回上一个页面 |
| | | if (res.statusCode === 403) { |
| | | uni.showModal({ |
| | | title: '温馨提示', |
| | | content: res.data.Message, |
| | | showCancel:false, |
| | | showCancel: false, |
| | | success: () => { |
| | | uni.navigateBack() |
| | | } |