/** * @Description: 通过外部webView跳转页面 * @author: xionghaojie * @param url //页面地址 * @param type //跳转类型 * @param onlyWeb //是否是webView内部页面跳转(默认true) * @return * @createTime: 2024-02-21 14:38:21 */ function jumpUrlExternal({url='',type='navigateTo',onlyWeb=true}) { webUrl = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + url var ua = window.navigator.userAgent.toLowerCase(); //判断是否是微信环境 if (ua.match(/MicroMessenger/i) == 'micromessenger') { jWeixin.miniProgram[type]({ url: onlyWeb?'/pages/webView/webView?url=' + encodeURIComponent(webUrl):url }); } else { // #ifdef H5 postMsg({ type: 'function', //function 直接调用方法(此时eventName必传) msg 传递参数 eventName: 'jumpUrl', data: { type:type, url: onlyWeb?'/pages/webView/webView?url=' + encodeURIComponent(webUrl):url } }) // #endif } } /** * @Description: 向外部webView传递数据(主要用于H5嵌套,请求外部方法,小程序由于限制(web-view postMessage触发限制)多用于分享时传递参数) * @author: xionghaojie * @param data //需要传递的数据 类型obj {type,eventName,data} type:数据类型(function触发外部方法) eventName外部方法名 data自定义数据(json格式) * @return * @createTime: 2024-02-21 14:41:35 */ function postMsg(data) { try { //小程序环境设置 var ua = window.navigator.userAgent.toLowerCase(); //判断是否是微信环境 if (ua.match(/MicroMessenger/i) == 'micromessenger') { //微信小程序环境 jWeixin.miniProgram.getEnv((res) => { if(res.miniprogram){ jWeixin.miniProgram.postMessage({ data: data }) }else{ window.parent.postMessage(data, '*'); //后面的*号就是处理跨域问题的 } }) } else { // #ifdef H5 window.parent.postMessage(data, '*'); //后面的*号就是处理跨域问题的 // #endif } } catch (ex) { console.log(ex); } } /** * @Description: * @author: xionghaojie * @param title 分享的标题 * @param imageUrl 分享图 * @param path 分享路径(可不穿,外部webView会默认当前页面,除非需要自定义的分享路径) * @param shareType 分享类型(1 内容 2 产品) * @param id 分享对象id(shareType 和 id两个参数是为了在外部webView分享页面可查详情渲染页面) * @return * @createTime: 2024-02-21 16:09:34 */ function jumpShare({title='',imageUrl='',path='',shareType='',id='',merchantId='',sendBehaviorObj={}}){ let webUrl = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + path if(path.indexOf('/share/home/index')>-1){ webUrl = path }else{ webUrl = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + path } let shareUrl = '/pages/webShare/webShare?title='+title+'&imageUrl='+imageUrl+'&path='+encodeURIComponent(webUrl) if(shareType){ shareUrl = shareUrl + '&shareType='+shareType } if(id){ shareUrl = shareUrl + '&id='+id } if(merchantId){ shareUrl = shareUrl + '&merchantId='+merchantId } shareUrl = shareUrl + '&sendBehaviorObj=' + JSON.stringify(sendBehaviorObj) jumpUrlExternal({ url:shareUrl, onlyWeb:false }) } /** * @Description:支付 * @author: xionghaojie * @param orderId 订单id * @return * @createTime: 2024-02-21 16:09:34 */ function jumpPay({orderId='',isRx='',pay='',activityId='',aprId='',merchantId='',money='',orderType='',status='',payType='order'}){ if(!orderId) return false 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 jumpUrlExternal({ url:payUrl, onlyWeb:false }) } /** * @Description:跳转首页 * @author: xionghaojie * @param url //页面路径 * @return * @createTime: 2024-02-21 16:09:34 */ function jumpIndex(url){ let payUrl = url?url:'/pages/index/index' jumpUrlExternal({ url:payUrl, type:'switchTab', onlyWeb:false }) } module.exports = { jumpUrlExternal:jumpUrlExternal, postMsg:postMsg, jumpShare:jumpShare, jumpPay:jumpPay, jumpIndex:jumpIndex };