判断是否在微信客户端

This commit is contained in:
hackrobot
2024-07-18 18:31:20 +08:00
parent e3599c3afc
commit 0e0fed4ecc

View File

@@ -53,6 +53,8 @@ const Index = () => {
const [joinMeetup, setjoinMeetup] = useState(false); const [joinMeetup, setjoinMeetup] = useState(false);
const [isWeChat, setisWeChat] = useState(false);
const getOpenidFromUrl = (url) => { const getOpenidFromUrl = (url) => {
const pattern = /[?&]openid=([^&#]+)/; const pattern = /[?&]openid=([^&#]+)/;
const match = url.match(pattern); const match = url.match(pattern);
@@ -62,6 +64,18 @@ const Index = () => {
return null; return null;
}; };
// 是否在微信客户端
const isWeChatFun = () => {
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("micromessenger") != -1) {
setisWeChat(true);
return true;
} else {
setisWeChat(false);
return false;
}
};
// 获取openid // 获取openid
const getOpenid = () => { const getOpenid = () => {
// let newparams = Taro.getCurrentInstance().router.params; // let newparams = Taro.getCurrentInstance().router.params;
@@ -232,13 +246,14 @@ const Index = () => {
// 入群申请 // 入群申请
useEffect(() => { useEffect(() => {
// 获取openId let inwechat = isWeChatFun();
getOpenid(); if (inwechat) {
// window.alert(window.location.href) // 获取openId
// o7LFAwR32hWGq9XOpd7ZxK1wZxq8 getOpenid();
// window.alert(window.localStorage.getItem('openid')) } else {
// window.alert('不在微信')
// tgMessage();
}
}, []); }, []);
const goBack = () => { const goBack = () => {