wechat-shop/miniprogram/utils/getUserInfo.ts
2025-11-05 10:33:46 +08:00

97 lines
3.0 KiB
TypeScript
Raw 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) {
if (res.data.token) {
wx.hideLoading();
const envParams = { linktoken: "miniProgram" };
// 获取经纬度
const locationDataObj = wx.getStorageSync("locationData");
if (locationDataObj) Object.assign(envParams, { ...locationDataObj });
// 获取小程序启动时的参数
const launchOptions = wx.getStorageSync("launch_options");
if (launchOptions)
Object.assign(envParams, { wxXcxLaunchOptions: launchOptions });
// 获取系统信息
Object.assign(envParams, {
phoneType: wxInfo.model,
devType: wxInfo.platform === "ios" ? 0 : 1,
devOS: wxInfo.platform,
devOSVersion: wxInfo.system,
devVersion: wxInfo.version,
appVersion: miniProgramVersion.replace(/\./g, ""),
});
const extendParams = {
lzCode: "CJTG_XCX_LZ",
uniqueCode: res.data.uniqueCode,
userName: res.data.userName,
userId: res.data.userId,
token: res.data.token,
};
wx.getStorage({
key: "unique_code",
success(res) {
if (res) {
Object.assign(extendParams, { uniqueCode: res });
}
},
});
const obj = { envParams, extendParams };
const returnDataValue = encodeURIComponent(
Base64.encode(JSON.stringify(obj))
);
resolve(returnDataValue);
}
},
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 targetUrl = `${url}${
/\?/.test(url) ? "&" : "?"
}jumpData=${encodeURIComponent(
Base64.encode(JSON.stringify({ is_need_login: false }))
)}`;
wx.navigateTo({
url: `/pages/H5/index?url=${encodeURIComponent(targetUrl)}`,
});
}
}
};