| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- import {
- clientId,
- clientSecret
- } from '@/common/setting'
- import {
- options
- } from '@/http/config.js';
- import {
- Base64
- } from '@/utils/base64.js';
- import Request from '@/utils/luch-request/index.js';
- const http = new Request(options);
- let isTokenExpired = false;
- // 检查网络状态函数
- const checkNetworkStatus = () => {
- return new Promise((resolve, reject) => {
- uni.getNetworkType({
- success: (res) => {
- console.log(res)
- if (res.networkType === 'none') {
- reject(new Error('网络连接已断开'));
- } else {
- resolve(true);
- }
- },
- fail: () => {
- reject(new Error('网络状态检测失败'));
- }
- });
- });
- };
- http.interceptors.request.use(async (config) => { // 可使用async await 做异步操作
- try {
- await checkNetworkStatus();
- } catch (error) {
- uni.showToast({
- title: error.message,
- icon: 'none',
- duration: 3000
- });
- return Promise.reject(config);
- }
- // 假设有token值需要在头部需要携带
- let accessToken = uni.getStorageSync('accessToken');
- if (accessToken) {
- config.header['Blade-Auth'] = 'bearer ' + accessToken;
- } else {
- config.header['tenant-id'] = uni.getStorageSync('tenantId')
- }
- // 客户端认证参数
- config.header['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret);
- // 额外参数
- // config.data = config.data || {};
- // config.data.pf = uni.getSystemInfoSync().platform;
- // config.data.sys = uni.getSystemInfoSync().system;
- // 演示custom 用处
- // if (config.custom.auth) {
- // config.header.token = 'token'
- // }
- // if (config.custom.loading) {
- // uni.showLoading()
- // }
- /**
- /* 演示
- if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
- return Promise.reject(config)
- }
- **/
- return config
- }, config => { // 可使用async await 做异步操作
- return Promise.reject(config)
- })
- http.interceptors.response.use((response) => {
- // 若有数据返回则通过
- if (response.data.access_token || response.data.key) {
- return response.data
- }
- // 服务端返回的状态码不等于200,则reject()
- if (response.data.code !== 200) {
- console.log(response.data.code)
- if (response.data.code === 400) {
- uni.showToast({
- title: response.data.msg,
- icon: 'none'
- });
- } else if (response.data.code === 403) {
- uni.showModal({
- title: '提示',
- content: response.data.msg,
- showCancel: false,
- success: function(res) {
- if (res.confirm) {
- uni.clearStorageSync();
- uni.navigateTo()
- uni.reLaunch({
- url: '/pages/login/index'
- });
- }
- }
- });
- } else if (response.data.code) {
- return Promise.reject(response);
- } else {
- return response.data;
- }
- }
- return response.data;
- }, (response) => {
- if (response.statusCode === 0 || !response.statusCode) {
- uni.showToast({
- title: '网络连接中断,请检查网络设置',
- icon: 'none',
- duration: 3000
- });
- return Promise.reject(response);
- }
- /* 对响应错误做点什么 (statusCode !== 200)*/
- if (response.data.code === 401&& !isTokenExpired) {
- isTokenExpired = true;
- uni.showModal({
- title: '提示',
- content: '登录状态失效,点击确定重新登录',
- showCancel: false,
- success: function(res) {
- if (res.confirm) {
- isTokenExpired = false;
- uni.reLaunch({
- url: '/pages/login/index'
- });
- }
- }
- });
- } else if (response.data.error === "unauthorized") {
- if (response.data.error_description) {
- uni.showToast({
- title: response.data.error_description,
- icon: 'none'
- });
- } else {
- uni.showToast({
- title: response.data.error,
- icon: 'none'
- });
- }
- } else if (response.data.error == "invalid_grant") {
- uni.showToast({
- title: response.data.error_description,
- icon: 'none'
- });
- } else if (response.data.error == "access_denied") {
- uni.showToast({
- title: response.data.error_description,
- icon: 'none'
- });
- } else {
- uni.showToast({
- title: response.data.msg,
- icon: 'none'
- });
- }
- return Promise.reject(response.data)
- })
- export default http;
|