从微信上获取用户的积分,为了同步到本地数据库。
前提:1.能够取得access_token,2.该公众号已经为用户发放了会员卡和积分。
源码如下:
public class TestPoint{
private static final String getCartListUrl = "https://api.weixin.qq.com/card/user/getcardlist? access_token=TOKEN";
private static final String GetCardInfoUrl = "https://api.weixin.qq.com/card/get?access_token=TOKEN";
private static final String getUserPointUrl = "https://api.weixin.qq.com/card/membercard/userinfo/get? access_token=TOKEN";
@Test
public Integer getPointFromWechatByOpenId(String openId,String access_token) {
Integer point = 0;
String retCartListUrl = getCartListUrl.replace("TOKEN", access_token);
UserInfo userInfo = userInfoService.getByOpenid(openId);
OpenId openid = new OpenId();
openid.setOpenid(userInfo.getOpenId());
JSONObject jsonObject = CommonUtil.httpsRequest(retCartListUrl, "POST", JSON.toJSONString(openid));
System.out.println(JSON.toJSONString(jsonObject));
JSONArray cardList = jsonObject.getJSONArray("card_list");
if (jsonObject.containsValue("ok")) {
if (cardList != null && cardList.size() > 0) {
JSONObject cardJSONObject = cardList.getJSONObject(0);
Card card = new Card(cardJSONObject.getString("card_id"), cardJSONObject.getString("code"));
//=============
//String getcardInfoUrl = GetCardInfoUrl.replace("TOKEN", access_token);
//CardId cardId = new CardId();
//cardId.setCard_id(card.getCard_id());
//JSONObject cardInfoJSONObjectd = CommonUtil.httpsRequest(getcardInfoUrl, "POST", JSON.toJSONString(cardId));
//System.out.println(JSON.toJSONString(cardInfoJSONObjectd));
//=============
String POINTURL = getUserPointUrl.replace("TOKEN", access_token);
JSONObject pointJSONObjectd = CommonUtil.httpsRequest(POINTURL, "POST", JSON.toJSONString(card));
System.out.println(JSON.toJSONString(pointJSONObjectd));
if (pointJSONObjectd.containsValue("ok")) {
int bonus = pointJSONObjectd.getInt("bonus");
System.out.println(bonus);
point = bonus;
}
}
}
return point;
} // endMethod
} // endClass
@Data
@AllArgsConstructor
@NoArgsConstructor
class Card {
private String card_id;
private String code;
}
@Data
class OpenId {
private String openid;
}