飞道的博客

uniapp微信小程序外壳内联H5实现支付

528人阅读  评论(0)

业务场景:用户有现成的微信H5应用(有微信支付)。用户想要一个一摸一样的小程序版本,但是又不想高成本去重新开发,所以可以考虑采用小程序的web-view组件内联现有的微信H5应用(哇简直不要再偷懒了!)简直就是分分钟搞定的事!

 是不是太简单了?给客户好心免费搞都行了!

但是这之中有个问题!因为用户线上微信H5应用涉及了微信支付功能,所以在小程序内联它后小程序中点击支付是无反应的!因为整体毕竟是小程序,支付需要走小程序的支付api

应对方案:必然涉及了H5与小程序的通信交互!

首先来放出该组件的文档:

官方:web-view | 微信开放文档

uniapp:web-view | uni-app官网

 

 这里划重点!

首先我创建一个uniapp-H5、uniapp-小程序的示例:

 wx_h5:


  
  1. <template>
  2. <view class="content">
  3. <image class="logo" src="/static/logo.png"> </image>
  4. <view class="text-area">
  5. <text class="title">{{title}} </text>
  6. </view>
  7. <button @click="setMsg()">向小程序发送消息 </button>
  8. <view>{{msg}} </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data( ) {
  14. return {
  15. title: 'Hello',
  16. msg: ''
  17. }
  18. },
  19. onLoad( ) {
  20. },
  21. methods: {
  22. setMsg( ){
  23. var that = this
  24. that. msg = '点击了'
  25. //发送消息给小程序(只有在小程序页面销毁、后退、分享时才会接收到该消息)
  26. that. wx. jweixin. miniProgram. postMessage({ data: '来自h5的消息'})
  27. //告诉小程序跳转小程序指定路径(没什么限制,执行小程序直接接收通信跳转指定小程序页面)
  28. /*
  29. that.wx.jweixin.miniProgram.navigateTo({
  30. url:'../index1/index1?data='+'ok'
  31. })
  32. */
  33. //
  34. }
  35. }
  36. }
  37. </script>
  38. <style>
  39. .content {
  40. display: flex;
  41. flex-direction: column;
  42. align-items: center;
  43. justify-content: center;
  44. }
  45. .logo {
  46. height: 200rpx;
  47. width: 200rpx;
  48. margin-top: 200rpx;
  49. margin-left: auto;
  50. margin-right: auto;
  51. margin-bottom: 50rpx;
  52. }
  53. .text-area {
  54. display: flex;
  55. justify-content: center;
  56. }
  57. .title {
  58. font-size: 36rpx;
  59. color: #8f8f94;
  60. }
  61. </style>

该H5引入了微信jssdk。

H5引入:

<script type="text/javascript" src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>

uniapp-H5引入:

npm install weixin-js-sdk --sava

这里我们是uniapp-H5,安装后创建一个wx.js

 wx.js


  
  1. let jweixin = require( 'jweixin-module');
  2. export default {
  3. jweixin:jweixin,
  4. }

main.js插入


  
  1. import wechat from './common/wx.js'
  2. Vue. prototype. wx = wechat;

 

具体api功能去查看文档:概述 | 微信开放文档

看上述代码调用postMessage方法可以给小程序发送消息,但是他有个弊端,就是点击后只有小程序页面(后退、组件销毁、分享)才会回调接收到该消息。这很不优雅,总不能点击支付没反应然后主动退出页面或者销毁或者分享才开始跑支付吧?

 

解决方案:

采用通知小程序跳转页面携带订单参数,小程序接收直接跳转指定的页面携带该参数,跳转到的页面接收参数并主动执行小程序支付api!


  
  1. this. wx. jweixin. miniProgram. navigateTo({
  2. url: '../index1/index1?data='+ 'ok'
  3. })

注意这里有坑!url可别写/pages/index1/index1,无反应的!

