| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import Request from '@/common/luch-request/index.js'
- import {
- ACCESS_TOKEN
- } from '@/common/util/constants.js'
- import configService from './config.service.js'
- import tip from '@/common/util/tip.js';
- import store from '@/store/index.js';
- let apiUrl = configService.apiUrl;
- const getTokenStorage = () => {
- let token = ''
- try {
- token = uni.getStorageSync(ACCESS_TOKEN)
- } catch (e) {
- //TODO handle the exception
- }
- return token
- }
- const http = new Request()
- http.setConfig((config) => {
- /* 设置全局配置 */
- config.baseUrl = apiUrl /* 根域名不同 */
- config.header = {
- ...config.header
- }
- return config
- })
- /**
- * 自定义验证器,如果返回true 则进入响应拦截器的响应成功函数(resolve),否则进入响应拦截器的响应错误函数(reject)
- * @param { Number } statusCode - 请求响应体statusCode(只读)
- * @return { Boolean } 如果为true,则 resolve, 否则 reject
- */
- // 有默认,非必写
- http.validateStatus = (statusCode) => {
- return statusCode === 200
- }
- http.interceptor.request((config, cancel) => {
- /* 请求之前拦截器 */
- //判断是否请求白名单,设置带不带token
- let url = config.url;
- if (url == "/ibps/oauth2/v3/user/login/apply" ||
- url == "/ibps/oauth2/v3/authorize/apply" ||
- url == "/ibps/oauth2/v3/authentication/apply" ||
- url.substring(0, 27) == "ibps/oauth2/v3/user/captcha"
- ) {
- return config;
- }
- let token = getTokenStorage();
- config.header = {
- ...config.header,
- 'X-Authorization-access_token': token,
- 'X-Authorization-systemid': ''
- }
- if (!token) { // 如果token不存在,调用cancel 会取消本次请求,但是该函数的catch() 仍会执行
- cancel('token 不存在') // 接收一个参数,会传给catch((err) => {}) err.errMsg === 'token 不存在'
- }
- return config
- })
- // 必须使用异步函数,注意
- http.interceptor.response(async (response) => {
- /* 请求之后拦截器 */
- /* if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
- return Promise.reject(response)
- } */
- //console.log(response.data.state)
- if (response.data.state == 50002) { // 服务端返回的状态码不等于200,则reject()
- tip.alert('服务不可用')
- return Promise.reject(response)
- }
- if (response.data.state == 6020301) { // 服务端返回的状态码不等于200,则reject()
- let timeout = setTimeout(tip.alert('登录已过期'), 1000);
- store.dispatch('Logout').then(() => {
- clearTimeout(timeout)
- window.location.reload()
- })
- return Promise.reject(response)
- }
- /* if (response.data.state !== 6020108) { // 服务端返回的状态码不等于200,则reject()
- tip.alert('用户被禁用')
- return Promise.reject(response)
- } */
- if (response.data.state !== 200) { // 服务端返回的状态码不等于200,则reject()
- return Promise.reject(response)
- }
- return response
- }, (response) => { //只有错误的响应才会到这里
- // 请求错误做点什么
- let index = JSON.stringify(response).toString().indexOf("request:fail");
- if (index == 11) {
- tip.alert("网络错误!")
- return;
- }
- if (response) {
- let data = response.data;
- const token = uni.getStorageSync(ACCESS_TOKEN)
- switch (data.state) {
- case 50002:
- tip.error('服务不可用');
- break
- case 6020301:
- let timeout = setTimeout(tip.alert('登录已过期'), 1000);
- store.dispatch('Logout').then(() => {
- clearTimeout(timeout)
- window.location.reload()
- })
- break
- case 404:
- break
- case 504:
- break
- case 401:
- if (token) {
- /* store.dispatch('Logout').then(() => {
- setTimeout(() => {
- window.location.reload()
- }, 1500)
- }) */
- }
- break
- default:
- tip.error({
- duration: 0,
- forbidClick: true,
- message: data.cause
- });
- break
- }
- }
- return response
- })
- export {
- http
- }
|