test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. export function init(){
  2. if (!is_weixn()) {
  3. return
  4. }
  5. wx.config({
  6. debug: false,
  7. appId, // 和获取Ticke的必须一样------必填,公众号的唯一标识
  8. timestamp: timestamp, // 必填,生成签名的时间戳
  9. nonceStr: nonceStr, // 必填,生成签名的随机串
  10. signature: signature, // 必填,签名
  11. //需要微信权限列表
  12. jsApiList: [
  13. "onMenuShareAppMessage", // 分享给朋友
  14. "onMenuShareTimeline", // 分享到朋友圈
  15. "onMenuShareQQ", // 分享到QQ
  16. "onMenuShareQZone", // 分享到QQ空间
  17. "checkJsApi" //判断当前客户端版本是否支持指定JS接口
  18. ],
  19. openTagList:['wx-open-launch-weapp'] // 微信开放标签 小程序跳转按钮:<wx-open-launch-weapp>
  20. });
  21. }
  22. // 动态生成标签
  23. // info参数
  24. /*
  25. let params={
  26. eleId:"", // 元素ID
  27. appid:"", // 小程序id号 gh_****
  28. url:"", // 跳转小程序的页面路径地址 例: pages/home/home.html - (后面必须带上.html后缀 否则IOS跳转时出现小程序页面未配置)
  29. content:"" // html字符串 例: "<button>点我</button>"
  30. }
  31. */
  32. export function wx_launch(info){
  33. if (!is_weixn()) {
  34. return
  35. }
  36. if(is_launch()){
  37. var btn = document.getElementById(info.eleId); //获取元素
  38. let script = document.createElement("script");// 创建script内容插槽 避免template标签冲突
  39. script.type = "text/wxtag-template"; // 使用script插槽 必须定义这个type
  40. script.text = info.content // 自定义的html字符串内容
  41. let html = `<wx-open-launch-weapp style="width:100%;display:block;" username="${info.appid}" path="${info.url}">${script.outerHTML}</wx-open-launch-weapp>`;
  42. btn.innerHTML = html; // html字符串赋值
  43. // 点击按钮 正常跳转触发
  44. btn.addEventListener("launch", function (e) {
  45. console.log("success");
  46. });
  47. // 点击跳转 抛出异常
  48. btn.addEventListener("error", function (e) {
  49. console.log("fail", e.detail);
  50. alert(`跳转异常 - ${e.detail}`)
  51. });
  52. }else{
  53. alert("您的版本号不支持")
  54. }
  55. }
  56. // 判断是否微信环境
  57. function is_weixn() {
  58. let ua = navigator.userAgent.toLowerCase()
  59. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  60. return true
  61. } else {
  62. return false
  63. };
  64. };
  65. // 判断当前微信版本号是否支持
  66. function is_version(){
  67. let client = false; // 当前版本号是否支持 (默认不支持)
  68. let wxInfo = navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i); // 微信浏览器信息
  69. // 微信版本号 wxInfo[1] = "7.0.18.1740" (示例)
  70. //进行split转成数组进行判断 [7,0,18,1740] (示例)
  71. let version = wxInfo[1].split(".");
  72. // 判断版本在7.0.12及以上的版本
  73. if (version[0] >= 7) {
  74. if (version[1] >= 0) {
  75. if (version[2] >= 12) {
  76. client = true; // 当前版本支持
  77. }
  78. }
  79. }
  80. return client;
  81. }