h5util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @Description: 通过外部webView跳转页面
  3. * @author: xionghaojie
  4. * @param url //页面地址
  5. * @param type //跳转类型
  6. * @param onlyWeb //是否是webView内部页面跳转(默认true)
  7. * @return
  8. * @createTime: 2024-02-21 14:38:21
  9. */
  10. function jumpUrlExternal({url='',type='navigateTo',onlyWeb=true}) {
  11. webUrl = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + url
  12. var ua = window.navigator.userAgent.toLowerCase();
  13. //判断是否是微信环境
  14. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  15. jWeixin.miniProgram[type]({
  16. url: onlyWeb?'/pages/webView/webView?url=' + encodeURIComponent(webUrl):url
  17. });
  18. // if(!type||type=='navigateTo'){
  19. // jWeixin.miniProgram.navigateTo({
  20. // url: onlyWeb?'/pages/webView/webView?url=' + encodeURIComponent(webUrl):url
  21. // });
  22. // }else if(type=='switchTab'){
  23. // jWeixin.miniProgram.switchTab({
  24. // url: onlyWeb?'/pages/webView/webView?url=' + encodeURIComponent(webUrl):url
  25. // });
  26. // }
  27. } else {
  28. // #ifdef H5
  29. postMsg({
  30. type: 'function', //function 直接调用方法(此时eventName必传) msg 传递参数
  31. eventName: 'jumpUrl',
  32. data: {
  33. type:type,
  34. url: onlyWeb?'/pages/webView/webView?url=' + encodeURIComponent(webUrl):url
  35. }
  36. })
  37. // #endif
  38. }
  39. }
  40. /**
  41. * @Description: 向外部webView传递数据(主要用于H5嵌套,请求外部方法,小程序由于限制(web-view postMessage触发限制)多用于分享时传递参数)
  42. * @author: xionghaojie
  43. * @param data //需要传递的数据 类型obj {type,eventName,data} type:数据类型(function触发外部方法) eventName外部方法名 data自定义数据(json格式)
  44. * @return
  45. * @createTime: 2024-02-21 14:41:35
  46. */
  47. function postMsg(data) {
  48. try {
  49. //小程序环境设置
  50. var ua = window.navigator.userAgent.toLowerCase();
  51. //判断是否是微信环境
  52. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  53. //微信小程序环境
  54. jWeixin.miniProgram.getEnv((res) => {
  55. if(res.miniprogram){
  56. jWeixin.miniProgram.postMessage({
  57. data: data
  58. })
  59. }else{
  60. window.parent.postMessage(data, '*'); //后面的*号就是处理跨域问题的
  61. }
  62. })
  63. } else {
  64. // #ifdef H5
  65. window.parent.postMessage(data, '*'); //后面的*号就是处理跨域问题的
  66. // #endif
  67. }
  68. } catch (ex) {
  69. console.log(ex);
  70. }
  71. }
  72. /**
  73. * @Description:
  74. * @author: xionghaojie
  75. * @param title 分享的标题
  76. * @param imageUrl 分享图
  77. * @param path 分享路径(可不穿,外部webView会默认当前页面,除非需要自定义的分享路径)
  78. * @param shareType 分享类型(1 内容 2 产品)
  79. * @param id 分享对象id(shareType 和 id两个参数是为了在外部webView分享页面可查详情渲染页面)
  80. * @return
  81. * @createTime: 2024-02-21 16:09:34
  82. */
  83. function jumpShare({title='',imageUrl='',path='',shareType='',id=''}){
  84. webUrl = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + path
  85. let shareUrl = '/pages/webShare/webShare?title='+title+'&imageUrl='+imageUrl+'&path='+encodeURIComponent(webUrl)
  86. if(shareType){
  87. shareUrl = shareUrl + '&shareType='+shareType
  88. }
  89. if(id){
  90. shareUrl = shareUrl + '&id='+id
  91. }
  92. jumpUrlExternal({
  93. url:shareUrl,
  94. onlyWeb:false
  95. })
  96. }
  97. /**
  98. * @Description:支付
  99. * @author: xionghaojie
  100. * @param orderId 订单id
  101. * @return
  102. * @createTime: 2024-02-21 16:09:34
  103. */
  104. function jumpPay({orderId='',isRx='',pay='',activityId='',aprId='',merchantId='',money='',orderType=''}){
  105. if(!orderId) return false
  106. let payUrl = '/pages/webPay/webPay?orderId='+orderId+'&isRx='+isRx+'&pay='+pay+'&activityId='+activityId+'&aprId='+aprId+'&merchantId='+merchantId+'&money='+money+'&orderType='+orderType
  107. jumpUrlExternal({
  108. url:payUrl,
  109. onlyWeb:false
  110. })
  111. }
  112. /**
  113. * @Description:跳转首页
  114. * @author: xionghaojie
  115. * @param orderId 订单id
  116. * @return
  117. * @createTime: 2024-02-21 16:09:34
  118. */
  119. function jumpIndex(){
  120. let payUrl = '/pages/tabBar/index'
  121. jumpUrlExternal({
  122. url:payUrl,
  123. type:'switchTab',
  124. onlyWeb:false
  125. })
  126. }
  127. module.exports = {
  128. jumpUrlExternal:jumpUrlExternal,
  129. postMsg:postMsg,
  130. jumpShare:jumpShare,
  131. jumpPay:jumpPay,
  132. jumpIndex:jumpIndex
  133. };