微信公众号开发之扫一扫

在实现微信扫一扫功能时需要做三件事:

1.在需要调用微信扫一扫的页面加载微信的js:

<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>

2.在调用接口之前需要有四个date,分别是:appId,timestamp,nonceStr,signature。这四个数据可以使用使用ajax从后台获取:具体方法如下:

JSP页面需要调用扫一扫的按钮:

<input class="text"  type="text" name="UUID" id="UUID"><img align="right" src="images/scanQRCode.png" onclick="scanQRCode('<%=user.getOpenId()%>');">

js方法实现后台调用四个数据:

function scanQRCode(openId){
	ht.request({
			type : "post",
			url : "web?operation=jsapi_sign&menu_type=user",
			params : {
				url : location.href.split('#')[0],
				open_id : openId
			},
			callback : function(data) {
				data = eval("(" + data + ")");
				wx.config({
					debug : false,    
					appId : data.appId, // 必填,公众号的唯一标识
					timestamp : data.timestamp, // 必填,生成签名的时间戳
					nonceStr : data.nonceStr, // 必填,生成签名的随机串
					signature : data.signature,// 必填,签名
					jsApiList : [ 'checkJsApi', 'startRecord', 'stopRecord',
							'translateVoice', 'scanQRCode',// 微信扫一扫接口
							'openCard' ]
				});
				wx.ready(function() {
					wx.checkJsApi({
						jsApiList : ['scanQRCode'],
						success : function(res) {
							//点击按钮扫描二维码
							wx.scanQRCode({
								needResult : 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
								scanType : [ "qrCode" ], // 可以指定扫二维码还是一维码,默认二者都有
								success : function(res) {
									var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
									document.getElementById("UUID").value=result;
								},
								fail : function() {
									alert("扫码错误");
								}
							});
						}
					});
				});
				wx.error(function(res) {
					alert("出错了:" + res.errMsg);//这个地方的好处就是wx.config配置错误,会弹出窗口哪里错误,然后根据微信文档查询即可。
				});
			}
		});
	}

js说明:ht.request({});即为ajax,等同于JQ的$ajax,使用的是公司封住的方法,这步可使用同步异步都行。wx.ready(function() {})这个方法一定要放入callback : function(data) {}之中,如果单独拿出,安卓手机正常是使用但苹果手机需要点击两次按钮才会成功,虽然后台也执行了同时也成功返回到前台由于执行时间较长还是无法实现扫一扫,具体原因不明,如果加个输出alert作为缓冲,则点击一次就可成功。

wx.checkJsApi({})该方法就是在调用微信的js接口,具体可详见开发文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

3.后台生成四个数据:

case JSAPI_SIGN: {
				String openId = RequestParameter.OPEN_ID.getValue(request, formData);
				String url = RequestParameter.URL.getValue(request, formData);
				String appId = Config.APP_ID.getValue();
				String token = AccessTokenManager.getAccessToken();
				if (token == null) {
					return null;
				}
				String requestUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?";//该链接用于获取签名
				String params = "access_token=" + token + "&type=jsapi";
				org.json.JSONObject result = ServiceUtil.request(requestUrl + params);

				String jsapi_ticket = result.getString("ticket");
				Map<String, String> map = WXUtil.sign(jsapi_ticket, url);
				map.put("appId", appId);
				JSONObject json = (JSONObject) JSONObject.toJSON(map);
				return json.toString();//将map转为map
			}

获取token:

public static synchronized String getAccessToken() {
		try {
			DAO dao = DAO.newInstance();
			String id = JSONObjectKey.ACCESS_TOKEN.getKeyName();
			AccessToken accessToken = dao.getAccessTokenById(id);
			if (accessToken == null) {
				accessToken = WXService.getAccessToken();
				if (accessToken == null) {
					return null;
				}
				accessToken.setRecId(JSONObjectKey.ACCESS_TOKEN.getKeyName());
				accessToken.setCreateTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
				dao.addAccessToken(accessToken);
			} else {
				Date date = DateUtil.parseDate(accessToken.getCreateTime());
				int expiresIn = Integer.parseInt(accessToken.getExpiresIn());
				long interval = (System.currentTimeMillis() - date.getTime()) / 1000;
				if (interval - expiresIn >= -60) {
					dao.deleteAccessTokenById(id);
					return getAccessToken();
				}
			}
			return accessToken.getAccessToken();
		} catch (Exception e) {
			Log.error(e);
			return null;
		}
	}
public class DAO extends DataAccessObject {

