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 [joinMeetup, setjoinMeetup] = useState(false);
const getOpenidFromUrl=(url)=> { const getOpenidFromUrl = (url) => {
const pattern = /[?&]openid=([^&#]+)/; const pattern = /[?&]openid=([^&#]+)/;
const match = url.match(pattern); const match = url.match(pattern);
if (match) { if (match) {
return match[1]; return match[1];
} }
return null; return null;
} };
// 获取openid // 获取openid
const getOpenid = () => { const getOpenid = () => {
@@ -72,24 +71,36 @@ const Index = () => {
// http://192.168.31.245:10086/?openid=o7LFAwR32hWGq9XOpd7ZxK1wZxq8#/pages/index/index // http://192.168.31.245:10086/?openid=o7LFAwR32hWGq9XOpd7ZxK1wZxq8#/pages/index/index
let hashopenid = null; let hashopenid = null;
if (window.location.href.includes("openid")) { if (window.location.href.includes("openid")) {
hashopenid= getOpenidFromUrl(window.location.href) hashopenid = getOpenidFromUrl(window.location.href);
} else { } else {
// hashopenid = window.localStorage.getItem("openid"); // hashopenid = window.localStorage.getItem("openid");
} }
if (!!hashopenid && hashopenid != "undefined" && hashopenid != "null") { if (!!hashopenid && hashopenid != "undefined" && hashopenid != "null") {
// 获取到openid // 获取到openid
window.localStorage.setItem("openid", hashopenid); window.localStorage.setItem("openid", hashopenid);
setopenId(hashopenid); setopenId(hashopenid);
// window.alert(hashopenid); // window.alert(hashopenid);
// 查询数据库是否存在openid // 查询数据库是否存在openid
Taro.request({
// 获取wxid参数 method: "GET",
getWxidFromUrl(); 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 { } else {
let nowUrl = window.location.href; 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}`; let payUrl = `https://payjs.cn/api/openid?mchid=1561724891&callback_url=${nowUrl}`;
window.location.href = payUrl; window.location.href = payUrl;
} }