2025-11-08 20:34:15 +08:00

80 lines
2.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Base64 } from "js-base64";
export const wxInfo = wx.getSystemInfoSync();
export const mpInfo = wx.getAccountInfoSync();
export const miniProgramVersion = mpInfo.miniProgram.version || "1.0.0";
const webUrl = require("../api/base").allBaseUrl.GDEnvs.web;
// 获取微信小程序token
export const getWxToken = async () => {
return new Promise((resolve) => {
wx.showLoading({ title: "加载中" });
wx.getStorage({
key: "user_info",
async success(res) {
console.warn("----- my data is res1111: ", res);
if (res.data.token) {
wx.hideLoading();
resolve(handleGetReturnData(res.data));
}
},
fail() {
wx.hideLoading();
wx.navigateTo({ url: "/pages/login/index" });
},
});
});
};
// 跳转H5页面
export const onGoH5Page = (params: any) => {
// url跳转链接is_need_login是否需要登录true是、false否type页面类型third三方
const { type, url, is_need_login } = params;
if (is_need_login) {
getWxToken().then((res: any) => {
if (res) {
const targetUrl = `${webUrl}${url}${
/\?/.test(url) ? "&" : "?"
}returnData=${res}`;
wx.navigateTo({
url: `/pages/H5/index?url=${encodeURIComponent(targetUrl)}`,
});
}
});
} else {
if (type === "third") {
wx.navigateTo({ url: `/pages/H5/index?url=${encodeURIComponent(url)}` });
} else {
const userInfo = wx.getStorageSync("user_info") || {};
const returnData = handleGetReturnData(userInfo);
const targetUrl = `${webUrl}${url}${
/\?/.test(url) ? "&" : "?"
}returnData=${returnData}`;
wx.navigateTo({
url: `/pages/H5/index?url=${encodeURIComponent(targetUrl)}`,
});
}
}
};
const handleGetReturnData = (userInfo: any) => {
const envParams = {};
const locationDataObj = wx.getStorageSync("locationData") || {}; // 获取经纬度
const launchOptions = wx.getStorageSync("launch_options") || {}; // 获取小程序启动时的参数
Object.assign(
envParams,
{
wxXcxLaunchOptions: launchOptions,
phoneType: wxInfo.model,
devType: wxInfo.platform === "ios" ? 0 : 1,
devOS: wxInfo.platform,
devOSVersion: wxInfo.system,
devVersion: wxInfo.version,
appVersion: miniProgramVersion.replace(/\./g, ""),
},
locationDataObj
);
const obj = { envParams, extendParams: userInfo };
console.warn("----- my data is obj111: ", obj);
return encodeURIComponent(Base64.encode(JSON.stringify(obj)));
};