根据openid获取当前用户相对于当前公众平台的所有的代金券

 

//获取当前微信里面的能够使用的代金券列表
public
List<WxCardUseVo> getCard(String openId) { System.out.println("getcard"); Map<String, String> token = commonService.getAccessToken(appId, appSecret); String accessToken = token.get("accessToken") + ""; // 获取用户用于当前公众号的卡券 List<WxCardPackageCode> wxCardPackageCodeList = commonService .getWxCardPackageCodeList(accessToken, openId); List<WxCard> cardList = new ArrayList<WxCard>(); List<WxCardUseVo> cardVoList = new ArrayList<WxCardUseVo>(); WxCardUseVo cardVo = new WxCardUseVo(); String errcode = null; String canConsume = null; Map<String, Integer> map = new HashMap<String, Integer>(); if (wxCardPackageCodeList != null && wxCardPackageCodeList.size() > 0) { for (int i = 0; i < wxCardPackageCodeList.size(); i++) { String cardId = wxCardPackageCodeList.get(i).getCard_id(); String code = wxCardPackageCodeList.get(i).getCode(); WxCard wxCardDetailByCardId = commonService .getWxCardDetailByCardId(accessToken, cardId); if (wxCardDetailByCardId != null && "CASH".equals(wxCardDetailByCardId.getCard_type())) { System.out.println("code==========" + code); Map<String, String> openIdCardIdByCode = commonService .getOpenIdCardIdByCode(accessToken, code); System.out.println("11111111111111111111111111111111111"); System.out.println(new Gson().toJson(openIdCardIdByCode)); errcode = openIdCardIdByCode.get("errcode"); canConsume = openIdCardIdByCode.get("can_consume"); if (StringUtils.isNotEmpty(canConsume) && "true".equals(canConsume)) { cardVo.setCardId(cardId); cardVo.setCode(code); cardVo.setReduce_cost(wxCardDetailByCardId.getCash() .getReduce_cost() / 100); cardVo.setTitle(wxCardDetailByCardId.getCash() .getBase_info().getTitle()); cardVoList.add(cardVo); } } } } return cardVoList; }

 

 

//获取卡券状态
@Override
public Map<String, String> getOpenIdCardIdByCode(String accessToken, String code) { String responseInfo = HttpUtil.sendPostJson(GET_OPEN_CARD_ID_URL + accessToken, "code=" + code + "&check_consume=true"); System.out.println("get opendId cardId responseInfo = " + responseInfo); Map<String, String> map = new HashMap<String, String>(); if (StringUtils.isNotEmpty(responseInfo) && !responseInfo.equals("null")) { JSONObject jsonObject = JSONObject.fromObject(responseInfo); if (jsonObject != null && jsonObject.get("errcode") != null) { String errCode = jsonObject.getString("errcode"); if (StringUtils.isNotEmpty(errCode) && errCode.equals("0")) { String opendId = jsonObject.getString("openid"); map.put("openId", opendId); if(jsonObject != null && jsonObject.get("user_card_status") != null) { String user_card_status = jsonObject.getString("user_card_status"); map.put("user_card_status", user_card_status); } if(jsonObject != null && jsonObject.get("can_consume") != null) { String can_consume = jsonObject.getString("can_consume"); map.put("can_consume", can_consume); } if (jsonObject.get("card") != null) { jsonObject = JSONObject.fromObject(jsonObject .get("card")); if (jsonObject != null && jsonObject.get("card_id") != null) { String cardId = jsonObject.getString("card_id"); map.put("cardId", cardId); } } } else { WxAccessToken wxAccessToken = getWxAccessTokenByAccessToken(accessToken); if (wxAccessToken != null) { wxAccessToken.setCreateTime(wxAccessToken .getCreateTime() - 100000000000L); int num = commonDao.modifyWxAccessToken(wxAccessToken); if (num > 0) { Map<String, String> maptmp = getAccessToken( wxAccessToken.getAppId(), wxAccessToken.getAppSecret()); if (maptmp != null) { responseInfo = HttpUtil.sendPostJson( GET_OPEN_CARD_ID_URL + maptmp.get("accessToken"), "code=" + code); System.out .println("get opendId cardId responseInfo = " + responseInfo); if (StringUtils.isNotEmpty(responseInfo) && !responseInfo.equals("null")) { jsonObject = JSONObject .fromObject(responseInfo); if (jsonObject != null && jsonObject.get("errcode") != null) { errCode = jsonObject .getString("errcode"); if (StringUtils.isNotEmpty(errCode) && errCode.equals("0")) { String opendId = jsonObject .getString("openid"); map.put("openId", opendId); if(jsonObject != null && jsonObject.get("user_card_status") != null) { String user_card_status = jsonObject.getString("user_card_status"); map.put("user_card_status", user_card_status); } if(jsonObject != null && jsonObject.get("can_consume") != null) { String can_consume = jsonObject.getString("can_consume"); map.put("can_consume", can_consume); } if (jsonObject.get("card") != null) { jsonObject = JSONObject .fromObject(jsonObject .get("card")); if (jsonObject != null && jsonObject .get("card_id") != null) { String cardId = jsonObject .getString("card_id"); map.put("cardId", cardId); } } } } } } } } } } } return map; }

//获取当前用户领取过当前公众号的所有卡券

private static String GET_CARD_PACKAGE_CODE_URL = "https://api.weixin.qq.com/card/user/getcardlist?access_token=";

@Override
	public List<WxCardPackageCode> getWxCardPackageCodeList(String accessToken,
			String openId) {
		String responseInfo = HttpUtil.sendPostJson(GET_CARD_PACKAGE_CODE_URL
				+ accessToken, "openid=" + openId);
		System.out.println("getWxCardPackageCode responseInfo = "
				+ responseInfo);
		List<WxCardPackageCode> list = null;
		if (StringUtils.isNotEmpty(responseInfo)
				&& !responseInfo.equals("null")) {
			JSONObject jsonObject = JSONObject.fromObject(responseInfo);
			if (jsonObject != null && jsonObject.get("errcode") != null) {
				String errcode = jsonObject.getString("errcode");
				if (StringUtils.isNotEmpty(errcode) && errcode.equals("0")) {
					JSONArray jsonArray = JSONArray.fromObject(jsonObject
							.get("card_list"));
					list = JSONArray.toList(jsonArray, new WxCardPackageCode(),
							new JsonConfig());
				} else {
					WxAccessToken wxAccessToken = getWxAccessTokenByAccessToken(accessToken);
					if (wxAccessToken != null) {
						wxAccessToken.setCreateTime(wxAccessToken
								.getCreateTime() - 100000000000L);
						int num = commonDao.modifyWxAccessToken(wxAccessToken);
						if (num > 0) {
							Map<String, String> maptmp = getAccessToken(
									wxAccessToken.getAppId(),
									wxAccessToken.getAppSecret());
							if (maptmp != null) {
								responseInfo = HttpUtil.sendJsonToPost(
										GET_CARD_PACKAGE_CODE_URL
												+ maptmp.get("accessToken"),
										"openid=" + openId);
								if (StringUtils.isNotEmpty(responseInfo)
										&& !responseInfo.equals("null")) {
									jsonObject = JSONObject
											.fromObject(responseInfo);
									if (jsonObject != null
											&& jsonObject.get("errcode") != null) {
										errcode = jsonObject
												.getString("errcode");
										if (StringUtils.isNotEmpty(errcode)
												&& errcode.equals("0")) {
											JSONArray jsonArray = JSONArray
													.fromObject(jsonObject
															.get("card_list"));
											list = JSONArray.toList(jsonArray,
													new WxCardPackageCode(),
													new JsonConfig());
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return list;
	}
posted @ 2015-09-14 17:58  ordinary01  阅读(375)  评论(0)    收藏  举报