首页 > 其他分享 >uniapp app端内嵌webview对接支付宝支付

uniapp app端内嵌webview对接支付宝支付

时间:2023-05-08 11:22:44浏览次数:43  
标签:toast uniapp app 50% box replace let webview document

//app支付
const appRequestPayment = (orderInfo: string) => {
uni.navigateTo({
url: '跳转的路径?one=' + encodeURIComponent(JSON.stringify(orderInfo))
})
}
View Code
<view class="page">        
    <web-view :src="state.navUrl"></web-view>
</view>
View Code
<script setup lang="ts">
    import {
        onl oad,
        onReady,
        onHide,
        onUnload,
        onBackPress,
    } from '@dcloudio/uni-app';
    import {
        reactive,
        getCurrentInstance,
    } from 'vue'
    
    const state : any = reactive({
                //src目录下的hybrid目录新建html页面
        navUrl: '../../hybrid/html/xxx.html?data=',
        action: '',
        isShow: true,
        navHeight: 0,
    })
    onl oad((options : {
        one : string,
    }) => {        
        let data = JSON.parse(decodeURIComponent(options.one))
        let htmlSplit = data.replace(/\?/g, "%3F")
        let htmlSplit3 = htmlSplit.replace(/\#/g, "%233")
        let sult = encodeURIComponent(JSON.stringify(htmlSplit3))
        state.navUrl += JSON.parse(decodeURIComponent(sult))
    })
    onReady(() => {
        // #ifdef APP-PLUS
        // vue2写法
        //     const currentWebview = this.$mp.page.$getAppWebview();
        // vue3写法
        /* 获取屏幕信息 */
        let pages = getCurrentPages();
        let page : any = pages[pages.length - 1];
        
        setTimeout(() => {
            let currentWebview = page.$getAppWebview();
            var wv = currentWebview.children()[0];
            let url = '此处需要拦截h5跳转的地址,转接到APP页面上来'
            // wv.overrideUrlLoading({mode: 'allow',match: '.*alipay\.com/.*'},(e)=>{//允许阿里的地址跳转
            wv.overrideUrlLoading({
                // "reject"  表示满足match属性定义的提交时拦截url跳转并触发callback回调,不满足match属性定义的条件时不拦截url继续加载。
                // "allow"  表示满足match属性定义的条件时不拦截url继续加载,不满足match属性定义的条件时拦截url跳转并触发callback回调;
                mode: 'reject',
                match: `.*${url}\.com/.*`
            }, (e) => {
                
            })
            
        }, 180)
    })
    onBackPress((e) => {
        // 强制不允许返回
        // return true;
    })
</script>
<style scoped lang="scss">
    .page {
        width: $uni-width-primary;
        margin: 0 auto;
    }    
</style>    
View Code
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <mete http-equiv="contentType" content="textml;charst=utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>支付订单</title>
            <style>
                @keyframes show {
                    0% {
                        opacity: 0;
                    }

                    100% {
                        opacity: 1;
                    }
                }

                @keyframes hide {
                    0% {
                        opacity: 1;
                    }

                    100% {
                        opacity: 0;
                    }
                }

                .toast_box {
                    width: 100%;
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);
                    justify-content: center;
                    display: none;
                }

                .toast_box p {
                    box-sizing: border-box;
                    padding: 10px 20px;
                    max-width: 72%;
                    width: max-content;
                    background: #000;
                    color: #FFF;
                    font-size: 16px;
                    text-align: center;
                    border-radius: 6px;
                    opacity: 0.8;
                }

                .zan-dialog-mask {
                    position: fixed;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    z-index: 999;
                    background: rgba(0, 0, 0, 0.7);
                }

                .zan-dialog__container {
                    position: fixed;
                    left: 50%;
                    top: 50%;
                    transform: translate(-50%, -50%);
                    background: #f8f8f8;
                    transition: all 0.4s ease;
                    z-index: 9999;
                    border-radius: 20px;
                }

                .container {
                    position: relative;
                    width: 300px;
                    height: 250px;
                    border-radius: 20px;
                    margin: 0 auto;
                }

                .title {
                    font-size: 23px;
                    color: #333;
                    font-weight: bold;
                    text-align: center;
                    padding: 25px 0 0 0;
                    margin-bottom: 20px;
                }

                .concent {
                    font-size: 20px;
                    color: #333;
                    font-weight: bold;
                    text-align: center;
                }

                .btnFlex {
                    position: absolute;
                    bottom: 15px;
                    width: 100%;
                    display: flex;
                    align-items: center;
                }

                .resetPay {
                    font-size: 18px;
                    width: 50%;
                    height: 50px;
                    line-height: 50px;
                    text-align: center;
                    font-weight: bold;
                }

                .cancel {
                    font-size: 18px;
                    width: 50%;
                    height: 50px;
                    line-height: 50px;
                    text-align: center;
                    font-weight: bold;
                    color: #ff2f4e;
                }

                .hideStyle {
                    display: none;
                }

                .showStyle {
                    display: block;
                }
            </style>
    </head>
    <body>
        <div style="height:500px"></div>
        <div class="toast_box">
            <p id="toast"></p>
        </div>
        <div id='replace'></div>
        <div id="zan-dialog-mask" class="hideStyle">
            <div class="zan-dialog-mask hideStyle">
                <div class="zan-dialog__container">
                    <div class="container">
                        <div class="title">提示</div>
                        <div class="concent">
                            <div>订单支付未完成,</div>
                            <div>是否重新支付?</div>
                        </div>
                        <div class="btnFlex">
                            <div class="resetPay" onclick="resetPay()">重新支付</div>
                            <div class="cancel" onclick="cancel()">取消</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- uniapp的引用 -->
    <!-- <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script> -->
    <script type="text/javascript" src="https://unpkg.com/@dcloudio/[email protected]/index.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script type="text/javascript">
        document.addEventListener('UniAppJSBridgeReady', function() {
            let plus=false;
            uni.postMessage({
                data: {
                    action: '',
                    payResult: 0
                }
            });
            
            //获取数据进行赋值
            getUrlParam()
            toast('正在跳转支付页面..', 1200)
            //获取元素 其实以下得看自身情况需求来做更改 但大体就是使用 window.location.search.substring(1)截取到凭借的参数 然后使用decodeURIComponent方法进行转译
            let replace = document.getElementById('replace')
            function getUrlParam() {
                // 取得url中?后面的字符
                let query = window.location.search.substring(1);
                // let pair, pairSplit, id, token,requestUrl,userTerminal;
                // // 把参数按&拆分成数组3
                // // let param_arr = query.split("=");
                let param_arr = query.substring(5);
//跳转webview需要转换参数,不然webview会报错
                let htmlSplit = param_arr.replace(/\%3F/g, "?")
                let htmlSplitSpace = htmlSplit.replace(/\%20/g, " ")
                let htmlSplitSpaceF = htmlSplitSpace.replace(/\%3C/g, "<")
                let htmlSplitSpaceFR = htmlSplitSpaceF.replace(/\%3E/g, ">")
                let htmlSplitSpaceFf = htmlSplitSpaceFR.replace(/\%22/g, '"')
                let htmlSplitSpaceFf3 = htmlSplitSpaceFf.replace(/\%233/g, '#')
                // let htmlSplitSpaceFfPay = htmlSplitSpaceFf3.replace(/\%E7%AB%8B%E5%8D%B3%E6%94%AF%E4%BB%98/g, '立即支付')
                setTimeout(() => {
                let divForm = document.getElementsByTagName('divform')
                if (divForm.length) {
                    document.body.removeChild(divForm[0])
                }
                const div = document.createElement('divform');
                div.innerHTML = htmlSplitSpaceFf3;
                    document.body.appendChild(div);
                    document.forms[0].setAttribute('target', '_blank')
                    document.forms[0].submit();
                }, 500)
            }        
            function toast(text, time) {
                var toast = document.getElementById('toast');
                var toast_box = document.getElementsByClassName('toast_box')[0];
                toast.innerHTML = text;
                toast_box.style.animation = 'show 1.5s'
                toast_box.style.display = 'flex';
                setTimeout(() => {
                    toast_box.style.animation = 'hide 1.5s'
                    setTimeout(() => {
                        toast_box.style.display = 'none';
                    }, 1400)
                }, time)
            }
        });
    </script>
