1
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package com.example.mini_program.service;
|
||||
|
||||
import com.example.mini_program.config.WxMiniAppConfig;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Service
|
||||
public class WxLoginService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WxLoginService.class);
|
||||
private static final String JSCODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
|
||||
|
||||
private final WxMiniAppConfig wxMiniAppConfig;
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public WxLoginService(WxMiniAppConfig wxMiniAppConfig) {
|
||||
this.wxMiniAppConfig = wxMiniAppConfig;
|
||||
this.restTemplate = new RestTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用微信接口用code换取openid
|
||||
*
|
||||
* @param code 小程序调用wx.login获取的code
|
||||
* @return 登录结果,包含openid等信息
|
||||
*/
|
||||
public WxLoginResult code2Session(String code) {
|
||||
String url = String.format(JSCODE2SESSION_URL,
|
||||
wxMiniAppConfig.getAppid(),
|
||||
wxMiniAppConfig.getSecret(),
|
||||
code);
|
||||
|
||||
logger.info("调用微信jscode2session接口,code: {}", code);
|
||||
|
||||
try {
|
||||
// 微信返回text/plain,手动解析JSON字符串
|
||||
String response = restTemplate.getForObject(url, String.class);
|
||||
logger.info("微信返回原始结果: {}", response);
|
||||
|
||||
JSONObject json = new JSONObject(response);
|
||||
WxLoginResult result = new WxLoginResult();
|
||||
result.setOpenid(json.optString("openid"));
|
||||
result.setSession_key(json.optString("session_key"));
|
||||
result.setUnionid(json.optString("unionid"));
|
||||
result.setErrcode(json.optString("errcode"));
|
||||
result.setErrmsg(json.optString("errmsg"));
|
||||
|
||||
logger.info("解析后结果: openid={}, unionid={}, errcode={}",
|
||||
result.getOpenid(), result.getUnionid(), result.getErrcode());
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
logger.error("调用微信接口失败", e);
|
||||
WxLoginResult errorResult = new WxLoginResult();
|
||||
errorResult.setErrcode("-1");
|
||||
errorResult.setErrmsg("系统错误: " + e.getMessage());
|
||||
return errorResult;
|
||||
}
|
||||
}
|
||||
|
||||
public static class WxLoginResult {
|
||||
|
||||
private String session_key;
|
||||
private String unionid;
|
||||
private String openid;
|
||||
private String errcode;
|
||||
private String errmsg;
|
||||
|
||||
public String getSession_key() {
|
||||
return session_key;
|
||||
}
|
||||
|
||||
public void setSession_key(String session_key) {
|
||||
this.session_key = session_key;
|
||||
}
|
||||
|
||||
public String getUnionid() {
|
||||
return unionid;
|
||||
}
|
||||
|
||||
public void setUnionid(String unionid) {
|
||||
this.unionid = unionid;
|
||||
}
|
||||
|
||||
public String getOpenid() {
|
||||
return openid;
|
||||
}
|
||||
|
||||
public void setOpenid(String openid) {
|
||||
this.openid = openid;
|
||||
}
|
||||
|
||||
public String getErrcode() {
|
||||
return errcode;
|
||||
}
|
||||
|
||||
public void setErrcode(String errcode) {
|
||||
this.errcode = errcode;
|
||||
}
|
||||
|
||||
public String getErrmsg() {
|
||||
return errmsg;
|
||||
}
|
||||
|
||||
public void setErrmsg(String errmsg) {
|
||||
this.errmsg = errmsg;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user