// info参数
/*
let params={
eleId:"", // 元素ID
appid:"", // 小程序id号 gh_****
url:"", // 跳转小程序的页面路径地址 例: pages/home/home.html - (后面必须带上.html后缀 否则IOS跳转时出现小程序页面未配置)
content:"" // html字符串 例: ""
}
*/
export function wx_launch(info){
if (!is_weixn()) {
return
}
if(!is_version()){
var btn = document.getElementById(info.eleId); //获取元素
let script = document.createElement("script");// 创建script内容插槽 避免template标签冲突
script.type = "text/wxtag-template"; // 使用script插槽 必须定义这个type
script.text = info.content // 自定义的html字符串内容
let html = `${script.outerHTML}`;
btn.innerHTML = html; // html字符串赋值
// 点击按钮 正常跳转触发
btn.addEventListener("launch", function (e) {
console.log("success");
});
// 点击跳转 抛出异常
btn.addEventListener("error", function (e) {
console.log("fail", e.detail);
alert(`跳转异常 - ${e.detail}`)
});
}else{
alert("您的版本号不支持")
}
}
// 判断是否微信环境
function is_weixn() {
let ua = navigator.userAgent.toLowerCase()
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true
} else {
return false
};
};
// 判断当前微信版本号是否支持
function is_version(){
let client = false; // 当前版本号是否支持 (默认不支持)
let wxInfo = navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i); // 微信浏览器信息
// 微信版本号 wxInfo[1] = "7.0.18.1740" (示例)
//进行split转成数组进行判断 [7,0,18,1740] (示例)
let version = wxInfo[1].split(".");
// 判断版本在7.0.12及以上的版本
if (version[0] >= 7) {
if (version[1] >= 0) {
if (version[2] >= 12) {
client = true; // 当前版本支持
}
}
}
return client;
}