uniapp app跳转至支付宝然后返回app

有需求:用支付宝支付时(第一次支付需授权)跳转至支付宝授权,然后再返回app

跳转支付宝:

   <button @click="toZFB">支付宝</button> 
  toZFB() { let urls
= 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=你的支付宝appid&scope=auth_base&redirect_uri=回调网址(用于跳转返回app)&state=init'
     //auth_base 静默授权(还有非静默授权代码,请百度)

     urls = encodeURIComponent(urls)
        plus.runtime.openURL('alipay://platformapi/startapp?appId=20000067&url=' + urls)
     }        

 

跳转到支付宝授权之后会打开redirect_uri的回调网页 , 这是需要从H5网页返回到app:

            openApp() {
          this.isIos = uni.getSystemInfoSync().platform //判断是否安卓、ios
if (this.isIos == 'android') { let ifr = document.createElement("iframe"); ifr.src = "hbuilder://"; // 这里的hbuilder在manifest.json中配置->“App常用其它设置” -> “Android设置” -> “UrlSchemes” 项中进行设置 ifr.style.display = "none"; document.body.appendChild(ifr); } else { window.location = "https://apps.apple.com/cn/app/idxxxxxxx"//你的苹果应用app链接 } }

UrlSchemes设置文档

注:设置urlschemes后需打包安装到手机,访问回调地址点击按钮才会跳转到app(安装到手机会给手机写入urlschemes)

 

 

如果想要打开APP后直接到之前的页面,可用官方文档中 UrlSchemes设置文档 的方法,也可以在跳转至支付宝之前设置手机粘贴板内容,然后在打开app时读取手机粘贴板的内容(如路径、相关数据信息)直接跳转(建议写在App.vue中的onShow生命周期中,且读取之后清空手机粘贴板内容)

 

posted @ 2023-02-27 16:54  zhangPooo  阅读(1895)  评论(0编辑  收藏  举报