|
@ -13,7 +13,6 @@ import java.util.Map;
|
|
|
import java.util.Random;
|
|
|
|
|
|
import com.yihu.wlyy.util.*;
|
|
|
import org.apache.commons.collections.map.HashedMap;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -54,22 +53,17 @@ public class WeixinBaseController extends BaseController {
|
|
|
* @param code
|
|
|
* @return
|
|
|
*/
|
|
|
public Map<String,Object> getOpenidByCode(String code) {
|
|
|
public String getOpenidByCode(String code) {
|
|
|
try {
|
|
|
String token_url = "https://api.weixin.qq.com/sns/oauth2/access_token";
|
|
|
String params = "appid=" + appId + "&secret=" +appSecret+ "&code=" + code + "&grant_type=authorization_code";
|
|
|
String result = HttpUtil.sendGet(token_url, params);
|
|
|
System.out.println("getOpenidByCode:"+result);
|
|
|
JSONObject json = new JSONObject(result);
|
|
|
Map<String,Object> map = new HashedMap();
|
|
|
if (json.has("openid")) {
|
|
|
map.put("openid",json.get("openid").toString());
|
|
|
if(json.has("unionid")){
|
|
|
map.put("unionid",json.get("unionid").toString());
|
|
|
}
|
|
|
return map;
|
|
|
return json.get("openid").toString();
|
|
|
} else {
|
|
|
return map;
|
|
|
return null;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
@ -78,19 +72,16 @@ public class WeixinBaseController extends BaseController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 通过code获取判断openid
|
|
|
*
|
|
|
* @param code
|
|
|
* 通过openid获取Unionid
|
|
|
* @return
|
|
|
*/
|
|
|
public String getUnionIDByCode(String code) {
|
|
|
public String getUnionidByOpenid(String openid) {
|
|
|
try {
|
|
|
String token_url = "https://api.weixin.qq.com/sns/oauth2/access_token";
|
|
|
String params = "appid=" + appId + "&secret=" +appSecret+ "&code=" + code + "&grant_type=authorization_code";
|
|
|
String token_url = "https://api.weixin.qq.com/cgi-bin/user/info";
|
|
|
String params = "access_token=" + getAccessToken() + "&openid=" +openid+ "&lang=zh_CN";
|
|
|
String result = HttpUtil.sendGet(token_url, params);
|
|
|
System.out.println("getOpenidByCode:"+result);
|
|
|
System.out.println("getUnionidByOpenid:"+result);
|
|
|
JSONObject json = new JSONObject(result);
|
|
|
System.out.println("result:"+json.toString());
|
|
|
if (json.has("unionid")) {
|
|
|
return json.get("unionid").toString();
|
|
|
} else {
|