【微信支付】H5 JSAPI WeixinJSBridge内含的几种接口调用方法

【微信支付】H5 JSAPI WeixinJSBridge内含的几种接口调用方法

注意:以下所有的接口,请在微信中测试


在这里插入图片描述


以下代码可创建空html,粘贴后直接在微信浏览器中进行测试。
注意引入jweixin-1.3.2.js文件

<!DOCTYPE html>
<html>
 <head>
 <title>微信WeixinJSBridge API</title> 
 <meta charset="utf-8" /> 
 <script type="text/javascript">
	(function(){
	var a=document.getElementsByTagName("html")[0];
	window.Session={appDomain:a.getAttribute("data-app-domain")||"",staticDomain:a.getAttribute("data-static-domain")||""}
	})();
	 
	window.registNS=function(fullNS,isIgnorSelf){
	var reg=/^[_$a-z]+[_$a-z0-9]*/i;var nsArray=fullNS.split(".");
	var sEval="";
	var sNS="";
	var n=isIgnorSelf?nsArray.length-1:nsArray.length;
	for(var i=0;i<n;i++){
	  if(!reg.test(nsArray[i])){throw new Error("Invalid namespace:"+nsArray[i]+"");
	  return
	  }
	  if(i!=0){sNS+="."}
	  sNS+=nsArray[i];
	  sEval+="if(typeof("+sNS+")=='undefined') "+sNS+"=new Object();
	  else "+sNS+";"
	}
	if(sEval!=""){
	return eval(sEval)
	}
	return{}
	 
	};
 
</script> 
 </head> 
 <body>
 <section class="mod-page-body"> 
  <div class="mod-page-main wordwrap clearfix"> 
  <div class="mod-pageheader"></div> 
  <div class="mod-pagecontent"> 
   <div class="mod-weixinjsapi"> 
   <div class="x-desc">
    微信客户端自带的Js Api:WeixinJSBridge
   </div> 
   <div id="WeixinJsApi"> 
    <input type="button" id="imagePreview" value="图片预览" /> 
    <input type="button" id="profile" value="查看profile" /> 
    <a href="weixin://profile/gh_412d74fbb474">企业微信小助手</a> 
    <input type="button" id="shareWeibo" value="分享微博" /> 
    <input type="button" id="shareFB" value="分享facebook" /> 
    <input type="button" id="addContact" value="添加联系人" /> 
    <input type="button" id="scanQRCode" value="扫描二维码" /> 
    <input type="button" id="jumpToBizProfile" value="跳转到指定公众账号页面" /> 
    <input type="button" id="toggleMenuBtn" value="隐藏右上角按钮" /> 
    <input type="button" id="toggleToolbar" value="隐藏底部导航栏" /> 
    <input type="button" id="getNetType" value="获取网络状态" /> 
    <input type="button" id="closeWindow" value="关闭" /> 
    <input type="button" id="getBrandWCPayRequest" value="发起公众号微信支付" /> 
    <input type="button" id="setPageState" value="设置页面状态" /> 
    <input type="button" id="sendEmail" value="发邮件" /> 
    <input type="button" id="openSpecificView" value="微信团队打开webView,跳到指定页面" /> 
    <input type="button" id="getCanIAPPay" value="getCanIAPPay" /> 
    <input type="button" id="getBrandIAPPayRequest" value="发起公众号IAP支付" /> 
    <input type="button" id="openUrlByExtBrowser" value="用safari打开指定链接" /> 
    <input type="button" id="openProductView" value="跳转微信商品页" /> 
    <input type="button" id="openLocation" value="查看地理位置" /> 
    <input type="button" id="timelineCheckIn" value="朋友圈签到" /> 
    <input type="button" id="getBrandWCPayCreateCreditCardRequest" value="开通微信信用卡" /> 
    <input type="button" id="geoLocation" value="获取地理位置" /> 
    <input type="button" id="getInstallState" value="获取某app是否安装" /> 
    <input type="button" id="editAddress" value="公众号编辑收货地址" /> 
    <input type="button" id="getLatestAddress" value="公众号获取最近的收货地址" /> 
    <input type="button" id="launch3rdApp" value="启动第三方APP" /> 
    <input type="button" id="jumpWCMall" value="跳转微信商品购买界面" /> 
    <input type="button" id="addEmoticon" value="添加表情" /> 
    <input type="button" id="cancelAddEmoticon" value="取消下载某表情" /> 
    <input type="button" id="hasEmoticon" value="查询是否存在某表情" /> 
   </div> 
   </div> 
  </div> 
  </div> 
 </section> 
 <script>
