Browse Source

2023年8月4日09:14:07

yxb
wq 1 year ago
parent
commit
3d1d943489
1 changed files with 33 additions and 24 deletions
  1. 33
    24
      utils/network/request.js

+ 33
- 24
utils/network/request.js View File

encryption encryption
} from "./encryption"; } from "./encryption";
import { import {
setItem,
getItem, getItem,
StorageKeys StorageKeys
} from "../storage"; } from "../storage";
import { import {
SM4Util SM4Util
} from '../util/sm4.js' } from '../util/sm4.js'
import {
updateToken
} from "@/utils/network/api";
const s4 = new SM4Util() const s4 = new SM4Util()


let toole = 0;

/* 刷新token */ /* 刷新token */
function updateToken() {
function updateGetToken() {
const options = { const options = {
type: 2, type: 2,
data: { data: {
openId: getItem("openId"), openId: getItem("openId"),
accessToken: getItem("accessToken")
accessToken: getItem(StorageKeys.Token)
}, },
method: "POST", method: "POST",
showLoading: false, showLoading: false,
//刷新token //刷新token
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const res = await request(updateToken, options); const res = await request(updateToken, options);
const data = stringToJson(res.bizContent);
const data = JSON.parse(res.bizContent);
console.log("data", data) console.log("data", data)
setItem('accessToken', data.accessToken); setItem('accessToken', data.accessToken);
setItem(StorageKeys.Token, data.accessToken);
setItem('openId', data.openId); setItem('openId', data.openId);
resolve(data); resolve(data);
}).catch((error) => { }).catch((error) => {
} }


//请求 //请求
export function request(code, options = {}) {
export function request(code, options = {}, start = false) {
//公参 //公参
const Common = { const Common = {
agentId: "52010106004", agentId: "52010106004",
//Url 地址 /api/interfaceMidGroundIn //Url 地址 /api/interfaceMidGroundIn
// options.url = envs[process.env.NODE_ENV].baseUrl + '/api/interfaceMidGroundIn' // options.url = envs[process.env.NODE_ENV].baseUrl + '/api/interfaceMidGroundIn'
// options.url = envs[process.env.NODE_ENV].baseUrl + '/ifzt/api/interfaceMidGroundIn' // options.url = envs[process.env.NODE_ENV].baseUrl + '/ifzt/api/interfaceMidGroundIn'

options.url = envs[process.env.NODE_ENV].baseUrl + '/ifzt/api/interfaceMidGroundIn' options.url = envs[process.env.NODE_ENV].baseUrl + '/ifzt/api/interfaceMidGroundIn'
//默认json数据格式提交`
let contentType = 'application/x-www-form-urlencoded'

//判断baseUri是否为空 //判断baseUri是否为空
if (options.baseUrl) { if (options.baseUrl) {
options.url = options.baseUrl options.url = options.baseUrl
} }
//默认json数据格式提交`
let contentType = 'application/x-www-form-urlencoded'
//根据type判断数据传输格式 //根据type判断数据传输格式
if (options.type && options.type === 2) { if (options.type && options.type === 2) {
contentType = 'application/json;charset=UTF-8' contentType = 'application/json;charset=UTF-8'
} }
options.header = {
'content-type': contentType,
'Access-Token': getItem(StorageKeys.Token)
}
//默认POST提交 //默认POST提交
options.method = options.method ? options.method : 'POST' options.method = options.method ? options.method : 'POST'

//设置请求超时时间 //设置请求超时时间
options.timeout = 60000 options.timeout = 60000
//判断code不为空
if (code) {
options.data = encryption(code, Object.assign(Common, {
...options.data,
'appId': appId
}), 2);
options.header = {
'content-type': contentType,
'Access-Token': getItem(StorageKeys.Token)
}
console.log('Token', getItem(StorageKeys.Token))
if (!start) {
//判断code不为空
if (code) {
options.data = encryption(code, Object.assign(Common, {
...options.data,
'appId': appId
}), 2);
}
} }
//是否显示加载中 //是否显示加载中
if (options.showLoading) { if (options.showLoading) {
} }
} }
}); });
} else if (res.data.statusCode == 34567) {
toole++
if (toole > 2) {
updateToken(result.code).then((data) => {
} else if (res.data.statusCode == 99999) {
if (!start) {
updateGetToken().then((data) => {
console.log("token刷新", data); console.log("token刷新", data);
request(code, options)
});
request(code, options, true)
})
} else { } else {
uni.showModal({ uni.showModal({
title: '提示', title: '提示',

Loading…
Cancel
Save