相关知识
微信小程序、spring-boot
一、步骤
1.获得的用户登录凭证code(有效期五分钟)
2.发送参数code至后端
3.通过code获取openid
二、实现
1.获取code
接口:wx.login
接口地址:https://developers.weixin.qq.com/miniprogram/dev/framework/plugin/functional-pages/user-info.html
app.js
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionIdp
console.log(res.code)
wx.request({
url: 'http://127.0.0.1:8081/user_info/openid?code='+res.code,
success:res=>{
console.log(res)
this.globalData.openid = res.data.data[0].openid
}
})
}
})
globalData: {
openid:null,
}
2.通过code获取openid
接口:https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
接口地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
核心代码
@Service
public class UserInfoServiceImpl implements UserInfoService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public List<HashMap> getOpenId(String code) {
logger.info("根据code获取openid");
String url = jscode2session + "?"
+ "appid=" + appid
+ "&secret=" + secret
+ "&js_code=" + code
+ "&grant_type=authorization_code";
try {
return HttpRequest.httpGet(url);
} catch (Exception e) {
e.printStackTrace();
logger.error("获取openid失败");
}
return null;
}
}
请求工具
<!-- druid start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<!-- druid end -->
<!--json对象转换 start-->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--json对象转换 end-->
package com.aiplay.aipic.utils.http;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class HttpRequest {
public static List<HashMap> httpGet(String httpUrl) {
try {
URL url = new URL(httpUrl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:get
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
// 通过connection连接,获取输入流
//得到响应流
InputStream inputStream = connection.getInputStream();
//获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
List<HashMap> hashMaps = new ArrayList<>();
while ((line = reader.readLine()) != null){
HashMap hashMap = JSONObject.parseObject(line, HashMap.class);
hashMaps.add(hashMap);
}
reader.close();
connection.disconnect();
return hashMaps;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
注意:
测试时需要进入微信小程序,关闭IP白名单,否则请求不了openid
测试结果:
补充:
获取openid的请求链接可以直接在浏览器输入,直接获取openid,但是会暴露appid和secret,因此需要在后端请求。