这段时间,一直闲,断断续续写了差不多10多篇博客了。今天下午研究了下微信小程序登录,以前没有做过这方面的,趁着有资源,玩一下小程序。
第一步: 去看微信开发文档接口,这个比什么资料都强,因为你是要调他们的接口。
第二步: 写Http请求工具类。
第三步: 配置账号AppID,AppSecret,与微信请求路径,也就是微信官方登录的请求路径,后面代码我会写上。
第四步: 接口,实现层,Controller写好。
第五步: 测试,联调。
其实调接口蛮简单的,只要你看懂了官方文档,无非是传参,业务处理,和异常处理。我就不啰嗦了,直接写代码把。
HttpClientUtil 请求工具类
/**
* dapeng
*/
public class HttpClientUtil {
public static String doGet(String url, Map param) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List/**/ paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url, Map param,String token) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
if(token!=null){
httpPost.addHeader("Authorization", "QBox "+token);
}
// 创建参数列表
if (param != null) {
List/**/ paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null,null);
}
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
System.out.println(httpPost);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPostJson(String url, String json,String token) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
if(token!=null){
httpPost.setHeader("Authorization","QBox <"+token+">");
}
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
System.out.println(httpPost);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
}
WxConfig 参数配置,同学们,AppID与AppSecret你要写上你们公司账号的哈!
public static final String AppID="AppID";
public static final String AppSecret="AppSecret";
public static final String CODE_SESSION = "https://api.weixin.qq.com/sns/jscode2session";
impl实现层,接口我就不显示了,因为我实现层都写出来了。
@Override
public WxLoginDto wxLogin(WxLoginParam wxLoginParam) {
Map param = new HashMap<>();
param.put("appid", WxConfigVar.AppID);
param.put("secret", WxConfigVar.AppSecret);
param.put("js_code", wxLoginParam.getWxCode());
param.put("grant_type", "authorization_code");
String result = HttpClientUtil.doGet(WxConfigVar.CODE_SESSION, param);
WxUserSession session = JSON.parseObject(result, WxUserSession.class);
try {
//这个里面是我公司的业务层,我直接去了,免得误导大家哈。
} catch (Exception e) {
e.printStackTrace();
new WxUserException(ErrorCodeEnum.EDINSERTFAIL.code(), ErrorCodeEnum.EDINSERTFAIL.msg());
}
//wxLoginDto 这个是我封装的一个类,用来返回前端需要的数据,你们自己随意。
return wxLoginDto;
}
小程序登录就这么简单,等会去看看微信登录,好像要申请应用。到时候我申请好了,发出来给大家看看,工作接口都写好了,等着联调了。