提交 a6d084b9 authored 作者: 钟是志's avatar 钟是志

增加依赖

上级 609ac13f
import { isJSON } from '@/H5Public/utils/authority';
export function getCurrentUser() {
const d = sessionStorage.getItem('currentUser');
if (d && isJSON(d)) {
return JSON.parse(d);
} else {
return {};
}
}
export function setCurrentUser(v){
if(v && typeof v === 'object'){
sessionStorage.setItem('currentUser', JSON.stringify(v));
}
}
// import { getCurrentUser, getToken } from '@/webPublic/one_stop_public/utils/token';
import Md5 from './md5';
import { getCurrentUser } from '@/H5Public/utils/2022new/currentUser';
import { getIsBei_Dian, getToken } from '@/H5Public/utils/authority';
import { getUrlParams } from '@/H5Public/utils/handleString';
const isTest = getUrlParams().test;
export function getHeaders(url = '') {
const token = getToken();
const currentInfo = getCurrentUser();
const timestamp = new Date().getTime() + window.differenceBetweenServerAndClientTime;
const secretString = token + currentInfo.xgUserId + timestamp;
const res = {
headers: {
Authorization: `bearer ${token}`,
},
};
return res;
if(getIsBei_Dian() && currentInfo?.userid){
res.headers.auth = Md5(secretString);
res.headers.timestamp = timestamp;
if (isTest) {
console.log(
{
secretString,
md5S: Md5(secretString),
timestamp,
xgUserId: currentInfo.xgUserId,
typeString: currentInfo.typeString,
differenceBetweenServerAndClientTime: window.differenceBetweenServerAndClientTime,
},
);
}
}
return res;
}
import request from '@/H5Public/utils/request';
import { setCurrentUser } from '@/H5Public/utils/2022new/currentUser';
window.differenceBetweenServerAndClientTime = 0;
const config = window.specialImportantSystemConfig || {};
const getTime = () => {
return request(`${config.mockServer}/${config?.gateWayUrl?.zydxg}/InstructorConfigApi/getCurrentDate`, {
method: 'POST',
body: {},
});
};
export async function queryCurrent() {
let t = await getTime();
if (t) {
window.differenceBetweenServerAndClientTime = t - new Date().getTime(); // 服务器时间 与 客户端时间的差值
console.log(window.differenceBetweenServerAndClientTime);
}
return request(`${config.mockServer}/${config?.gateWayUrl?.zydxg}/UserApi/queryCurrent`, {
method: 'POST',
body: {},
}).then((res) => {
if (res) {
setCurrentUser(res);
}
});
}
差异被折叠。
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
* @param value cookie的值 * @param value cookie的值
* @param day cookie的过期时间 * @param day cookie的过期时间
*/ */
export const setCookie = function(name, value, day) { export const setCookie = function(name, value, day) {
if (day !== 0) { if (day !== 0) {
//当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除 //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
...@@ -14,6 +15,7 @@ export const setCookie = function(name, value, day) { ...@@ -14,6 +15,7 @@ export const setCookie = function(name, value, day) {
document.cookie = name + '=' + escape(value) + ';path=/'; document.cookie = name + '=' + escape(value) + ';path=/';
} }
}; };
/** /**
* 获取对应名称的cookie * 获取对应名称的cookie
* @param name cookie的名称 * @param name cookie的名称
......
...@@ -5,7 +5,9 @@ import router from 'umi/router'; ...@@ -5,7 +5,9 @@ import router from 'umi/router';
import { routerRedux } from 'dva/router'; import { routerRedux } from 'dva/router';
import store from 'dva'; import store from 'dva';
import FormdataWrapper from './object-to-formdata-custom'; import FormdataWrapper from './object-to-formdata-custom';
import {getToken, clearToken, getIsBei_Dian,} from './authority'; import { getToken, clearToken, getIsBei_Dian } from './authority';
import { getHeaders } from '@/H5Public/utils/2022new/getHeaders';
const codeMessage = { const codeMessage = {
200: '服务器成功返回请求的数据。', 200: '服务器成功返回请求的数据。',
201: '新建或修改数据成功。', 201: '新建或修改数据成功。',
...@@ -23,6 +25,7 @@ const codeMessage = { ...@@ -23,6 +25,7 @@ const codeMessage = {
503: '服务不可用,服务器暂时过载或维护。', 503: '服务不可用,服务器暂时过载或维护。',
504: '网关超时。', 504: '网关超时。',
}; };
function checkStatus(response) { function checkStatus(response) {
if (response.status >= 200 && response.status < 300) { if (response.status >= 200 && response.status < 300) {
...@@ -43,7 +46,7 @@ function checkStatus(response) { ...@@ -43,7 +46,7 @@ function checkStatus(response) {
* @param {object} [options] The options we want to pass to "fetch" * @param {object} [options] The options we want to pass to "fetch"
* @return {object} An object containing either "data" or "err" * @return {object} An object containing either "data" or "err"
*/ */
export default function request(url, options, config = {enableLoading: true}) { export default function request(url, options, config = { enableLoading: true }) {
const { errorHandle } = config; // 错误弹窗 回调 const { errorHandle } = config; // 错误弹窗 回调
const defaultOptions = { const defaultOptions = {
credentials: getIsBei_Dian() ? 'include' : 'omit', credentials: getIsBei_Dian() ? 'include' : 'omit',
...@@ -54,9 +57,8 @@ export default function request(url, options, config = {enableLoading: true}) { ...@@ -54,9 +57,8 @@ export default function request(url, options, config = {enableLoading: true}) {
if (getToken() && (url.indexOf('/third/token') <= -1 if (getToken() && (url.indexOf('/third/token') <= -1
&& url.indexOf('wxConfigApi/getConfig') <= -1 && url.indexOf('wxConfigApi/getConfig') <= -1
&& url.indexOf('wxConfigApi/auth') <= -1 && url.indexOf('wxConfigApi/auth') <= -1
)) { ) && !getIsBei_Dian()) {
console.log(); url = url + '?token=' + getToken();
url = url + "?token=" + getToken()
} }
if (newOptions.method === 'POST' || newOptions.method === 'PUT') { if (newOptions.method === 'POST' || newOptions.method === 'PUT') {
...@@ -66,6 +68,7 @@ export default function request(url, options, config = {enableLoading: true}) { ...@@ -66,6 +68,7 @@ export default function request(url, options, config = {enableLoading: true}) {
Accept: 'application/json;charset=UTF-8', Accept: 'application/json;charset=UTF-8',
/* 'X-Requested-With':'XMLHttpRequest', */ /* 'X-Requested-With':'XMLHttpRequest', */
...getHeaders().headers,
...newOptions.headers, ...newOptions.headers,
}; };
newOptions.body = FormdataWrapper(newOptions.body); newOptions.body = FormdataWrapper(newOptions.body);
...@@ -74,6 +77,7 @@ export default function request(url, options, config = {enableLoading: true}) { ...@@ -74,6 +77,7 @@ export default function request(url, options, config = {enableLoading: true}) {
newOptions.headers = { newOptions.headers = {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'multipart/form-data', 'Content-Type': 'multipart/form-data',
...getHeaders().headers,
...newOptions.headers, ...newOptions.headers,
}; };
} }
...@@ -96,22 +100,22 @@ export default function request(url, options, config = {enableLoading: true}) { ...@@ -96,22 +100,22 @@ export default function request(url, options, config = {enableLoading: true}) {
.then(response => { .then(response => {
if (typeof response === 'string') { if (typeof response === 'string') {
let xxx = ''; let xxx = '';
try{ try {
xxx = JSON.parse(response); xxx = JSON.parse(response);
if (xxx.errMsg) { if (xxx.errMsg) {
Toast.hide(); Toast.hide();
Modal.alert('', `${xxx.errMsg}`, [ Modal.alert('', `${xxx.errMsg}`, [
{ text: '知道了', onPress: errorHandle ? ()=>errorHandle(xxx) : () => console.log('ok') }, { text: '知道了', onPress: errorHandle ? () => errorHandle(xxx) : () => console.log('ok') },
]); ]);
return; return;
} }
}catch(e){ } catch (e) {
} }
}else if(response && (response.errMsg || response.errmsg)){ } else if (response && (response.errMsg || response.errmsg)) {
Toast.hide(); Toast.hide();
Modal.alert('', `${response.errMsg || response.errmsg}`, [ Modal.alert('', `${response.errMsg || response.errmsg}`, [
{ text: '知道了', onPress: errorHandle ? ()=>errorHandle(response) : () => console.log('ok') }, { text: '知道了', onPress: errorHandle ? () => errorHandle(response) : () => console.log('ok') },
]); ]);
return; return;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论