import { message } from 'antd'; import { useLocation } from '@ywfe/router'; // TODO 需要替换为uitls中的方法 function setCookie(name: string, value: any) { const Days = 30; const exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = `${name}=${escape(value)};expires=${exp.toUTCString()}`; } const getQuery = (name: string) => { const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i'); const values = reg.exec(window.location.search.substr(1)); if (values != null) { return unescape(values[2]); } return null; }; export const useAuthResult = () => { const { search } = useLocation(); const needReset = (res: any) => { if (res?.needReset) { message.info('请先修改密码后再尝试登录!'); initSlideState(); return false; } return true; }; const initSlideState = () => { // setSlideInfo({}); }; const resultSuccess = (res: any, fn: Function) => { if (res?.userToken) { if (typeof window !== 'undefined') { window.localStorage.setItem('token', res.userToken); window.localStorage.setItem('userName', res.userName); window.localStorage.setItem('userPhone', res.userPhone); window.localStorage.setItem('yun_userInfos', JSON.stringify(res)); if (res.breakDownStart && res.breakDownEnd) { const nNowTime = new Date().getTime(); const nStartTime = new Date(res.breakDownStart).getTime(); const nEndTime = new Date(res.breakDownEnd).getTime(); if (nNowTime >= nStartTime && nEndTime >= nNowTime) { setCookie('breakMessage', decodeURIComponent(res.breakMessage)); } } window.localStorage.setItem('isGray', res.isGray || null); } // 历史版本兼容 灰度写入 setCookie('yowantId', window.localStorage.getItem('isGray')); // 新版 版本灰度 标识写入 start window.localStorage.setItem('grayParams', res.grayParams || ''); if (res.grayParams) { const opt = JSON.parse(res.grayParams) if ('gray' in opt) { if ('grayTag' in opt.gray) { setCookie('yowantId', opt.gray.grayTag || null); } } } // 新版 版本灰度 标识写入 end typeof fn === 'function' && fn(); const isCWxTalk = /wxwork/i.test(navigator.userAgent); if (isCWxTalk) { // 遥望云H5 const url = `${window.location.protocol}//${window.location.host}`; // 判断是否调到固定页面 const redirectUrl = getQuery('redirectUrl') if (redirectUrl) { window.location.replace(decodeURIComponent(redirectUrl as any)) return } window.location.replace(`${url}/`) } return { status: true, msg: '登录成功', } } else { return { status: false, msg: '登录失败', } } }; const resultFailed = (type: string) => { if (type === 'NORMAL') { return { status: false, msg: '登录失败', } } else if (type === 'CODE') { setTimeout(() => { const url = window.location.href; const urlOpt = new URL(url) const targetUrl = urlOpt.searchParams.get('targetUrl'); const redirectUrl = urlOpt.searchParams.get('redirectUrl'); urlOpt.searchParams.delete('code') if (targetUrl) { const targetUrlOpt = new URL(targetUrl) targetUrlOpt.searchParams.delete('code') urlOpt.searchParams.set('targetUrl', targetUrlOpt.toString()) } if (redirectUrl) { const redirectUrlOpt = new URL(redirectUrl) redirectUrlOpt.searchParams.delete('code') urlOpt.searchParams.set('redirectUrl', redirectUrlOpt.toString()) } window.location.replace(urlOpt.toString()) }, 200) } return { status: false, msg: '登录失败', } }; const resultError = () => {}; const pageLocation = () => { const token = localStorage.getItem('token') || ''; let targetUrl = window.location.origin; // 支持三方登录 TODO:新旧切换 if (getQuery('redirectUrl')) { const url = getQuery('redirectUrl'); const hasToken = url ? url.includes('token') : false; if (url && url.split('?').length > 1) { window.location.replace(`${getQuery('redirectUrl')}${hasToken ? '' : `&token=${token}`}`) return; } window.location.replace(`${getQuery('redirectUrl')}${hasToken ? '' : `?token=${token}`}`) return; } // if (search.includes('contractNo=')) { // targetUrl = `${targetUrl}/supplyManage/DM27005074D931194412238924920431/contractManagefromUrl${search}`; if (search.includes('targetUrl=')) { const url = decodeURIComponent(getQuery('targetUrl') as string); targetUrl = url; } else if (search.includes('pathname=')) { const url = decodeURIComponent(getQuery('pathname') as string); targetUrl = url; } else { targetUrl = `${targetUrl}/`; } window.location.replace(targetUrl); }; return { needReset, pageLocation, resultSuccess, resultFailed, resultError, }; };