import { message } from 'antd'; import { getEnv } from '@ywfe/utils' import { useAuthResult } from '../index'; import { useGetAPI11940, usePostAPI45655, usePostAPI45662, usePostAPI49141, } from '../../generated/api'; interface IenvProps { dev: string; test: string; test2: string; pre: string; prod: string; } const cbUrls: IenvProps = { dev: 'http://dianhu.fed.ywwl.com/jump.html', test: 'http://dianhu.fed.ywwl.com/jump.html', test2: 'http://dianhu.fed.ywwl.com/jump.html', pre: 'https://cdn.ywwl.com/bps/ywyun/jump.html', prod: 'https://cdn.ywwl.com/bps/ywyun/jump.html', }; const cWxAppids: IenvProps = { dev: 'wwe2ddee10b7984df5', test: 'wwe2ddee10b7984df5', test2: 'wwe2ddee10b7984df5', pre: 'wwe2ddee10b7984df5', prod: 'wwe2ddee10b7984df5', }; export const useAuthAction = () => { const { fetchAPI11940Data } = useGetAPI11940(); const { fetchAPI45655Data } = usePostAPI45655(); const { fetchAPI45662Data } = usePostAPI45662(); const { fetchAPI49141Data } = usePostAPI49141(); const { needReset, resultSuccess, resultFailed, pageLocation } = useAuthResult(); // 校验地址是否含有微信code自动登录 const GetQueryString = (name: string) => { const url = window.location.href; const reg = new RegExp(`(\\?|&)${name}=([^&]*)(&|$)`, 'i'); const r = url.match(reg); if (r !== null) { return unescape(r[2]); } return ''; }; const fetchCodeLogin = async (code: string, type: string, grayPlatform?: string) => { const key = 'updatable'; message.loading({ content: '正在登录中,请稍等...', key }); const resData = await fetchAPI45662Data({ platformCode: localStorage.getItem('platformCode') || 'YW_YUN', thirdCode: code, thirdPlatform: type, grayPlatform, }); if (resData) { if (resData && resData?.userToken) { message.success({ content: '登录成功!', key, duration: 2 }); const bReset = needReset(resData); bReset && resultSuccess(resData, pageLocation); } else { resultFailed('CODE'); } } else { resultFailed('CODE'); } }; const actionLogin = async (values: any): Promise => { let res = {} if (values.loginType === 'VERIFYCODE') { res = await fetchAPI49141Data({ ...values, }); } else { res = await fetchAPI45655Data({ ...values, }); } if (res) { return res || ({} as any); } else { return {}; } }; const actionLoginOut = async (): Promise => { const res = await fetchAPI11940Data(); return !!res; }; // 企业微信自动登录 const actionLoginAuto = (sourceUrl?: string) => { const isCWxTalk = /wxwork/i.test(navigator.userAgent); let env: 'dev' | 'test' | 'test2' | 'pre' | 'prod' = getEnv() as any; if (isCWxTalk) { const pageUrl = encodeURIComponent(sourceUrl || window.location.href); const url = encodeURIComponent(`${cbUrls[env]}?currentUrl=${pageUrl}`); window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${cWxAppids[env]}&redirect_uri=${url}&response_type=code&scope=snsapi_base&state=#wechat_redirect`; } }; const actionWXScanLogin = () => { }; const actionDDScanLogin = () => { }; // 检测微信code自动登录 const actionCodeLogin = () => { const grayPlatform = GetQueryString('grayPlatform') || localStorage.getItem('grayPlatform') || ''; if (GetQueryString('code') && GetQueryString('state')) { fetchCodeLogin(GetQueryString('code'), 'DINGDING', grayPlatform); } else if (GetQueryString('code')) { fetchCodeLogin(GetQueryString('code'), 'WX', grayPlatform); } }; return { actionLogin, actionLoginOut, actionLoginAuto, actionWXScanLogin, actionDDScanLogin, actionCodeLogin, }; };