</html>
View Code

如有疑问,可联系随笔作者!!

标签:toast,uniapp,app,50%,box,replace,let,webview,document
From: https://www.cnblogs.com/yuan-xiaohai/p/17381196.html

相关文章

  • css 文字内容过长和下一个信息项重叠解决办法(uniapp)
    1.把固定高度注释掉,自动调整高度.cardBox{ padding:20rpx30rpx; .item{ //width:calc(100%-40rpx); //height:398rpx; padding:10rpx20rpx0rpx; margin-bottom:25rpx; border-radius:8rpx; box-shadow:0rpx4rpx16rpx2rpx#BDC0C6; background:......
  • Mybatis-Plus基本CRUD——通用Mapper
    BaseMapper接口MyBatis-Plus中的基本CRUD在内置的BaseMapper中都已得到了实现,我们可以直接使用,接口如下:/***Mapper继承该接口后,无需编写mapper.xml文件,即可获得CRUD功能*<p>这个Mapper支持id泛型</p>**@authorhubin*@since2016-01-23*/publicinter......
  • springboot集成下,mybatis的mapper代理对象究竟是如何生成的
    springboot集成下,mybatis的mapper代理对象究竟是如何生成的 前情回顾Mybatis源码解析-mapper代理对象的生成,你有想过吗,我们讲到了mybatis操作数据库的流程:先创建SqlSessionFactory,然后创建SqlSession,然后再创建获取mapper代理对象,最后利用mapper代理对象完成数据库......
  • 记一次使用laravel-snappy把g2和plotly.js图生成pdf踩坑经历
    最近有一个需求,需要把页面上的echarts等js组件生成的图表放到pdf中。使用laravel框架,找到了laravel-snappy这个包,其底层是使用wkhtmltopdf来生成pdf。因为有把数据点都画到箱型图上的需求,找到了plotly.js这个组件,功能还是很强大的,可以轻松实现如下的效果 页面都没问题,然使用lar......
  • 通话记录生成器app软件下载,通话记录生成器最新版,电销通话记录生成器
    铁牛通话记录生成器haosen111.com是可以一键自动批量生成通话记录app。铁牛通话记录生成器,复制这些字母串到手机浏览器去打开,可以进来jp155.com,默认跟着软件上设置好的五步,从上往下顺着操作。“铁牛通话记录生成器下载”?在这里也可以jp1988.com铁牛通话记录生成器,前面的这些网都可......
  • APP和WEB的测试区别
    在功能测试时,要考虑手机应用的特性:1)手机屏幕尺寸偏小,所以手机应用一般就占满了全屏,因此要考虑手机在前后端切换时被测试应用在资源使用时的优先级变化情况;还要考虑手机横竖屏切换时的测试2)手机是智能移动终端,因此要考虑网络运营商、网络信号强弱、网络信号有无、被测试应用在低电......
  • 通话记录生成器安卓版下载,手机通话记录生成器app,一键批量生成通话记录
    铁牛通话记录生成器是可以批量自动生成通话记录的app软件。如何“铁牛通话记录生成器下载”?这段话的这几个网都可以,复制它们到手机浏览器打开就可以。通话记录生成器下载jp155.com,通话记录生成器安卓版下载jp1988.com,下载通话记录生成器app,体验通话记录生成器在线,了解通话记录生成......
  • Hive表 Parquet压缩 , Gzip,Snappy,uncompressed 效果对比
     创建两张表,通过一种是parquet,一种使用parquetsnappy压缩创建表使用snappyCREATEEXTERNALTABLEIFNOTEXISTStableName(xxxstring)partitionedby(pt_xvcstring)ROWFORMATDELIMITEDFIELDSTERMINATEDBY'\001'STOREDASPARQUETTBLPROPERTIES('parquet.compre......
  • Could not create ActionMapper: WebWork will *not* work!
    CouldnotcreateActionMapper:WebWorkwill*not*work!解决方法:将webwork.properties的webwork.objectFactory=springwebwork.objectFactory.spring.autoWire=name 两行去掉就可以了......
  • application/x-www-form-urlencoded & multipart/form-data & text/plain
    FORM元素的enctype属性指定了表单数据向服务器提交时所采用的编码类型  我们知道在通过POST方式向服务器发送AJAX请求时最好要通过设置请求头来指定为application/x-www-form-urlencoded编码类型。知道通过表单上传文件时必须指定编码类型为"multipart/form-data"。而text/plain......