Vue的写法

main.js中定义:

  1. /* ================================= APP下载 start ========================================*/
  2. // 判断是安卓还是IOS
  3. Vue.prototype.detect = function (){
  4. var equipmentType = "";
  5. var agent = navigator.userAgent.toLowerCase();
  6. var android = agent.indexOf("android");
  7. var iphone = agent.indexOf("iphone");
  8. var ipad = agent.indexOf("ipad");
  9. if(android != -1){
  10. console.log('android')
  11. equipmentType = "android";
  12. }
  13. if(iphone != -1 || ipad != -1){
  14. console.log('ios')
  15. equipmentType = "ios";
  16. }
  17. return equipmentType;
  18. }
  19. // 判断是否是微信或者微博环境
  20. Vue.prototype.softEnviroment = function () {
  21. let ua = window.navigator.userAgent.toLowerCase();
  22. let isweixin = ua.match(/micromessenger/i) != null;
  23. let isweibo = ua.match(/weibo/i) == "weibo";
  24. if (isweixin) {
  25. return 'weixin'
  26. }else if (isweibo) {
  27. return 'weibo'
  28. }
  29. return ''
  30. }
  31. // IOS 的唤起
  32. Vue.prototype.downloadIOSApp = function (urlScheme, storeURL, fallback, universalLink) {
  33. var tid = this.deferFallback(3000, storeURL, fallback);
  34. let str = navigator.userAgent.toLowerCase();
  35. let list = str.match(/cpu iphone os (.*?) like mac os/);
  36. console.log(str, list)
  37. if (parseInt(list[1], 10) < 8) {
  38. this.bindPagehideEvent(tid);
  39. } else {
  40. this.bindVisibilityChangeEvent(tid);
  41. }
  42. if (parseInt(list[1], 10) > 8) {
  43. // 通过universalLink
  44. if (universalLink === undefined) {
  45. universalLink = urlScheme;
  46. } else {
  47. clearTimeout(tid);
  48. }
  49. this.vLocation(universalLink);
  50. } else {
  51. this.vIframe(urlScheme);
  52. }
  53. }
  54. // IOS 中的 可见性事件
  55. Vue.prototype.bindPagehideEvent = function (tid) {
  56. window.addEventListener('pagehide', function clear() {
  57. if (this.isPageVisible()) {
  58. clearTimeout(tid);
  59. window.removeEventListener('pagehide', clear);
  60. }
  61. });
  62. }
  63. Vue.prototype.bindVisibilityChangeEvent = function (tid) {
  64. document.addEventListener('visibilitychange', function clear() {
  65. if (this.isPageVisible()) {
  66. clearTimeout(tid);
  67. document.removeEventListener('visibilitychange', clear);
  68. }
  69. });
  70. }
  71. Vue.prototype.isPageVisible = function () {
  72. var attrNames = ['hidden', 'webkitHidden'];
  73. for (var i = 0, len = attrNames.length; i < len; i++) {
  74. if (typeof document[attrNames[i]] !== 'undefined') {
  75. return !document[attrNames[i]];
  76. }
  77. }
  78. return true;
  79. }
  80. // 安卓 的唤起
  81. Vue.prototype.deferFallback = function (timeout, storeURL, fallback) {
  82. var clickedAt = new Date().getTime();
  83. return setTimeout(function () {
  84. var now = new Date().getTime();
  85. if (Vue.prototype.isPageVisible() && now - clickedAt < timeout + 1000) {
  86. fallback(storeURL);
  87. }
  88. }, timeout);
  89. }
  90. // 打开页面的方式可能
  91. // 1. 通过改变location
  92. Vue.prototype.vLocation = function (urlScheme) {
  93. window.location.href = urlScheme;
  94. }
  95. // 2. 通过ifreame
  96. Vue.prototype.vIframe = function (urlScheme) {
  97. setTimeout(() => {
  98. var iframe = this.createHiddenIframe('appLauncher');
  99. iframe.src = urlScheme;
  100. }, 100);
  101. }
  102. // 3. 通过intent
  103. function vIntent(launchURI) {
  104. if (browser.name == 'Chrome') {
  105. move();
  106. } else {
  107. setTimeout(move, 100);
  108. }
  109. function move() {
  110. // window.top.location.href = launchURI;
  111. window.location.href = launchURI
  112. }
  113. }
  114. Vue.prototype.createHiddenIframe = function (id) {
  115. const ele = document.createElement('iframe');
  116. ele.id = id;
  117. ele.style.display = 'none';
  118. document.documentElement.appendChild(ele);
  119. return ele;
  120. }
  121. /* ================================= APP下载 end ========================================*/

vue中调用

  1. // 进入应用宝下载APP(区分ios和安卓)
  2. if (enviro === 'weibo' || enviro === 'weixin') {
  3. // 如果是微博或者微信的话,则android提示“选择在浏览器中打开”,IOS提示“选择“在safari中打开””
  4. this.$refs.tipBg.style.display = 'block';
  5. } else {
  6. // 区分android和IOS
  7. if (this.palateInfo === 'android') {
  8. this.deferFallback(0, '/apis/web/app/download/KTB.apk', this.vLocation);
  9. } else if (this.palateInfo === 'ios') {
  10. this.downloadIOSApp('baswiftdiscount://', 'https://apps.apple.com/cn/app/%E5%BF%AB%E8%B4%B4%E5%AE%9D/id1527518469', this.vLocation, 'https://ktb.rzline.com.cn');
  11. }
  12. }