import React, { useState, useEffect } from 'react'; import { random } from 'oak-domain/lib/utils/string'; import classNames from 'classnames'; import './index.less'; function WeChatLoginQrCode(props) { const { id = 'login_qr_container', appId, scope, redirectUri, state, style = '', href = "data:text/css;base64,LmltcG93ZXJCb3ggLnFyY29kZSB7d2lkdGg6IDIwMHB4O30KLmltcG93ZXJCb3ggLnRpdGxlIHtkaXNwbGF5OiBub25lO30KLmltcG93ZXJCb3ggLmluZm8ge3dpZHRoOiAyMDBweDt9Ci5zdGF0dXNfaWNvbiB7ZGlzcGxheTogbm9uZX0KLmltcG93ZXJCb3ggLnN0YXR1cyB7dGV4dC1hbGlnbjogY2VudGVyO30gCg==", dev = process.env.NODE_ENV === 'development', // 默认本地为true 发布时为false disabled = false, disableText, rootStyle, rootClassName, } = props; const [code, setCode] = useState(''); useEffect(() => { if (appId) { if (disabled) { return; } // 由于本地不能微信扫码测试 所以只能模拟 输入code使用weChatLogin if (dev) { setCode(random(6)); return; } loadScript(`${window.location.protocol}//res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js`, () => { const params = { id, appid: appId, scope, redirect_uri: redirectUri, style, href, state: state || '' }; // @ts-ignore new WxLogin(params); }); } }, [appId, disabled]); function loadScript(url, callback) { const script = document.createElement('script'); // @ts-ignore if (script.readyState) { // IE // @ts-ignore script.onreadystatechange = function () { if ( // @ts-ignore script.readyState === 'loaded' || // @ts-ignore script.readyState === 'complete') { // @ts-ignore script.onreadystatechange = null; callback(); } }; } else { // 其他浏览器 script.onload = function () { callback(); }; } script.type = 'text/javascript'; script.src = url; document.getElementsByTagName('head')[0].appendChild(script); } const prefixCls = 'oak'; const prefixCls2 = `${prefixCls}-loginQrCode`; let V; let DisableV; if (disabled) { DisableV = (