123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import {
- prodUrl,
- contentType,
- clientId,
- clientSecret
- } from '@/common/setting'
- import {setUuid} from '@/utils/uuid.js'
- 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 做异步操作
- //获取宿主App参数内容
- let szInfo = uni.getStorageSync('isSzApp');
- // 假设有token值需要在头部需要携带
- let accessToken = uni.getStorageSync('accessToken');
- if (accessToken) {
- config.header['Blade-Auth'] = 'bearer ' + accessToken;
- }
- if(szInfo){
- //判断网络环境,公安网添加请求头部
- if(!szInfo.extraData.internet){
- config.header['appCredential'] = szInfo.extraData.data.appCredential;
- config.header['userCredential'] =szInfo.extraData.data.userCredential;
- }else{
- //互联网输入用户名和密码
- config.data.username = szInfo.extraData.xwkjUserName
- config.data.password = szInfo.extraData.xwkjPassword
- }
- //小程序应用id
- config.data.appId = szInfo.extraData.appId
- //设置动态参数Url,
- config.baseURL = szInfo.extraData.data.resourceList[0].resourceAddress
- //拼接请求主体
- let dataParams ={
- messageId:setUuid(),
- verson:'1.0',
- parameter:{
- condition:{
- keyValueList:[{
- key :"params",
- relationOperator:'=',
- value:JSON.stringify(config.data)
- }],
- logicalOperate:'and'
- },
- dataObjId:szInfo.extraData.data.dataObjId,
- fields:"data",
- networkCode:szInfo.extraData.data.NetworkCode,
- page:{pageNo:1,pageSize:1},
- orderBy:[],
- regionalismCode:szInfo.extraData.data.resourceList[0].resourceRegionalismCode
- },
- }
- config.data = JSON.stringify(dataParams);
- }
-
- //encodeURIComponent
- // 安全请求header
- config.header['Blade-Requested-With'] = 'BladeHttpRequest';
- // 客户端认证参数
- config.header['Authorization'] = 'Basic c2FiZXI6c2FiZXJfc2VjcmV0'//'Basic ' + Base64.encode(clientId + ':' + clientSecret);
-
- // #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)
- }
- **/
- //console.log(config,'config')
- return config
- }, config => { // 可使用async await 做异步操作
- return Promise.reject(config)
- })
- http.interceptors.response.use((response) => {
- //console.log(response,'response')
- // 若有数据返回则通过
- if (response.statusCode==200) {
- return response.data
- }
- // 服务端返回的状态码不等于200,则reject()
- if (response.statusCode !== 200) {
- uni.showToast({
- title: response.data.msg,
- 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.redirectTo({
- url: `/pages/login/login-account?redirect=/${currentPage.route}`
- })
- }
-
- return Promise.reject(response)
- })
- export default http;
|