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 v-if="!isWechat" 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. <!-- 此处的APPID为移动应用APPID -->
  12. <wx-open-launch-app appid="wx703c4bd07bbd1695" :extinfo="extinfo" @launch="AppLaunch" @error="AppError">
  13. <component :is="'script'" type="text/wxtag-template">
  14. <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;">
  15. App内打开
  16. </button>
  17. </component>
  18. </wx-open-launch-app>
  19. </template>
  20. </view>
  21. </uni-popup>
  22. </template>
  23. <script>
  24. import * as wx from 'weixin-js-sdk'
  25. import {getWxConfig} from "@/api/common.js";
  26. export default {
  27. props: ['pageUrl','type','courseId'],
  28. data() {
  29. return {
  30. timer: null,
  31. isWechat: false,
  32. // 跳转所需额外信息
  33. extinfo: "rtlive://livingApp",
  34. }
  35. },
  36. created() {
  37. this.isWechat = String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger"
  38. this.initWXConfig()
  39. },
  40. methods: {
  41. openPop() {
  42. this.$refs.downappPop.open()
  43. },
  44. initWXConfig(packageVal) {
  45. if(this.isWechat) {
  46. let url = window.location.href.split('#')[0]
  47. let param ={
  48. url: encodeURIComponent(url)
  49. }
  50. uni.showLoading({
  51. title: '加载中'
  52. });
  53. getWxConfig(param).then(res => {
  54. uni.hideLoading();
  55. if (res.code == 200) {
  56. wx.config({
  57. debug: false,
  58. appId: res.data.appId,
  59. timestamp: res.data.timestamp,
  60. nonceStr: res.data.nonceStr,
  61. signature: res.data.signature,
  62. jsApiList: ['requestMerchantTransfer','updateAppMessageShareData'],
  63. openTagList: ['wx-open-launch-app']
  64. });
  65. wx.error(function(error){
  66. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  67. });
  68. } else {
  69. uni.showToast({
  70. title: res.msg,
  71. icon: 'none'
  72. })
  73. }
  74. }).catch(err=>{
  75. uni.hideLoading();
  76. })
  77. } else {
  78. // 非微信浏览器
  79. this.openApp()
  80. }
  81. },
  82. AppLaunch(e){
  83. console.log("success",e)
  84. },
  85. AppError(e){
  86. if(e.detail.errMsg == "launch:fail"){
  87. window.location.href = uni.getStorageSync('h5Path') + "/pages/index/appDownload";
  88. } else {
  89. uni.showToast({
  90. title: e.detail.errMsg,
  91. icon: 'error'
  92. })
  93. }
  94. },
  95. // 判断浏览器是否为头部APP(判断是否为微信或则其它APP浏览器,如果是则引导外置浏览器打开)
  96. isBlackApp() {
  97. var u = navigator.userAgent.toLowerCase();
  98. return /micromessenger/i.test(u) || u.indexOf("weibo") > -1 || u.indexOf("qq") > -1 || u.indexOf('mqqbrowser') > -1;
  99. },
  100. openApp(type){
  101. if(type == 'btn') {
  102. // 打开app,打不开跳转下载页
  103. const isOpenMode = this.isOpenMode()
  104. if(isOpenMode == 1 ||isOpenMode == 4) {
  105. if (this.isBlackApp() ) {
  106. // 头部APP让网页显示提示在浏览器中打开
  107. uni.showToast({
  108. title: '复制页面链接在浏览器中打开',
  109. icon: 'none'
  110. })
  111. } else {
  112. // 不是头部APP就直接打开
  113. const pagesUrl = encodeURIComponent(this.pageUrl)
  114. let schemeUrl = ''
  115. schemeUrl = "rtlive://livingApp";
  116. // if(this.type == 'course') {
  117. // schemeUrl = "rtlive://course?courseId="+this.courseId;
  118. // }else {
  119. // // window.location.href="rtlive://pagesApp?pagesUrl="+pagesUrl;
  120. // schemeUrl = "rtlive://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 = "rtlive://course?courseId="+this.courseId;
  134. }else {
  135. // window.location.href="rtlive://pagesApp?pagesUrl="+pagesUrl;
  136. schemeUrl = "rtlive://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>