Controller类
@RequestMapping(value = "/getOpenId", method = RequestMethod.POST)
@ResponseBody
public ReturnDto loginApp(@RequestBody String code) {
Map<String, Object> ret = new HashMap<String, Object>();
try {
JSONObject json = WeChatCommon.getOpenId(properties.getAppid(), properties.getAppsecret(), code);
ret.put("openid", json.getString("openid"));
if (json.get("unionid") != null) {
ret.put("unionid", json.getString("unionid"));
}
ret.put("status", true);
} catch (Exception e) {
ret.put("status", false);
e.printStackTrace();
}
return new ReturnDto(ret);
}
上面的Appid Appsecret在application.yml文件中定义死,每个小程序特有的
weChat:
appid: xxxxx
appsecret: xxxxxx
调用共通方法WeChatCommon.getOpenId()
/**
* 获取接口访问凭证
*
* @param appid