This commit is contained in:
hackrobot
2024-07-18 11:32:59 +08:00
parent 33c3a6de8e
commit 05e0dc3511

View File

@@ -53,15 +53,14 @@ const Index = () => {
const [joinMeetup, setjoinMeetup] = useState(false);
const getOpenidFromUrl=(url)=> {
const getOpenidFromUrl = (url) => {
const pattern = /[?&]openid=([^&#]+)/;
const match = url.match(pattern);
if (match) {
return match[1];
}
return null;
}
};
// 获取openid
const getOpenid = () => {
@@ -72,24 +71,36 @@ const Index = () => {
// http://192.168.31.245:10086/?openid=o7LFAwR32hWGq9XOpd7ZxK1wZxq8#/pages/index/index
let hashopenid = null;
if (window.location.href.includes("openid")) {
hashopenid= getOpenidFromUrl(window.location.href)
hashopenid = getOpenidFromUrl(window.location.href);
} else {
// hashopenid = window.localStorage.getItem("openid");
}
if (!!hashopenid && hashopenid != "undefined" && hashopenid != "null") {
// 获取到openid
window.localStorage.setItem("openid", hashopenid);
setopenId(hashopenid);
// window.alert(hashopenid);
// 查询数据库是否存在openid
// 获取wxid参数
getWxidFromUrl();
Taro.request({
method: "GET",
url: `/api/user/${hashopenid}`, //仅为示例,并非真实的接口地址
// url: `/api/user/${window.localStorage.getItem("openid")}`, //仅为示例,并非真实的接口地址
success: function (res) {
if (res?.data?.userInfo) {
// 有完整的用户信息userInfo
setuserInfo(res?.data?.userInfo);
window.localStorage.setItem("userInfo", userInfo);
}
},
complete: function (finy) {
// 获取wxid参数
getWxidFromUrl();
},
});
} else {
let nowUrl = window.location.href;
window.localStorage.removeItem("openid")
window.localStorage.removeItem("openid");
let payUrl = `https://payjs.cn/api/openid?mchid=1561724891&callback_url=${nowUrl}`;
window.location.href = payUrl;
}