	public static DAO newInstance() {
		DAO dao = new DAO();
		return dao;
	}

public AccessToken getAccessTokenById(final String id) throws Exception {
		if (id == null) {
			return null;
		}
		return new DAOMethodWithReturnValue<AccessToken>(this) {

			@Override
			protected AccessToken onExecute() throws Exception {
				return new AccessTokenTableAdapter(dbo).selectFirst("rec_id = ?", new int[] { Types.VARCHAR },
						new String[] { id });
			}
		}.execute();
	}


public int deleteAccessTokenById(final String id) throws Exception {
		if (id == null) {
			return 0;
		}
		return new DAOMethodWithReturnValue<Integer>(this) {

			@Override
			protected Integer onExecute() throws Exception {
				return new AccessTokenTableAdapter(dbo).delete("rec_id = ?", new int[] { Types.VARCHAR },
						new String[] { id });
			}
		}.execute();
	}

public int addAccessToken(final AccessToken... records) throws Exception {
		if (records == null || records.length == 0) {
			return 0;
		}
		return new DAOMethodWithReturnValue<Integer>(this) {

			@Override
			protected Integer onExecute() throws Exception {
				return new AccessTokenTableAdapter(dbo).insert(records);
			}
		}.execute();
	}

}
WXservice(){

public static AccessToken getAccessToken() {
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
		url = String.format(url, Config.APP_ID.getValue(), Config.APP_SECRET.getValue());
		JSONObject result = ServiceUtil.request(url);
		if (result == null) {
			return null;
		}
		if (ServiceUtil.isError(result)) {
			Log.error(result);
			return null;
		}
		AccessToken token = new AccessToken();
		token.setAccessToken(JSONObjectKey.ACCESS_TOKEN.getString(result));
		token.setExpiresIn(String.valueOf(JSONObjectKey.EXPIRES_IN.getInt(result, 60)));
		return token;
	}
}

生成签名,时间内戳以及随机字符串并组织成map

WXUtil(){

/**
	 * 生成签名并组织成map返回
	 * @param jsapi_ticket
	 * @param url
	 * @return
	 */
	public static Map<String, String> sign(String jsapi_ticket, String url) {
		Map<String, String> ret = new HashMap<String, String>();
		String nonce_str = create_nonce_str();
		String timestamp = create_timestamp();
		String string1;
		String signature = "";

		// 注意这里参数名必须全部小写,且必须有序
		string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "&timestamp=" + timestamp + "&url=" + url;
		System.out.println(string1);

		try {
			MessageDigest crypt = MessageDigest.getInstance("SHA-1");
			crypt.reset();
			crypt.update(string1.getBytes("UTF-8"));
			signature = byteToHex(crypt.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		ret.put("url", url);
		ret.put("jsapi_ticket", jsapi_ticket);
		ret.put("nonceStr", nonce_str);
		ret.put("timestamp", timestamp);
		ret.put("signature", signature);

		return ret;
	}

	//生成签名
	private static String byteToHex(final byte[] hash) {
		Formatter formatter = new Formatter();
		for (byte b : hash) {
			formatter.format("%02x", b);
		}
		String result = formatter.toString();
		formatter.close();
		return result;
	}

	//生成随机字符串nonceStr
	private static String create_nonce_str() {
		return UUID.randomUUID().toString();
	}

	//生成时间戳timeStamp
	private static String create_timestamp() {
		return Long.toString(System.currentTimeMillis() / 1000);
	}

	public static InputStream getMedia(String mediaId) {
	    String url = "https://api.weixin.qq.com/cgi-bin/media/get";
	    String access_token = AccessTokenManager.getAccessToken();
	    String params = "access_token=" + access_token + "&media_id=" + mediaId;
	       InputStream is = null;
	       try {
	        String urlNameString = url + "?" + params;
	           URL urlGet = new URL(urlNameString);
	           HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
	           http.setRequestMethod("GET"); // 必须是get方式请求
	           http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
	           http.setDoOutput(true);
	           http.setDoInput(true);
	           http.connect();
	           // 获取文件转化为byte流
	           is = http.getInputStream();
	       } catch (Exception e) {
	           e.printStackTrace();
	       }
	    return is;
	}
}

 

 

Java代码说明:在用户使用公众号时会生成一个token,时间为7200秒,我将它存入数据库了,所以在获取token时先查数据库存在重新生成,不存在重新调用url生成token插入数据库。

总结:所有调用微信js接口比如,上传图片等都可以按该流程走下去,获取签名等数据,然后调用js接口。

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页