WeixinJSBridge API使用实例

淡淡的烟草味﹌ 2022-06-18 06:52 410阅读 0赞
  1. <**span style=”color: rgb(51, 51, 51); font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 21px; background-color: rgb(246, 251, 255);”>这篇文章主要介绍了微信WeixinJSBridge API使用实例,本文直接给出HTML代码,代码中包含了很多实用功能,如图片预览、分享到微博、隐藏右上角按钮、获取网络状态、发起公众号微信支付等内容,需要的朋友可以参考下</span>**

[html] view plain copy

  1. </**pre><**pre name=”code” class=”html”><!DOCTYPE html>
  2. <**html**>
  3. <**head**>
  4. <**title**>微信WeixinJSBridge API</**title**>
  5. <**meta charset=”utf-8” />**
  6. <**script type=”text/javascript”>**
  7. (function(){
  8. var a=document.getElementsByTagName(“html”)[0];
  9. window.Session={appDomain:a.getAttribute(“data-app-domain”)||””,staticDomain:a.getAttribute(“data-static-domain”)||””}
  10. })();
  11. window.registNS=function(fullNS,isIgnorSelf){
  12. var reg=/^[_$a-z]+[_$a-z0-9]*/i;var nsArray=fullNS.split(“.”);
  13. var sEval=””;
  14. var sNS=””;
  15. var n=isIgnorSelf?nsArray.length-1:nsArray.length;
  16. for(var i=0;i<**n**;i++){
  17. if(!reg.test(nsArray[i])){throw new Error(“Invalid namespace:”+nsArray[i]+””);
  18. return
  19. }
  20. if(i!=0){sNS+=”.”}
  21. sNS+=nsArray[i];
  22. sEval+=”if(typeof(“+sNS+”)==’undefined’) “+sNS+”=new Object();
  23. else “+sNS+”;”
  24. }
  25. if(sEval!=””){
  26. return eval(sEval)
  27. }
  28. return{}
  29. };
  30. </**script**>
  31. </**head**>
  32. <**body**>
  33. <**section class=”mod-page-body”>**
  34. <**div class=”mod-page-main wordwrap clearfix”>**
  35. <**div class=”mod-pageheader”></div**>
  36. <**div class=”mod-pagecontent”>**
  37. <**div class=”mod-weixinjsapi”>**
  38. <**div class=”x-desc”>**
  39. 微信客户端自带的Js Api:WeixinJSBridge
  40. </**div**>
  41. <**div id=”WeixinJsApi”>**
  42. <**input type=”button” id=”imagePreview” value=”图片预览” />**
  43. <**input type=”button” id=”profile” value=”查看profile” />**
  44. <**a href=”weixin://profile/gh_412d74fbb474”>企业微信小助手</a>**
  45. <**input type=”button” id=”shareWeibo” value=”分享微博” />**
  46. <**input type=”button” id=”shareFB” value=”分享facebook” />**
  47. <**input type=”button” id=”addContact” value=”添加联系人” />**
  48. <**input type=”button” id=”scanQRCode” value=”扫描二维码” />**
  49. <**input type=”button” id=”jumpToBizProfile” value=”跳转到指定公众账号页面” />**
  50. <**input type=”button” id=”toggleMenuBtn” value=”隐藏右上角按钮” />**
  51. <**input type=”button” id=”toggleToolbar” value=”隐藏底部导航栏” />**
  52. <**input type=”button” id=”getNetType” value=”获取网络状态” />**
  53. <**input type=”button” id=”closeWindow” value=”关闭” />**
  54. <**input type=”button” id=”getBrandWCPayRequest” value=”发起公众号微信支付” />**
  55. <**input type=”button” id=”setPageState” value=”设置页面状态” />**
  56. <**input type=”button” id=”sendEmail” value=”发邮件” />**
  57. <**input type=”button” id=”openSpecificView” value=”微信团队打开webView,跳到指定页面” />**
  58. <**input type=”button” id=”getCanIAPPay” value=”getCanIAPPay” />**
  59. <**input type=”button” id=”getBrandIAPPayRequest” value=”发起公众号IAP支付” />**
  60. <**input type=”button” id=”openUrlByExtBrowser” value=”用safari打开指定链接” />**
  61. <**input type=”button” id=”openProductView” value=”跳转微信商品页” />**
  62. <**input type=”button” id=”openLocation” value=”查看地理位置” />**
  63. <**input type=”button” id=”timelineCheckIn” value=”朋友圈签到” />**
  64. <**input type=”button” id=”getBrandWCPayCreateCreditCardRequest” value=”开通微信信用卡” />**
  65. <**input type=”button” id=”geoLocation” value=”获取地理位置” />**
  66. <**input type=”button” id=”getInstallState” value=”获取某app是否安装” />**
  67. <**input type=”button” id=”editAddress” value=”公众号编辑收货地址” />**
  68. <**input type=”button” id=”getLatestAddress” value=”公众号获取最近的收货地址” />**
  69. <**input type=”button” id=”launch3rdApp” value=”启动第三方APP” />**
  70. <**input type=”button” id=”jumpWCMall” value=”跳转微信商品购买界面” />**
  71. <**input type=”button” id=”addEmoticon” value=”添加表情” />**
  72. <**input type=”button” id=”cancelAddEmoticon” value=”取消下载某表情” />**
  73. <**input type=”button” id=”hasEmoticon” value=”查询是否存在某表情” />**
  74. </**div**>
  75. </**div**>
  76. </**div**>
  77. </**div**>
  78. </**section**>
  79. <**script**>
  80. function onBridgeReady() {
  81. WeixinJSBridge.on(‘menu:share:appmessage’, function(argv)
  82. {
  83. WeixinJSBridge.invoke(‘sendAppMessage’,{
  84. “link”:”http://m.exmail.qq.com/“,
  85. “desc”:”desc”,
  86. “title”:”title for WeiXinJsBridge”
  87. }, function(res) {
  88. WeixinJSBridge.log(res.err_msg);
  89. });
  90. });
  91. WeixinJSBridge.on(‘menu:share:timeline’, function(argv)
  92. {
  93. WeixinJSBridge.invoke(“shareTimeline”,{
  94. “link”:”http://m.exmail.qq.com“,
  95. “img_url”:”http://rescdn.qqmail.com/bizmail/zh\_CN/htmledition/images/bizmail/v3/logo1ca3fe.png“,
  96. “img_width”:”172”,
  97. “img_height”:”40”,
  98. “desc”:”i am description”,
  99. “title”:”just test from WeixinJsBridge”
  100. },
  101. function(e){
  102. alert(e.err_msg);
  103. })
  104. });
  105. }
  106. if (typeof WeixinJSBridge === “undefined”){
  107. if (document.addEventListener){
  108. document.addEventListener(‘WeixinJSBridgeReady’, onBridgeReady, false);
  109. }
  110. }else{
  111. onBridgeReady();
  112. }
  113. var menuHidden=!1,toolbarHidden=!1,netType={“network_type:wifi”:”wifi网络”,”network_type:edge”:”非wifi,包含3G/2G”,”network_type:fail”:”网络断开连接”,”network_type:wwan”:”2g或者3g”};
  114. document.addEventListener(“WeixinJSBridgeReady”,function(){
  115. document.getElementById(“imagePreview”).addEventListener(
  116. “click”,function(){
  117. WeixinJSBridge.invoke(“imagePreview”,{
  118. “urls”:[
  119. “http://rescdn.qqmail.com/bizmail/zh\_CN/htmledition/images/bizmail/v3/logo1ca3fe.png“,
  120. “http://rescdn.qqmail.com/bizmail/zh\_CN/htmledition/images/bizmail/v3/icons\_features1ca3fe.png“,
  121. “http://rescdn.qqmail.com/bizmail/zh\_CN/htmledition/images/bizmail/v3/icons\_workStyle1ca3fe.png“
  122. ],
  123. “current”:”http://rescdn.qqmail.com/bizmail/zh\_CN/htmledition/images/bizmail/v3/icons\_features1ca3fe.png“
  124. })
  125. },!1),
  126. document.getElementById(“profile”).addEventListener(
  127. “click”,function(){
  128. alert(“profile clicked”);
  129. WeixinJSBridge.invoke(“profile”,{
  130. “username”:”gh_412d74fbb474”,
  131. “nickname”:”企业微信小助手”
  132. })
  133. },!1),
  134. document.getElementById(“shareWeibo”).addEventListener(
  135. “click”,function(){
  136. WeixinJSBridge.invoke(“shareWeibo”,{
  137. “type”:”link”,
  138. “link”:”http://m.exmail.qq.com“
  139. },
  140. function(e){
  141. alert(e.err_msg);
  142. })
  143. },!1),
  144. document.getElementById(“shareFB”).addEventListener(
  145. “click”,function(){
  146. WeixinJSBridge.invoke(“shareFB”,{
  147. “link”:”http://m.exmail.qq.com“
  148. })
  149. },!1),
  150. document.getElementById(“scanQRCode”).addEventListener(
  151. “click”,function(){
  152. WeixinJSBridge.invoke(“scanQRCode”,{
  153. })
  154. },!1),
  155. document.getElementById(“addEmoticon”).addEventListener(
  156. “click”,function(){
  157. WeixinJSBridge.invoke(“addEmoticon”,{
  158. “url”:”http://rescdn.qqmail.com/bizmail/zh\_CN/htmledition/images/bizmail/v3/icons\_features1ca3fe.png“,
  159. “thumb_url”:”http://rescdn.qqmail.com/bizmail/zh\_CN/htmledition/images/bizmail/v3/logo1ca3fe.png“
  160. },
  161. function(e){
  162. alert(e.err_msg);
  163. })
  164. },!1),
  165. document.getElementById(“cancelAddEmoticon”).addEventListener(
  166. “click”,function(){
  167. WeixinJSBridge.invoke(“cancelAddEmoticon”,{
  168. “url”:”http://rescdn.qqmail.com/bizmail/zh\_CN/htmledition/images/bizmail/v3/icons\_features1ca3fe.png“
  169. },
  170. function(e){
  171. alert(e.err_msg);
  172. })
  173. },!1),
  174. document.getElementById(“hasEmoticon”).addEventListener(
  175. “click”,function(){
  176. WeixinJSBridge.invoke(“hasEmoticon”,{
  177. “url”:”http://rescdn.qqmail.com/bizmail/zh\_CN/htmledition/images/bizmail/v3/icons\_features1ca3fe.png“
  178. },
  179. function(e){
  180. alert(e.err_msg);
  181. })
  182. },!1),
  183. document.getElementById(“addContact”).addEventListener(
  184. “click”,function(){
  185. WeixinJSBridge.invoke(“addContact”,{
  186. “webtype”:”1”,
  187. “username”:”gh_412d74fbb474”
  188. },
  189. function(e){
  190. alert(e.err_msg);
  191. })
  192. },!1),
  193. document.getElementById(“jumpToBizProfile”).addEventListener(
  194. “click”,function(){
  195. WeixinJSBridge.invoke(“jumpToBizProfile”,{
  196. “tousername”:”gh_2248a2ade13e”
  197. },
  198. function(e){
  199. alert(e.err_msg);
  200. })
  201. },!1),
  202. document.getElementById(“toggleMenuBtn”).addEventListener(
  203. “click”,function(){
  204. menuHidden?
  205. (WeixinJSBridge.call(“showOptionMenu”),menuHidden=!1,this.value=”隐藏右上角按钮”)
  206. :
  207. (WeixinJSBridge.call(“hideOptionMenu”),menuHidden=!0,this.value=”显示右上角按钮”)
  208. },!1),
  209. document.getElementById(“toggleToolbar”).addEventListener(
  210. “click”,function(){
  211. toolbarHidden?
  212. (WeixinJSBridge.call(“showToolbar”),toolbarHidden=!1,this.value=”隐藏底部导航栏”)
  213. :
  214. (WeixinJSBridge.call(“hideToolbar”),toolbarHidden=!0,this.value=”显示底部导航栏”)
  215. },!1),
  216. document.getElementById(“getNetType”).addEventListener(
  217. “click”,function(){
  218. WeixinJSBridge.invoke(“getNetworkType”,{},
  219. function(e){
  220. alert(netType[e.err_msg])
  221. })
  222. },!1),
  223. document.getElementById(“closeWindow”).addEventListener(
  224. “click”,function(){
  225. WeixinJSBridge.invoke(“closeWindow”,{},function(e){})
  226. },!1),
  227. document.getElementById(“getBrandWCPayRequest”).addEventListener(
  228. “click”,function(){
  229. WeixinJSBridge.invoke(“getBrandWCPayRequest”,{
  230. “appId” : “wxf8b4f85f3a794e77”, //公众号名称,由商户传入
  231. “timeStamp” : “189026618”, //时间戳 这里随意使用了一个值
  232. “nonceStr” : “adssdasssd13d”, //随机串
  233. “package” :
  234. “body=xxx&fee_type=1&input_charset=GBK¬ify_url=http&out_trade_no=16642817866003386000&partner=1900000109&return_url=http&spbill_create_ip=127.0.0.1&total_fee=1&sign=273B7EEEE642A8E41F27213D8517E0E4”, //扩展字段,由商户传入
  235. “signType” : “SHA1”, //微信签名方式:sha1
  236. “paySign” : “b737015b5b1eabe5db580945a07eac08c7bb55f8” //微信签名
  237. },
  238. function(e){
  239. alert(e.err_msg)
  240. })
  241. },!1),
  242. document.getElementById(“setPageState”).addEventListener(
  243. “click”,function(){
  244. WeixinJSBridge.invoke(“setPageState”,{
  245. “state” : “1”
  246. })
  247. },!1),
  248. document.getElementById(“sendEmail”).addEventListener(
  249. “click”,function(){
  250. WeixinJSBridge.invoke(“sendEmail”,{
  251. “title” : “title!”,
  252. “content” : “i am an Email!”, //时间戳 这里随意使用了一个值
  253. },
  254. function(e){
  255. // alert(e.err_msg)
  256. })
  257. },!1),
  258. document.getElementById(“openSpecificView”).addEventListener(
  259. “click”,function(){
  260. WeixinJSBridge.invoke(“openSpecificView”,{
  261. “specificview” : “contacts”
  262. },
  263. function(e){
  264. alert(e.err_msg)
  265. })
  266. },!1),
  267. document.getElementById(“getCanIAPPay”).addEventListener(
  268. “click”,function(){
  269. WeixinJSBridge.invoke(“getCanIAPPay”,{ },
  270. function(e){
  271. alert(e.err_msg)
  272. })
  273. },!1),
  274. document.getElementById(“getBrandIAPPayRequest”).addEventListener(
  275. “click”,function(){
  276. WeixinJSBridge.invoke(“getBrandIAPPayRequest”,{
  277. “appId” : “wxf8b4f85f3a794e77”, //公众号名称,由商户传入
  278. “timeStamp” : “189026618”, //时间戳 这里随意使用了一个值
  279. “nonceStr” : “adssdasssd13d”, //随机串
  280. “package” : “bankType=CITIC_CREDIT&bankName=%e4%b8%ad%e4%bf%a1%e9%93%b6%e8%a1%8c&sign=CF8922F49431FFE8A1834D0B32B25CE3”,
  281. //扩展字段,由商户传入
  282. “signType” : “SHA1”, //微信签名方式:sha1
  283. “paySign” : “1e6f13f78ca0ec43fbb80899087f77568af66987” //微信签名
  284. },
  285. function(e){
  286. alert(e.err_msg)
  287. })
  288. },!1),
  289. document.getElementById(“openLocation”).addEventListener(
  290. “click”,function(){
  291. WeixinJSBridge.invoke(“openProductView”,{
  292. “latitude” : 23.113, //纬度
  293. “longitude” : 113.23, //经度
  294. “name” : “TIT创意园”, //POI名称
  295. “address” : “广州市海珠区新港中路397号”, //地址
  296. “scale” : 14, //地图缩放级别
  297. “infoUrl” : “http://weixin.qq.com/“, //查看位置界面底部的超链接
  298. },
  299. function(e){
  300. alert(e.err_msg)
  301. })
  302. },!1),
  303. document.getElementById(“timelineCheckIn”).addEventListener(
  304. “click”,function(){
  305. WeixinJSBridge.invoke(“timelineCheckIn”,{
  306. “img_url”: “http://mmsns.qpic.cn/mmsns/RLllkTm3DUdV24xbZnKicx9jJWxXI0Bq84zzbtibGuRyk/0“, // 分享到朋友圈的缩略图
  307. “img_width”: “640”, // 图片的长度
  308. “img_height”: “640”, // 图片高度
  309. “link”: “http://news.qq.com/zt2012/cxkyym/index.htm“, // 连接地址
  310. “desc”: “这个是描述啊啊”, // 描述
  311. “title”: “朝鲜称中国渔船越界捕捞”, // 分享标题
  312. “latitude” : 23.113, //纬度
  313. “longitude” : 113.23, //经度
  314. “poiId” : “dianping_2331037”, //商户id
  315. “poiName” : “TIT创意园”, //POI名称
  316. “poiAddress” : “广州市海珠区新港中路397号”, //地址
  317. “poiScale” : 14, //地图缩放级别
  318. “poiInfoUrl” : “http://weixin.qq.com/“ //查看位置界面底部的超链接
  319. },
  320. function(e){
  321. alert(e.err_msg)
  322. })
  323. },!1),
  324. document.getElementById(“geoLocation”).addEventListener(
  325. “click”,function(){
  326. WeixinJSBridge.invoke(“geoLocation”,{
  327. },
  328. function(e){
  329. alert(e.err_msg)
  330. })
  331. },!1),
  332. document.getElementById(“getBrandWCPayCreateCreditCardRequest”).addEventListener(
  333. “click”,function(){
  334. WeixinJSBridge.invoke(“getBrandWCPayCreateCreditCardRequest”,{
  335. “appId” : “wxf8b4f85f3a794e77”, //公众号名称,由商户传入
  336. “timeStamp” : “189026618”, //时间戳 这里随意使用了一个值
  337. “nonceStr” : “adssdasssd13d”, //随机串
  338. “package” : “bankType=CITIC_CREDIT&bankName=%e4%b8%ad%e4%bf%a1%e9%93%b6%e8%a1%8c&sign= CF8922F49431FFE8A1834D0B32B25CE3”,
  339. //扩展字段,由商户传入
  340. “signType” : “SHA1”, //微信签名方式:sha1
  341. “paySign” : “1e6f13f78ca0ec43fbb80899087f77568af66987” //微信签名
  342. },
  343. function(e){
  344. alert(e.err_msg)
  345. })
  346. },!1),
  347. document.getElementById(“getInstallState”).addEventListener(
  348. “click”,function(){
  349. WeixinJSBridge.invoke(“getInstallState”,{
  350. “packageUrl”:”teamcircle://“
  351. },
  352. function(e){
  353. alert(e.err_msg)
  354. })
  355. },!1),
  356. document.getElementById(“openProductView”).addEventListener(
  357. “click”,function(){
  358. WeixinJSBridge.invoke(“openProductView”,{
  359. “productInfo”:”json”
  360. },
  361. function(e){
  362. alert(e.err_msg)
  363. })
  364. },!1),
  365. document.getElementById(“getLatestAddress”).addEventListener(
  366. “click”,function(){
  367. WeixinJSBridge.invoke(“getLatestAddress”,{
  368. “appId” : “wxf8b4f85f3a794e77”, //公众号名称,由商户传入
  369. “timeStamp” : “189026618”, //时间戳 这里随意使用了一个值
  370. “nonceStr” : “adssdasssd13d”, //随机串
  371. “signType” : “SHA1”, //微信签名方式:sha1
  372. “addrSign” : “b737015b5b1eabe5db580945a07eac08c7bb55f8”, //微信签名
  373. “scope” : “snsapi”
  374. },
  375. function(e){
  376. alert(e.err_msg)
  377. })
  378. },!1),
  379. document.getElementById(“editAddress”).addEventListener(
  380. “click”,function(){
  381. WeixinJSBridge.invoke(“editAddress”,{
  382. “appId” : “wxf8b4f85f3a794e77”, //公众号名称,由商户传入
  383. “timeStamp” : “189026618”, //时间戳 这里随意使用了一个值
  384. “nonceStr” : “adssdasssd13d”, //随机串
  385. “signType” : “SHA1”, //微信签名方式:sha1
  386. “addrSign” : “b737015b5b1eabe5db580945a07eac08c7bb55f8”, //微信签名
  387. “scope” : “snsapi”
  388. },
  389. function(e){
  390. alert(e.err_msg)
  391. })
  392. },!1),
  393. document.getElementById(“launch3rdApp”).addEventListener(
  394. “click”,function(){
  395. WeixinJSBridge.invoke(“launch3rdApp”,{
  396. “appId” : “wx5823bf96d3bd56c7”, //公众号名称,由商户传入
  397. },
  398. function(e){
  399. alert(e.err_msg)
  400. })
  401. },!1),
  402. document.getElementById(“jumpWCMall”).addEventListener(
  403. “click”,function(){
  404. WeixinJSBridge.invoke(“jumpWCMall”,{
  405. “appId” : “wx5823bf96d3bd56c7”, //公众号名称,由商户传入
  406. “funcId”:”1000”
  407. },
  408. function(e){
  409. alert(e.err_msg)
  410. })
  411. },!1),
  412. document.getElementById(“openUrlByExtBrowser”).addEventListener(
  413. “click”,function(){
  414. WeixinJSBridge.invoke(“openUrlByExtBrowser”,{
  415. “url” : “http://m.exmail.qq.com“
  416. },
  417. function(e){
  418. alert(e.err_msg)
  419. })
  420. },!1)
  421. }
  422. );
  423. </**script**>
  424. </**body**>
  425. </**html**>

发表评论

表情:
评论列表 (有 0 条评论,410人围观)

还没有评论,来说两句吧...

相关阅读