微信JSSDK开发,调用微信扫一扫jsp前端

java后台代码
public Map getSignture(HttpServletRequest request,String urls) {

		String APPID = "";// 第三方用户唯一凭证,appid

		String APPSECRET = "";// 第三方用户唯一凭证密钥,即appsecret
		
		//获取微信access_token的URL
		String access_tokenURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
				+ APPID + "&secret=" + APPSECRET;

		//获取微信access_token
		String access_token = LwxUtil.rulReturnJSON(access_tokenURL, "GET",
				"access_token");

		String jsapi_ticketURL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="
				+ access_token + "&type=jsapi";

		//获取微信jsapi_ticket 
		String jsapi_ticket = LwxUtil.rulReturnJSON(jsapi_ticketURL, "GET",
				"ticket");
		
		return Sign.sign(jsapi_ticket, urls);
	}


jsp前段代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>扫一扫</title>
<script type="text/javascript"
	src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
	var urls = "${pageContext.request.contextPath}";
	var timestamp = "";// 时间戳
	var nonceStr = "";// 随机串
	var signature = "";// 签名
	$(function() {
		$.get(urls + "/getSignture.do", {
			"urls" : location.href
		}, function(res) {
			$.each(res, function(i, v) {
				if ("timestamp" == i.trim()) {
					timestamp = v;
				}
				if ("nonceStr" == i.trim()) {
					nonceStr = v;
				}
				if ("signature" == i.trim()) {
					signature = v;
				}
			});
			wx.config({
				debug : true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
				appId : "wxae0029b16ee5cab4", // 必填,公众号的唯一标识
				timestamp : timestamp, // 必填,生成签名的时间戳
				nonceStr : nonceStr, // 必填,生成签名的随机串
				signature : signature,// 必填,签名,见附录1
				jsApiList : ["scanQRCode"]
			// 必填,需要使用的JS接口列表,所有JS接口列表见附录2
			});
		});
	});
</script>
</head>
<body>
	<input type="button" value="扫一扫" id="scanQRCode"
		style="width: 3rem; height: 3rem;" />
	<input type="text" id="sousuoid" placeholder="请输入商品关键字" name="ssnr">
</body>
<script type="text/javascript" src="js/wx.js"></script>
</html>


wx.js代码

// 点击扫描按钮,扫描二维码并返回结果
$("#scanQRCode").click(function() {
	wx.scanQRCode({ // 默认为0,扫描结果由微信处理,1则直接返回扫描结果 
		needResult : 1,
	    scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都有
		desc : 'scanQRCode desc',
		success : function(res) { // 扫码后获取结果参数赋值给Input
			alert(res);
			var url = res.resultStr; // 商品条形码,取","后面的
			if (url.indexOf(",") >= 0) {
				var tempArray = url.split(',');
				var tempNum = tempArray[1];
				$("#sousuoid").val(tempNum);
			} else {
				$("#sousuoid").val(url);
			}
		},
		error : function(){
			alert("fail");
		}
	});
});



  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值