| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import request from './request.js' // 文档地址:https://github.com/jerry-9527/uni_request
- import config from '@/config/config.js'
- // import store from '@/store'
- import cache from '@/utils/cache'
- import {
- LOGIN_TOKEN,
- CLIENT_ID
- } from '@/config/cache'
- // 创建request实例
- const service = request({
- baseURL: config.baseUrl,
- timeout: 5000, // 请求超时,
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Accept': 'application/json'
- }
- });
- // 请求拦截器
- service.interceptors.request.use(async (config, ...args) => {
- console.log(config);
- console.log('ddd',cache.get(LOGIN_TOKEN));
- // 判断token是否存在
- let token = cache.get(LOGIN_TOKEN) || '';
- // token为空已过期 刷新token
- if (token) {
- config.body.token = token;
- }
- return config
- })
- // response interceptor
- service.interceptors.response.use((response, ...args) => { // 响应拦截器(可以设置多个, 同时可以也可以使用异步方法)
- // 状态403token无效 清除token
- if (response.data.errorCode == 403) {
- // cache.set(LOGIN_TOKEN, '');
- }
- if (response.data.message) {
- uni.showToast({
- title: response.data.message,
- duration: 1500,
- mask: true,
- icon: 'none'
- });
- }
- return response
- })
- export default service
|