h5-down-app-popup.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <uni-popup ref="downappPop" type="center" is-mask-click="false">
  3. <view class="downappPop">
  4. <uni-icons class="closebtn" type="closeempty" size="27" color="#bbb" @click="$refs.downappPop.close()"></uni-icons>
  5. <image src="/static/logo.png" class="es-w-100 es-h-100"></image>
  6. <view>请下载App观看</view>
  7. <view class="es-br-20 es-w-250 es-bc es es-pc es-ac es-fw-bold es-fs-30" style="min-height: 80rpx;border-radius: 10rpx;" @tap="openApp('btn')">
  8. <view class="es-ml-30 es-mr-30">App内打开</view>
  9. </view>
  10. <!-- <template v-if="isWechat">
  11. <wx-open-launch-app appid="" :extinfo="extinfo" @launch="AppLaunch" @error="AppError">
  12. <component :is="'script'" type="text/wxtag-template">
  13. <button id="footer-btn" class="es-br-20 es-w-250 es-bc es es-pc es-ac es-fw-bold es-fs-30 es-ml-30 es-mr-30" style="min-height: 80rpx;border-radius: 10rpx;">
  14. App内打开
  15. </button>
  16. </component>
  17. </wx-open-launch-app>
  18. </template> -->
  19. </view>
  20. </uni-popup>
  21. </template>
  22. <script>
  23. import * as wx from 'weixin-js-sdk'
  24. import {getWxConfig} from "@/api/common.js";
  25. export default {
  26. props: ['pageUrl','type','courseId'],
  27. data() {
  28. return {
  29. timer: null,
  30. isWechat: false,
  31. // 跳转所需额外信息
  32. extinfo: "zklive://livingApp",
  33. }
  34. },
  35. created() {
  36. this.isWechat = String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger"
  37. // this.initWXConfig()
  38. },
  39. methods: {
  40. openPop() {
  41. this.$refs.downappPop.open()
  42. },
  43. initWXConfig(packageVal) {
  44. if(this.isWechat) {
  45. let url = window.location.href.split('#')[0]
  46. let param ={
  47. url: encodeURIComponent(url)
  48. }
  49. uni.showLoading({
  50. title: '加载中'
  51. });
  52. getWxConfig(param).then(res => {
  53. uni.hideLoading();
  54. if (res.code == 200) {
  55. wx.config({
  56. debug: false,
  57. appId: res.data.appId,
  58. timestamp: res.data.timestamp,
  59. nonceStr: res.data.nonceStr,
  60. signature: res.data.signature,
  61. jsApiList: ['requestMerchantTransfer','updateAppMessageShareData'],
  62. openTagList: ['wx-open-launch-app']
  63. });
  64. wx.error(function(error){
  65. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  66. });
  67. } else {
  68. uni.showToast({
  69. title: res.msg,
  70. icon: 'none'
  71. })
  72. }
  73. }).catch(err=>{
  74. uni.hideLoading();
  75. })
  76. } else {
  77. // 非微信浏览器
  78. this.openApp()
  79. }
  80. },
  81. AppLaunch(e){
  82. console.log("success",e)
  83. },
  84. AppError(e){
  85. if(e.detail.errMsg == "launch:fail"){
  86. window.location.href = uni.getStorageSync('h5Path') + "/pages/index/appDownload";
  87. } else {
  88. uni.showToast({
  89. title: e.detail.errMsg,
  90. icon: 'error'
  91. })
  92. }
  93. },
  94. // 判断浏览器是否为头部APP(判断是否为微信或则其它APP浏览器,如果是则引导外置浏览器打开)
  95. isBlackApp() {
  96. var u = navigator.userAgent.toLowerCase();
  97. return /micromessenger/i.test(u) || u.indexOf("weibo") > -1 || u.indexOf("qq") > -1 || u.indexOf('mqqbrowser') > -1;
  98. },
  99. openApp(type){
  100. if(type == 'btn') {
  101. // 打开app,打不开跳转下载页
  102. const isOpenMode = this.isOpenMode()
  103. if(isOpenMode == 1 ||isOpenMode == 4) {
  104. if (this.isBlackApp() ) {
  105. // 头部APP让网页显示提示在浏览器中打开
  106. // uni.showToast({
  107. // title: '复制页面链接在浏览器中打开',
  108. // icon: 'none'
  109. // })
  110. window.location.href = "https://m.malink.cn/s/yaIBJz"
  111. } else {
  112. // 不是头部APP就直接打开
  113. const pagesUrl = encodeURIComponent(this.pageUrl)
  114. let schemeUrl = ''
  115. schemeUrl = "zklive://livingApp";
  116. // if(this.type == 'course') {
  117. // schemeUrl = "zklive://course?courseId="+this.courseId;
  118. // }else {
  119. // // window.location.href="zklive://pagesApp?pagesUrl="+pagesUrl;
  120. // schemeUrl = "zklive://pagesApp?pagesUrl="+pagesUrl;
  121. // }
  122. this.jumpApp(schemeUrl);
  123. this.noApp();
  124. }
  125. }
  126. } else {
  127. // 直接打开,打不开不跳转
  128. const isOpenMode = this.isOpenMode()
  129. if(isOpenMode == 4) {
  130. const pagesUrl = encodeURIComponent(this.pageUrl)
  131. let schemeUrl = ''
  132. if(this.type == 'course') {
  133. schemeUrl = "zklive://course?courseId="+this.courseId;
  134. }else {
  135. // window.location.href="zklive://pagesApp?pagesUrl="+pagesUrl;
  136. schemeUrl = "zklive://pagesApp?pagesUrl="+pagesUrl;
  137. }
  138. this.jumpApp(schemeUrl);
  139. }
  140. }
  141. },
  142. // 跳转打开app
  143. jumpApp(t) {
  144. console.log(t, "===========》jumpApp");
  145. try {
  146. var e = navigator.userAgent.toLowerCase(),
  147. n = e.match(/cpu iphone os (.*?) like mac os/);
  148. if (((n = null !== n ? n[1].replace(/_/g, ".") : 0), parseInt(n) >= 9)) {
  149. //ios8之后的版本
  150. window.location.href = t;
  151. } else { //注意iOS8之前是可以使用iframe来实现的
  152. var r = document.createElement("iframe");
  153. (r.src = t), (r.style.display = "none"), document.body.appendChild(r);
  154. }
  155. } catch (e) {
  156. window.location.href = t;
  157. }
  158. },
  159. // 无响应或者没安装跳转下载
  160. noApp() {
  161. console.log("===========》noApp");
  162. var t = Date.now(),
  163. r = uni.getStorageSync('h5Path') + "/pages/index/appDownload";
  164. this.timer = setTimeout(()=> {
  165. return Date.now() - t > 3200 ?
  166. (clearTimeout(this.timer), !1) :
  167. !document.webkitHidden &&
  168. !document.hidden &&
  169. void(location.href = r);
  170. }, 3000);
  171. },
  172. isOpenMode() {
  173. //平台、设备和操作系统
  174. var system = {
  175. win: false,
  176. mac: false,
  177. xll: false,
  178. ipad: false
  179. };
  180. //检测平台
  181. var p = navigator.platform;
  182. system.win = p.indexOf("Win") == 0;
  183. system.mac = p.indexOf("Mac") == 0;
  184. system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
  185. system.ipad = (navigator.userAgent.match(/iPad/i) != null) ? true : false;
  186. //跳转语句,如果是手机访问就自动跳转到wap.baidu.com页面
  187. if (system.win || system.mac || system.xll || system.ipad) {
  188. var ua = navigator.userAgent.toLowerCase();
  189. if (ua.match(/MicroMessenger/i) == "micromessenger") {
  190. // alert("在PC端微信上打开的");
  191. return 2;
  192. } else {
  193. // alert("在PC端非微信上打开的");
  194. return 3;
  195. }
  196. } else {
  197. var ua = navigator.userAgent.toLowerCase();
  198. if (ua.match(/MicroMessenger/i) == "micromessenger") {
  199. // alert("在手机端微信上打开的");
  200. return 1;
  201. } else {
  202. // alert("在手机上非微信上打开的");
  203. return 4;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. </script>
  210. <style scoped>
  211. .downappPop {
  212. border-radius: 20rpx;
  213. background-color: #fff;
  214. padding: 30rpx;
  215. width: 80vw;
  216. height: 400rpx;
  217. box-sizing: border-box;
  218. display: flex;
  219. flex-direction: column;
  220. align-items: center;
  221. justify-content: space-evenly;
  222. text-align: center;
  223. position: relative;
  224. }
  225. .closebtn {
  226. position: absolute;
  227. top: 14rpx;
  228. right: 14rpx;
  229. }
  230. </style>