1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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);
- http.interceptors.request.use((config) => { // 可使用async await 做异步操作
- // 假设有token值需要在头部需要携带
- let accessToken = uni.getStorageSync('accessToken');
- if (accessToken) {
- config.header['Blade-Auth'] = 'bearer ' + accessToken;
- }
- // 客户端认证参数
- 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) {
- if (response.data.code) {
- return Promise.reject(response);
- } else {
- return response.data;
- }
- }
- return response.data;
- }, (response) => {
- /* 对响应错误做点什么 (statusCode !== 200)*/
- if (response.data.code === 401) {
- uni.showModal({
- title: '提示',
- content: '登录状态失效,点击确定重新登录',
- showCancel: false,
- success: function(res) {
- if (res.confirm) {
- uni.reLaunch({
- url: '/pages/login/login'
- });
- }
- }
- });
- } 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 {
- uni.showToast({
- title: response.data.msg,
- icon: 'none'
- });
- }
- return Promise.reject(response.data)
- })
- export default http;
|