function onBridgeReady() {
  WeixinJSBridge.on('menu:share:appmessage', function(argv) 
  {
    WeixinJSBridge.invoke('sendAppMessage',{
          "link":"http://m.exmail.qq.com/",
          "desc":"desc",
          "title":"title for WeiXinJsBridge"
    }, function(res) {
      WeixinJSBridge.log(res.err_msg);
    });
  });
  WeixinJSBridge.on('menu:share:timeline', function(argv) 
  {
  WeixinJSBridge.invoke("shareTimeline",{
    "link":"http://m.exmail.qq.com",
    "img_url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/logo1ca3fe.png",
    "img_width":"172",
    "img_height":"40",
    "desc":"i am description",
    "title":"just test from WeixinJsBridge"
    },
    function(e){
    alert(e.err_msg);
    })
  });
}
 
if (typeof WeixinJSBridge === "undefined"){
  if (document.addEventListener){
    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  }
}else{
  onBridgeReady();
}
 
  var menuHidden=!1,toolbarHidden=!1,netType={"network_type:wifi":"wifi网络","network_type:edge":"非wifi,包含3G/2G","network_type:fail":"网络断开连接","network_type:wwan":"2g或者3g"};
  document.addEventListener("WeixinJSBridgeReady",function(){
      document.getElementById("imagePreview").addEventListener(
      "click",function(){
                WeixinJSBridge.invoke("imagePreview",{
        "urls":[
        "http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/logo1ca3fe.png",
        "http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png",
        "http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_workStyle1ca3fe.png"
        ],
        "current":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png"
        })
            },!1),
    document.getElementById("profile").addEventListener(
      "click",function(){
        alert("profile clicked");
        WeixinJSBridge.invoke("profile",{
          "username":"gh_412d74fbb474",
          "nickname":"企业微信小助手"  
        })
      },!1),
    document.getElementById("shareWeibo").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("shareWeibo",{
          "type":"link",
          "link":"http://m.exmail.qq.com"
        },
        function(e){
          alert(e.err_msg);
        })
      },!1),
    document.getElementById("shareFB").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("shareFB",{
          "link":"http://m.exmail.qq.com"
        })
      },!1),
    document.getElementById("scanQRCode").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("scanQRCode",{
        })
      },!1),
    document.getElementById("addEmoticon").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("addEmoticon",{
          "url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png",
          "thumb_url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/logo1ca3fe.png"
 
        },
        function(e){
                    alert(e.err_msg);
                })
      },!1),
    document.getElementById("cancelAddEmoticon").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("cancelAddEmoticon",{
          "url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png"
 
        },
        function(e){
                    alert(e.err_msg);
                })
      },!1),
    document.getElementById("hasEmoticon").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("hasEmoticon",{
          "url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png"
 
        },
        function(e){
                    alert(e.err_msg);
                })
      },!1),
    document.getElementById("addContact").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("addContact",{
          "webtype":"1",
          "username":"gh_412d74fbb474"
        },
        function(e){
          alert(e.err_msg);
        })
      },!1),
    document.getElementById("jumpToBizProfile").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("jumpToBizProfile",{
          "tousername":"gh_2248a2ade13e"
        },
        function(e){
          alert(e.err_msg);
        })
      },!1),
    document.getElementById("toggleMenuBtn").addEventListener(
      "click",function(){
        menuHidden?
        (WeixinJSBridge.call("showOptionMenu"),menuHidden=!1,this.value="隐藏右上角按钮")
        :
        (WeixinJSBridge.call("hideOptionMenu"),menuHidden=!0,this.value="显示右上角按钮")
      },!1),
    document.getElementById("toggleToolbar").addEventListener(
      "click",function(){
        toolbarHidden?
        (WeixinJSBridge.call("showToolbar"),toolbarHidden=!1,this.value="隐藏底部导航栏")
        :
        (WeixinJSBridge.call("hideToolbar"),toolbarHidden=!0,this.value="显示底部导航栏")
      },!1),
    document.getElementById("getNetType").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("getNetworkType",{},
          function(e){
            alert(netType[e.err_msg])
          })
      },!1),
     document.getElementById("closeWindow").addEventListener(
            "click",function(){
                WeixinJSBridge.invoke("closeWindow",{},function(e){})
            },!1),
    document.getElementById("getBrandWCPayRequest").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getBrandWCPayRequest",{
        "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
        "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
        "nonceStr" : "adssdasssd13d", //随机串
        "package" :
        "body=xxx&fee_type=1&input_charset=GBK&notify_url=http&out_trade_no=16642817866003386000&partner=1900000109&return_url=http&spbill_create_ip=127.0.0.1&total_fee=1&sign=273B7EEEE642A8E41F27213D8517E0E4", //扩展字段,由商户传入
        "signType" : "SHA1", //微信签名方式:sha1
        "paySign" : "b737015b5b1eabe5db580945a07eac08c7bb55f8" //微信签名
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("setPageState").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("setPageState",{
        "state" : "1"
        })
      },!1),
 
    document.getElementById("sendEmail").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("sendEmail",{
        "title" : "title!",
        "content" : "i am an Email!", //时间戳 这里随意使用了一个值
        },
        function(e){
    //     alert(e.err_msg)
        })
      },!1),
    document.getElementById("openSpecificView").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("openSpecificView",{
        "specificview" : "contacts"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getCanIAPPay").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getCanIAPPay",{ },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getBrandIAPPayRequest").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getBrandIAPPayRequest",{
        "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
        "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
        "nonceStr" : "adssdasssd13d", //随机串
        "package" : "bankType=CITIC_CREDIT&bankName=%e4%b8%ad%e4%bf%a1%e9%93%b6%e8%a1%8c&sign=CF8922F49431FFE8A1834D0B32B25CE3",
        //扩展字段,由商户传入
        "signType" : "SHA1", //微信签名方式:sha1
        "paySign" : "1e6f13f78ca0ec43fbb80899087f77568af66987" //微信签名
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("openLocation").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("openProductView",{  
        "latitude" : 23.113, //纬度
        "longitude" : 113.23, //经度
        "name" : "TIT创意园", //POI名称
        "address" : "广州市海珠区新港中路397号", //地址
        "scale" : 14, //地图缩放级别
        "infoUrl" : "http://weixin.qq.com/", //查看位置界面底部的超链接        
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("timelineCheckIn").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("timelineCheckIn",{  
        "img_url": "http://mmsns.qpic.cn/mmsns/RLllkTm3DUdV24xbZnKicx9jJWxXI0Bq84zzbtibGuRyk/0", // 分享到朋友圈的缩略图
        "img_width": "640", // 图片的长度
        "img_height": "640", // 图片高度
        "link": "http://news.qq.com/zt2012/cxkyym/index.htm", // 连接地址
        "desc": "这个是描述啊啊", // 描述
        "title": "朝鲜称中国渔船越界捕捞", // 分享标题
        "latitude" : 23.113, //纬度
        "longitude" : 113.23, //经度
        "poiId" : "dianping_2331037", //商户id
        "poiName" : "TIT创意园", //POI名称
        "poiAddress" : "广州市海珠区新港中路397号", //地址
        "poiScale" : 14, //地图缩放级别
        "poiInfoUrl" : "http://weixin.qq.com/" //查看位置界面底部的超链接
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("geoLocation").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("geoLocation",{  
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getBrandWCPayCreateCreditCardRequest").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getBrandWCPayCreateCreditCardRequest",{ 
        "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
        "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
        "nonceStr" : "adssdasssd13d", //随机串
        "package" : "bankType=CITIC_CREDIT&bankName=%e4%b8%ad%e4%bf%a1%e9%93%b6%e8%a1%8c&sign= CF8922F49431FFE8A1834D0B32B25CE3",
        //扩展字段,由商户传入
        "signType" : "SHA1", //微信签名方式:sha1
        "paySign" : "1e6f13f78ca0ec43fbb80899087f77568af66987" //微信签名
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getInstallState").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getInstallState",{  
          "packageUrl":"teamcircle://"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("openProductView").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("openProductView",{  
          "productInfo":"json"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getLatestAddress").addEventListener(
        "click",function(){
        WeixinJSBridge.invoke("getLatestAddress",{ 
          "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
          "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
          "nonceStr" : "adssdasssd13d", //随机串
          "signType" : "SHA1", //微信签名方式:sha1
          "addrSign" : "b737015b5b1eabe5db580945a07eac08c7bb55f8", //微信签名
          "scope"  : "snsapi"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("editAddress").addEventListener(
        "click",function(){
        WeixinJSBridge.invoke("editAddress",{  
          "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
          "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
          "nonceStr" : "adssdasssd13d", //随机串
          "signType" : "SHA1", //微信签名方式:sha1
          "addrSign" : "b737015b5b1eabe5db580945a07eac08c7bb55f8", //微信签名
          "scope"  : "snsapi"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("launch3rdApp").addEventListener(
        "click",function(){
        WeixinJSBridge.invoke("launch3rdApp",{ 
          "appId" : "wx5823bf96d3bd56c7", //公众号名称,由商户传入
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("jumpWCMall").addEventListener(
        "click",function(){
        WeixinJSBridge.invoke("jumpWCMall",{  
          "appId" : "wx5823bf96d3bd56c7", //公众号名称,由商户传入
          "funcId":"1000"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("openUrlByExtBrowser").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("openUrlByExtBrowser",{
        "url" : "http://m.exmail.qq.com"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1)
    }
  );
</script> 
 </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Uniapp是一种跨平台的开发框架,可以用于同时开发iOS、Android和H5应用。而H5是指在网页上运行的应用程序。微信支付JSAPI是微信提供的一种支付接口,通过调用JSAPI接口,可以在H5应用中实现微信支付功能。 在Uniapp中使用微信支付JSAPI,可以通过以下步骤进行操作: 1. 首先,在Uniapp项目中引入微信支付的相关配置文件和SDK库文件。 2. 在需要使用微信支付的页面中,引入微信支付的JS文件,并初始化微信支付参数。支付参数包括商户号、appid、签名等信息。 3. 创建统一下单接口的后端处理程序,用于生成预支付订单并返回支付预处理参数给前端。 4. 在支付页面中,调用微信支付JSAPI的方法,传入预支付处理参数和支付回调方法。 5. 当用户点击支付按钮后,会弹出微信支付界面,用户可以选择支付方式进行支付操作。 6. 支付成功后,微信会将支付结果返回给前端,并通过支付回调方法进行处理,可以展示支付成功的提示信息和更新订单状态等操作。 需要注意的是,使用微信支付JSAPI需要在微信开放平台上注册并申请相关的权限。同时,在使用过程中,还需要确保支付参数的正确性、支付安全性和业务逻辑的完善性。 总之,Uniapp可以很好地支持H5应用中的微信支付JSAPI,通过合理的配置和调用,可以方便地在H5应用中实现微信支付功能,为用户提供更加便捷的支付体验。 ### 回答2: Uniapp是一款跨平台的应用开发框架,可以让开发者使用Vue.js语法来开发同时兼容多个平台的应用程序。Uniapp支持在H5平台中使用微信支付JSAPI。 微信支付JSAPI是微信提供的一组用于在网页中实现微信支付功能的JavaScriptAPI接口。通过调用微信支付JSAPI,开发者可以在H5页面中调起微信支付功能,用户可以使用微信支付完成支付操作。 在Uniapp中使用微信支付JSAPI,首先需要引入微信支付的JS文件。可以在页面的头部引入:<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>。 然后,在需要使用微信支付的地方,可以通过uni.request方法向服务器请求获取支付参数。获取到支付参数后,可以使用wx.requestPayment方法调起微信支付界面,并传入支付参数。支付成功后,微信会返回支付结果给开发者的回调函数,开发者可以在回调函数中处理支付结果。 需要注意的是,在使用微信支付JSAPI的过程中需要保证支付参数的安全性,避免支付参数被恶意篡改。通常可以在服务器端生成支付参数,并通过服务器端返回给前端,以确保支付参数的安全性。 总结起来,Uniapp可以在H5平台中使用微信支付JSAPI来实现微信支付功能,开发者需要引入微信支付的JS文件,通过uni.request方法获取支付参数,调用wx.requestPayment方法调起支付,处理支付结果的回调函数,并确保支付参数的安全性。 ### 回答3: Uniapp 是一款跨平台开发框架,可以同时开发小程序、H5 和APP。Uniapp 提供了对微信支付的支持,可以使用微信支付的 JSAPI(JavaScript API)来进行支付功能的开发。 Uniapp H5 微信支付 JSAPI 的实现过程如下: 1. 首先,需要在微信支付商户平台上注册并获取到自己的商户号(mch_id),同时生成随机字符串(nonce_str)和当前时间戳(timestamp)。 2. 接下来,在前端页面中引入微信支付的 JS 文件,可以通过在页面头部添加以下代码进行引入: ``` <script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script> ``` 3. 在页面中设置要支付的订单信息,包括订单号(out_trade_no)、订单总金额(total_fee)等,并将这些信息以及商户号、随机字符串和时间戳等传递给后端服务器。 4. 后端服务器在接收到前端传递的支付信息后,根据微信支付 API 的要求,生成签名(sign)并返回给前端。 5. 前端页面收到后端返回的签名后,调用微信支付的 JSAPI 方法,传入订单信息和签名等参数,即可发起支付请求。 6. 微信客户端会弹出支付窗口,用户输入密码或进行指纹验证后,支付完成。 注意事项: - 在使用 Uniapp 进行微信支付开发时,需要确保页面已经获得了微信公众号的授权,因为微信支付是需要通过公众号的权限进行的。 - 需要合理处理支付结果的回调,根据支付结果进行相应的处理,如跳转到支付成功页面或给予支付失败的提示等。 通过使用 Uniapp H5 微信支付 JSAPI,我们可以方便地在 Uniapp 框架下进行微信支付的开发,实现支付功能的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值