开发微信小程序时,经常用到获取用户的openid,那么如何获取用户openid呢?
1.首先需要在小程序中获取用户的code。
如何获取code?
只需小程序用户登录就行。
//登录
wx.login({
success: function (res) {
//res.code
然后就请求你的后台接口获取。
},
fail: function () {
}
})
2、通过用户登录获取的code,请求微信接口获取openid。
注:一个code只能用一次,虽然微信小程序可以请求微信接口获取用户openid,但是发布版不行,需要添加安全域名,但是微信的域名不能存放在安全域名中,所有少年!放弃吧,用后台接口吧。
微信api接口:"https://api.weixin.qq.com/sns/jscode2session"
请求参数说明:
我用的是java获取用户openid
private Map<String, Object> _getOpenId(String code){
Map<String, Object> ret = new HashMap<>();
// 组装参数*****
Map<String, String> urlData= new HashMap<String, String>();
urlData.put("appid",appid);//小程序id
urlData.put("secret",appKey);//小程序key
urlData.put("grant_type","authorization_code");//固定值这样写就行
urlData.put("js_code",code);//小程序传过来的code
HttpsClientUtil httpsClientUtil = new HttpsClientUtil();
Object data_deserialize = null;
try {
//code2OpenidUrl "https://api.weixin.qq.com/sns/jscode2session";
String dataStr = httpsClientUtil.doGet(code2OpenidUrl, urlData);
data_deserialize = JSONUtil.deserialize(dataStr);
}catch(Exception ex){
ret.put("success", false);
ret.put("msg", "_getOpenId_未知异常");
ret.put("message", ex);
return ret;
}
Map<String, String> data= (Map<String, String>)data_deserialize;
if( data.containsKey("errcode") ){
ret.put("success", false);
ret.put("msg", data.containsKey("errcode"));
ret.put("message", data.containsKey("errmsg"));
}else{
ret.put("success", true);
ret.put("result",data);
}
return ret;
}
我用的java请求微信的方法, 三种,get、post、postXml,每个人写的不同,每个库也不同,仅供参考。
public String doPost(String url, Map<String,String> map) throws Exception{
String result = null;
HttpClient httpClient = new SSLClient();
HttpPost httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity, "UTF-8");
}
}
return result;
}
public String doGet(String url, Map<String,String> map) throws Exception{
String result = null;
HttpClient httpClient = new SSLClient();
String param="";
for(String nameKey:map.keySet()){
param += nameKey+"="+map.get(nameKey)+"&";
}
param = param.substring(0,param.length()-1);
String urlNameString = url + "?" + param;
HttpGet httpGet = new HttpGet(urlNameString);
HttpResponse response = httpClient.execute(httpGet);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity, "UTF-8");
}
}
return result;
}
public String doPostXml(String url, String xml) throws Exception{
String result = null;
HttpClient httpClient = new SSLClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type","text/xml;charset=UTF-8");
StringEntity stringEntity = new StringEntity(xml, "UTF-8");
stringEntity.setContentEncoding("UTF-8");
httpPost.setEntity(stringEntity);
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity, "UTF-8");
}
}
return result;
}