| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- /**
- * 判断是否在微信环境
- */
- export function isWechat() {
- const ua = window.navigator.userAgent.toLowerCase();
- // @ts-ignore
- return ua.match(/MicroMessenger/i) == 'micromessenger';
- }
- /**
- * 判断是否在企业微信环境
- */
- export function isEnterpriseWechat() {
- const ua = window.navigator.userAgent.toLowerCase();
- // @ts-ignore
- return isWechat() && ua.match(/wxwork/i) == 'wxwork';
- }
- /**
- * 重定向到企业微信授权页面
- * @param redirectUri 回调地址
- */
- export function redirectToWechatAuth(redirectUri: string) {
- const corpId = import.meta.env.VITE_WECHAT_CORPID;
- // const agentId = import.meta.env.VITE_WECHAT_AGENTID; // 非扫码登录场景通常不需要 agentid,由后端配置决定
- const state = 'STATE';
- const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${corpId}&redirect_uri=${encodeURIComponent(
- redirectUri
- )}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
- window.location.href = url;
- }
- /**
- * 从 URL 中获取 code
- */
- export function getUrlCode(): string | null {
- const url = window.location.href;
- const match = url.match(/[?&]code=([^&#]*)/);
- return match ? match[1] : null;
- }
|