完整H5代码(index.vue)


  
  1. <template>
  2. <view class="content">
  3. <image class="logo" src="/static/logo.png"> </image>
  4. <view class="text-area">
  5. <text class="title">{{title}} </text>
  6. </view>
  7. <button @click="setMsg()">向小程序发送消息 </button>
  8. <view>{{msg}} </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data( ) {
  14. return {
  15. title: 'Hello',
  16. msg: ''
  17. }
  18. },
  19. onLoad( ) {
  20. },
  21. methods: {
  22. setMsg( ){
  23. var that = this
  24. that. msg = '点击了'
  25. //发送消息给小程序(只有在小程序页面销毁、后退、分享时才会接收到该消息)
  26. //that.wx.jweixin.miniProgram.postMessage({data:'来自h5的消息'})
  27. //告诉小程序跳转小程序指定路径(没什么限制,执行小程序直接接收通信跳转指定小程序页面)
  28. that. wx. jweixin. miniProgram. navigateTo({
  29. url: '../index1/index1?data='+ 'ok'
  30. })
  31. //
  32. }
  33. }
  34. }
  35. </script>
  36. <style>
  37. .content {
  38. display: flex;
  39. flex-direction: column;
  40. align-items: center;
  41. justify-content: center;
  42. }
  43. .logo {
  44. height: 200rpx;
  45. width: 200rpx;
  46. margin-top: 200rpx;
  47. margin-left: auto;
  48. margin-right: auto;
  49. margin-bottom: 50rpx;
  50. }
  51. .text-area {
  52. display: flex;
  53. justify-content: center;
  54. }
  55. .title {
  56. font-size: 36rpx;
  57. color: #8f8f94;
  58. }
  59. </style>

完事以后编辑成发布版,部署上线。

然后小程序使用web-view组件内联该上线地址(小程序项目的index.vue)


  
  1. <template>
  2. <view class="content">
  3. <web-view src="http://h5.com" @message="setMsg"> </web-view>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data( ) {
  9. return {
  10. title: 'Hello'
  11. }
  12. },
  13. onLoad( ) {
  14. },
  15. onShareAppMessage( ) {
  16. return{
  17. }
  18. },
  19. methods: {
  20. setMsg( e){
  21. console. log(e)
  22. }
  23. }
  24. }
  25. </script>
  26. <style>
  27. .content {
  28. display: flex;
  29. flex-direction: column;
  30. align-items: center;
  31. justify-content: center;
  32. }
  33. .logo {
  34. height: 200rpx;
  35. width: 200rpx;
  36. margin-top: 200rpx;
  37. margin-left: auto;
  38. margin-right: auto;
  39. margin-bottom: 50rpx;
  40. }
  41. .text-area {
  42. display: flex;
  43. justify-content: center;
  44. }
  45. .title {
  46. font-size: 36rpx;
  47. color: #8f8f94;
  48. }
  49. </style>

我们打开小程序进行测试:

 

 接下来该页面只需要接收参数直接onload调支付就好了!

支付成功回调的事就看业务去处理,可以说返回上一页携带参数修改web-view组件src地址到H5的支付后页面完成整个支付流程。

虽然这个方案很别扭,但是确实能降低成本和周期。

当然该方法适合的不单单是支付,比如小程序内联H5 需要实现小程序的一些功能,必然需要H5与小程序通信,以下是我总结的方案。

小程序通信H5:通过web-view组件src地址携带参数。

H5通信小程序:通过上述发送跳转小程序页面命令携带参数。

关于H5判断是在微信内打开还是内联小程序中打开判断(处理各自的支付流程业务)

 

 


  
  1. var ua = navigator. userAgent. toLowerCase();
  2. if(ua. match( /MicroMessenger/i)== "micromessenger") {
  3. //ios的ua中无miniProgram,但都有MicroMessenger(表示是微信浏览器)
  4. that. wx. jweixin. miniProgram. getEnv( (res)=>{
  5. if (res. miniprogram) {
  6. //在微信内,在小程序内。
  7. console. log( 1)
  8. console. log( "小程序的支付业务")
  9. that. wx. jweixin. miniProgram. navigateTo({
  10. url: '../index1/index1?data='+ 'ok'
  11. })
  12. return
  13. } else{
  14. //在微信内,不在小程序内。
  15. console. log( 2)
  16. console. log( "微信H5的支付业务")
  17. return
  18. }
  19. })
  20. } else{
  21. //不在微信内。
  22. console. log( 3)
  23. console. log( "微信以外的业务")
  24. return
  25. }

 这样的话微信H5应用就走自己原来的支付业务,小程序内联这个H5就走这个特殊的支付业务,微信以外的走以外的业务啦!

希望能帮助大家避免深坑。

点个关注不迷路~


转载:https://blog.csdn.net/weixin_47723549/article/details/125442473
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场