h5util.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. } else {
  19. // #ifdef H5
  20. postMsg({
  21. type: 'function', //function 直接调用方法(此时eventName必传) msg 传递参数
  22. eventName: 'jumpUrl',
  23. data: {
  24. type:type,
  25. url: onlyWeb?'/pages/webView/webView?url=' + encodeURIComponent(webUrl):url
  26. }
  27. })
  28. // #endif
  29. }
  30. }
  31. /**
  32. * @Description: 向外部webView传递数据(主要用于H5嵌套,请求外部方法,小程序由于限制(web-view postMessage触发限制)多用于分享时传递参数)
  33. * @author: xionghaojie
  34. * @param data //需要传递的数据 类型obj {type,eventName,data} type:数据类型(function触发外部方法) eventName外部方法名 data自定义数据(json格式)
  35. * @return
  36. * @createTime: 2024-02-21 14:41:35
  37. */
  38. function postMsg(data) {
  39. try {
  40. //小程序环境设置
  41. var ua = window.navigator.userAgent.toLowerCase();
  42. //判断是否是微信环境
  43. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  44. //微信小程序环境
  45. jWeixin.miniProgram.getEnv((res) => {
  46. if(res.miniprogram){
  47. jWeixin.miniProgram.postMessage({
  48. data: data
  49. })
  50. }else{
  51. window.parent.postMessage(data, '*'); //后面的*号就是处理跨域问题的
  52. }
  53. })
  54. } else {
  55. // #ifdef H5
  56. window.parent.postMessage(data, '*'); //后面的*号就是处理跨域问题的
  57. // #endif
  58. }
  59. } catch (ex) {
  60. console.log(ex);
  61. }
  62. }
  63. /**
  64. * @Description:
  65. * @author: xionghaojie
  66. * @param title 分享的标题
  67. * @param imageUrl 分享图
  68. * @param path 分享路径(可不穿,外部webView会默认当前页面,除非需要自定义的分享路径)
  69. * @param shareType 分享类型(1 内容 2 产品)
  70. * @param id 分享对象id(shareType 和 id两个参数是为了在外部webView分享页面可查详情渲染页面)
  71. * @return
  72. * @createTime: 2024-02-21 16:09:34
  73. */
  74. function jumpShare({title='',imageUrl='',path='',shareType='',id='',merchantId='',sendBehaviorObj={}}){
  75. let webUrl = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + path
  76. if(path.indexOf('/share/home/index')>-1){
  77. webUrl = path
  78. }else{
  79. webUrl = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + path
  80. }
  81. let shareUrl = '/pages/webShare/webShare?title='+title+'&imageUrl='+imageUrl+'&path='+encodeURIComponent(webUrl)
  82. if(shareType){
  83. shareUrl = shareUrl + '&shareType='+shareType
  84. }
  85. if(id){
  86. shareUrl = shareUrl + '&id='+id
  87. }
  88. if(merchantId){
  89. shareUrl = shareUrl + '&merchantId='+merchantId
  90. }
  91. shareUrl = shareUrl + '&sendBehaviorObj=' + JSON.stringify(sendBehaviorObj)
  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='',status='',payType='order'}){
  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 +'&status='+status+'&orderType='+orderType+'&payType='+payType
  107. jumpUrlExternal({
  108. url:payUrl,
  109. onlyWeb:false
  110. })
  111. }
  112. /**
  113. * @Description:跳转首页
  114. * @author: xionghaojie
  115. * @param url //页面路径
  116. * @return
  117. * @createTime: 2024-02-21 16:09:34
  118. */
  119. function jumpIndex(url){
  120. let payUrl = url?url:'/pages/index/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. };