80 lines
2.5 KiB
TypeScript
80 lines
2.5 KiB
TypeScript
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)));
|
||
};
|