Selaa lähdekoodia

更换了微信扫码登录的方案

bding 3 kuukautta sitten
vanhempi
commit
31287ef6f9
2 muutettua tiedostoa jossa 29 lisäystä ja 6 poistoa
  1. 19 5
      src/Login2p/Login2pContext.tsx
  2. 10 1
      src/Login2p/Login2pWechatLanding.tsx

+ 19 - 5
src/Login2p/Login2pContext.tsx

@@ -54,6 +54,7 @@ const Login2pProvider: React.FC<ContextProviderProps> = ({ children }: ContextPr
     history.go(0)
   }
   const loginWechatLaunch = (redirectURI: string) => {
+    const windowFeatures = 'left=500,top3100,width=500,height=500'
     if (isWechat) {
       window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
         process.env.REACT_APP_WECHART_APPID_NEW
@@ -61,11 +62,24 @@ const Login2pProvider: React.FC<ContextProviderProps> = ({ children }: ContextPr
         redirectURI
       )}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
     } else {
-      window.location.href = `https://open.weixin.qq.com/connect/qrconnect?appid=${
-        process.env.REACT_APP_WECHAT_OPEN_APPID_NEW
-      }&redirect_uri=${encodeURIComponent(
-        redirectURI
-      )}&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect`
+      // 创建 BroadcastChannel 实例
+      const channel = new BroadcastChannel('myChannel')
+
+      // 监听广播通道的消息
+      channel.addEventListener('message', event => {
+        loginWechat(event.data.code, decodeURIComponent(event.data.next ? event.data.next : '/')).then(r =>
+          console.log(r)
+        )
+      })
+      window.open(
+        `https://open.weixin.qq.com/connect/qrconnect?appid=${
+          process.env.REACT_APP_WECHAT_OPEN_APPID_NEW
+        }&redirect_uri=${encodeURIComponent(
+          redirectURI
+        )}&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect`,
+        'mozillaWindow',
+        windowFeatures
+      )
     }
   }
   const loginWechat = async (code: string, next: string) =>

+ 10 - 1
src/Login2p/Login2pWechatLanding.tsx

@@ -11,8 +11,17 @@ const Login2pWechatLanding: React.FC = () => {
   console.log(next)
   const login2p = useLogin2p()
   React.useEffect(() => {
+    // 创建 BroadcastChannel 实例
+
+    const channel = new BroadcastChannel('myChannel')
+
     if (code) {
-      login2p.loginWechat(code, decodeURIComponent(next ? next : '/')).then(r => console.log(r))
+      // 向广播通道发送消息
+      channel.postMessage({ code, next })
+      // 关闭频道
+      channel.close()
+      window.close()
+      // login2p.loginWechat(code, decodeURIComponent(next ? next : '/')).then(r => console.log(r))
     }
   }, [])