仅前端
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
private async wxConfig() {
// 请求接口获取微信签名
const shareParams = {
title: 'title',
description: '摘要',
image: '图片线上地址',
url: window.location.href,
};
const signParams: any = {
signUrl: shareParams.url,
};
const jsApiList = [
'onMenuShareAppMessage', 'onMenuShareTimeline', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone',
];
const wxShareParams = {
title: shareParams.title, // 分享标题
desc: shareParams.description, // 分享描述
link: shareParams.url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: shareParams.image, // 分享图标
};
const res = await this.chargerMapService.wxConfigSig(signParams.url);
if (!res.data) {
return
}
const data = res.data
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来
appId: data.app_id, // 必填,公众号的唯一标识
timestamp: data.timestamp, // 必填,生成签名的时间戳
nonceStr: data.nonce_str, // 必填,生成签名的随机串
signature: data.signature, // 必填,签名
jsApiList,
});
wx.ready(() => {
wx.checkJsApi({
jsApiList,
});
// 分享到朋友圈
wx.onMenuShareTimeline(wxShareParams);
// 分享给朋友
wx.onMenuShareAppMessage(wxShareParams);
// 分享到QQ
wx.onMenuShareQQ(wxShareParams);
// 分享到腾讯微博
wx.onMenuShareWeibo(wxShareParams);
// 分享到QQ空间
wx.onMenuShareQZone(wxShareParams);
});
}
h5页面环境判断
const ua = navigator.userAgent || '';
// 版本号比较
export const semverCompare = (verionA: string, versionB: string): -1 | 0 | 1 => {
const {isNaN} = Number;
const splitA = verionA.split('.');
const splitB = versionB.split('.');
for (let i = 0; i < 3; i++) {
const snippetA = Number(splitA[i]);
const snippetB = Number(splitB[i]);
if (snippetA > snippetB) {
return 1;
}
if (snippetB > snippetA) {
return -1;
}
// e.g. '1.0.0-rc' -- Number('0-rc') = NaN
if (!isNaN(snippetA) && isNaN(snippetB)) {
return 1;
}
if (isNaN(snippetA) && !isNaN(snippetB)) {
return -1;
}
}
return 0;
};
/**
* 获取 ios 大版本号
*/
export const getIOSVersion = (): number => {
const version = navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/) as string[];
return Number.parseInt(version[1], 10);
};
/**
* 获取 微信 版本号
*/
export const getWeChatVersion = (): string => {
const version = navigator.appVersion.match(/micromessenger\/(\d+\.\d+\.\d+)/i) as string[];
return version[1];
};
export const isAndroid = /android/i.test(ua);
export const isIos = /iphone|ipad|ipod/i.test(ua);
export const isWechat = /micromessenger\/([\d.]+)/i.test(ua);
export const isWeibo = /(weibo).*weibo__([\d.]+)/i.test(ua);
export const isBaidu = /(baiduboxapp)\/([\d.]+)/i.test(ua);
export const isQQ = /qq\/([\d.]+)/i.test(ua);
export const isQQBrowser = /(qqbrowser)\/([\d.]+)/i.test(ua);
export const isQzone = /qzone\/.*_qz_([\d.]+)/i.test(ua);
// 安卓 chrome 浏览器,包含 原生chrome浏览器、三星自带浏览器、360浏览器以及早期国内厂商自带浏览器
export const isOriginalChrome =
/chrome\/[\d.]+ mobile safari\/[\d.]+/i.test(ua) && isAndroid && ua.indexOf('Version') < 0;
// chrome for ios 和 safari 的区别仅仅是将 Version/<VersionNum> 替换成了 CriOS/<ChromeRevision>
// ios 上很多 app 都包含 safari 标识,但它们都是以自己的 app 标识开头,而不是 Mozilla
export const isSafari =
/safari\/([\d.]+)$/i.test(ua) && isIos && ua.indexOf('Crios') < 0 && ua.indexOf('Mozilla') === 0;