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');
|
//headers判断是否需要
|
if (!config.custom.need) {
|
// 客户端认证参数
|
config.header['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret);
|
}
|
if (config.custom.Authorization == true) {
|
console.log(config.custom, "==========")
|
config.header['Authorization'] =
|
"Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJBRE1JTiIsIlVzZXJJZCI6IjEiLCJzY29wZSI6WyJhbGwiXSwiVXNlclJlYWxOYW1lIjoi6LaF57qn566h55CG5ZGYIiwiVXNlclh6cWhkbSI6IjQyMDUiLCJleHAiOjI1MTk3NzIwMTAsImp0aSI6ImExNTcxYzk1LTBkNjMtNDkyYi1iOWEyLTE2ZTIzNTQ5ZTY1ZiIsImNsaWVudF9pZCI6InVzZXItc2VydmljZSJ9.ebNVZrw9LbhKaj2w6RR8b2wccQiDkhvBeq79SxxCK-yWiOlIFqBkotTN4TNJg8umcpyYvLILwvqXWRJhffEtgi25sX2y6MqLIWM4kMZ9d8ptdnSmTpBPhltSiQOM0KFa1kl5nSDCBwYOLn-pESJglam76cjpgZNoC88x3iNHacdiXDItY0rtY85HrQ26uyJu9UovKtmYmZRHsIbGMpDta5Q1p4vfaCIr-YUayDrCweZJiQDEEcOSpWJ7O7RMk3pRkX_4UmPHFzrOI2lMp1jQIhxnSTE7EVAz_4z8h8r46muSkpF54Ic4XSawHKqdSLHx8T05LB0MpvOzWMPS6c1uHA"
|
config.header["Content-Type"] = "application/json";
|
}
|
if (accessToken) {
|
config.header['Blade-Auth'] = 'bearer ' + accessToken;
|
config.header['roleName'] = uni.getStorageSync("activeRole").roleAlias || "";
|
let activeRole = uni.getStorageSync("activeRole")
|
if (activeRole.roleAlias === 'inhabitant') {
|
config.header['communityCode'] = uni.getStorageSync("siteInfo").neiCode || ""
|
} else {
|
config.header['communityCode'] = uni.getStorageSync("siteInfo").id || ""
|
}
|
}
|
if (config.params.tenantId) {
|
config.header['Tenant-Id'] = config.params.tenantId;
|
}
|
if (config.json === true) {
|
|
}
|
|
//数据中台请求头
|
config.header['apikey'] =
|
'F1DBECD719108635189480CF60E6553ADB3109616426BD537F25A430DFC613B491A025C4A51E77FD08C6E5B7CBE05917A461286E7B6D69F1AB1B14F946149D2065B0C675F8FEDF4B9B05C1496881BC5A'
|
// #ifndef H5
|
// let url = config.url
|
// if (process.env.NODE_ENV == 'development' && !url.startsWith("http")) {
|
// url = url.substring(url.indexOf('/api') + 4)
|
// config.url = url
|
// }
|
// #endif
|
|
// 额外参数
|
// 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)
|
})
|
|
// uni.showLoading({
|
// title:"加载中..."
|
// })
|
|
http.interceptors.response.use((response) => {
|
|
// 若有数据返回则通过
|
if (response.data.access_token || response.data.key) {
|
return response.data
|
}
|
// 服务端返回的状态码不等于200,则reject()
|
if (response.data.code) {
|
if (response.data.code !== 200) {
|
uni.showToast({
|
title: response.data.msg,
|
icon: 'none'
|
});
|
return Promise.reject(response);
|
}
|
} else {
|
if (response.data.error_code !== 200) {
|
uni.showToast({
|
title: response.data.msg,
|
icon: 'none'
|
});
|
return Promise.reject(response);
|
}
|
}
|
|
//数据中台返回的数据结果
|
if (response.statusCode) {
|
// uni.hideLoading()
|
if (response.statusCode >= 300) {
|
console.log(response.data.msg)
|
uni.showToast({
|
title: '系统繁忙,请稍后再试',
|
icon: 'none'
|
});
|
return Promise.reject(response);
|
}
|
}
|
return response.data;
|
}, (response) => {
|
/* 对响应错误做点什么 (statusCode !== 200)*/
|
// uni.showToast({
|
// title: response.data.msg,
|
// icon: 'none'
|
// });
|
if (response.statusCode == 401) {
|
const pages = getCurrentPages()
|
const currentPage = pages[pages.length - 1]
|
uni.setStorageSync("responIndex", 0)
|
uni.clearStorageSync()
|
// uni.redirectTo({
|
// url: `/pages/login/login-account?redirect=/${currentPage.route}`
|
// })
|
|
|
uni.showModal({
|
title: "提示",
|
content: "请登录",
|
// showCancel: false,
|
success: res => {
|
if (res.confirm) {
|
uni.redirectTo({
|
url: `/pages/login/login-account?redirect=/${currentPage.route}`
|
})
|
} else if (res.cancel) {
|
console.log('用户点击取消');
|
}
|
}
|
})
|
} else {
|
uni.showToast({
|
title: '系统繁忙,请稍后再试',
|
icon: 'none'
|
});
|
}
|
return Promise.reject(response)
|
})
|
export default http;
|