diff --git a/src/pages/index/index.jsx b/src/pages/index/index.jsx index 1f8e1d6..0e63d11 100644 --- a/src/pages/index/index.jsx +++ b/src/pages/index/index.jsx @@ -53,6 +53,16 @@ const Index = () => { const [joinMeetup, setjoinMeetup] = useState(false); + const getOpenidFromUrl=(url)=> { + const pattern = /[?&]openid=([^&#]+)/; + const match = url.match(pattern); + if (match) { + return match[1]; + } + return null; + } + + // 获取openid const getOpenid = () => { // let newparams = Taro.getCurrentInstance().router.params; @@ -62,50 +72,24 @@ const Index = () => { // http://192.168.31.245:10086/?openid=o7LFAwR32hWGq9XOpd7ZxK1wZxq8#/pages/index/index let hashopenid = null; if (window.location.href.includes("openid")) { - hashopenid = window.location.href - .split("#")[0] - .split("?")[1] - .split("=")[1]; + hashopenid= getOpenidFromUrl(window.location.href) } else { - hashopenid = window.localStorage.getItem("openid"); + // hashopenid = window.localStorage.getItem("openid"); } - - let newOpenid = window.localStorage.getItem("openid"); - - if ( - (!!newOpenid && newOpenid != "undefined" && newOpenid != "null") || - (!!hashopenid && hashopenid != "undefined" && hashopenid != "null") - ) { + if (!!hashopenid && hashopenid != "undefined" && hashopenid != "null") { // 获取到openid window.localStorage.setItem("openid", hashopenid); + setopenId(hashopenid); + // window.alert(hashopenid); // 查询数据库是否存在openid - Taro.request({ - method: "GET", - // url: `/api/add?openid=${hashopenid}`, //仅为示例,并非真实的接口地址 - url: `/api/user/${window.localStorage.getItem("openid")}`, //仅为示例,并非真实的接口地址 - success: function (res) { - if (res?.data?.userInfo) { - //判断wxid是否存在 - if (res?.data?.userInfo?.wxid) { - // 有完整的用户信息userInfo - setuserInfo(res?.data?.userInfo); - window.localStorage.setItem("userInfo", userInfo); - } else { - //不完整的openid用户信息 - } - } else { - // 新增openid用户 - // addopenidUser() - } - }, - complete: function (finy) { - // tg消息通知 - tgMessage(); - }, - }); + + // 获取wxid参数 + getWxidFromUrl(); + } else { let nowUrl = window.location.href; + window.localStorage.removeItem("openid") let payUrl = `https://payjs.cn/api/openid?mchid=1561724891&callback_url=${nowUrl}`; window.location.href = payUrl; } @@ -147,15 +131,18 @@ const Index = () => { if (res?.data?.userInfo) { //判断wxid是否存在 if (res?.data?.userInfo?.openid) { + // window.alert(res?.data?.userInfo?.openid); + // window.alert(window.localStorage.getItem("openid")); + if ( res?.data?.userInfo?.openid !== window.localStorage.getItem("openid") ) { // 删除其他用户的wxid - window.localStorage.removeItem('userInfo') - window.localStorage.removeItem('openid') - window.localStorage.removeItem('wxid') - window.location.href="https://nomad.hackrobot.cn" + window.localStorage.removeItem("userInfo"); + window.localStorage.removeItem("openid"); + window.localStorage.removeItem("wxid"); + window.location.href = "http://nomad.hackrobot.cn"; } else { // 有完整的用户信息userInfo setuserInfo(res?.data?.userInfo); @@ -239,8 +226,7 @@ const Index = () => { // window.alert(window.location.href) // o7LFAwR32hWGq9XOpd7ZxK1wZxq8 // window.alert(window.localStorage.getItem('openid')) - // 获取wxid参数 - getWxidFromUrl(); + // tgMessage(); }, []); @@ -393,7 +379,9 @@ const Index = () => { // type: "success", // }); - setbookToast(true); + if (res.data.code == "200") { + setbookToast(true); + } }, fail: function (err) {}, complete: function (finy) { @@ -518,7 +506,9 @@ const Index = () => { // message: "预约成功", // type: "success", // }); - setmeetupToast(true); + if (res.data.code == "200") { + setmeetupToast(true); + } }, fail: function (err) {}, complete: function (finy) {