|
|
|
@ -8,10 +8,8 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.client.RestClientException;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@ -28,11 +26,13 @@ public class WxLoginController {
|
|
|
|
|
@Value("${Wechat.Applets.appSecret}")
|
|
|
|
|
private String appSecret;
|
|
|
|
|
|
|
|
|
|
//小程序登录
|
|
|
|
|
// 小程序登录
|
|
|
|
|
@PostMapping("/wxLogin")
|
|
|
|
|
public ResultVo wxLogin(@RequestBody LoginParm parm){
|
|
|
|
|
public ResultVo wxLogin(@RequestBody LoginParm parm) {
|
|
|
|
|
String code = parm.getCode();
|
|
|
|
|
log.info("wxlogin - code: " + code);
|
|
|
|
|
log.info("appId: " + appId);
|
|
|
|
|
log.info("appSecret: " + (appSecret != null ? "已设置" : "未设置"));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 构建请求URL
|
|
|
|
@ -45,17 +45,40 @@ public class WxLoginController {
|
|
|
|
|
log.info("Request URL: " + url);
|
|
|
|
|
|
|
|
|
|
// 使用 RestTemplate 发送请求
|
|
|
|
|
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
|
|
|
|
String wxResult = response.getBody();
|
|
|
|
|
ResponseEntity<String> response;
|
|
|
|
|
try {
|
|
|
|
|
response = restTemplate.getForEntity(url, String.class);
|
|
|
|
|
} catch (RestClientException e) {
|
|
|
|
|
log.error("调用微信API失败: " + e.getMessage(), e);
|
|
|
|
|
return ResultUtils.error("网络连接失败,无法访问微信服务器");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String wxResult = response.getBody();
|
|
|
|
|
log.info("WeChat API response: " + wxResult);
|
|
|
|
|
|
|
|
|
|
//转换参数
|
|
|
|
|
// 检查微信API返回的错误
|
|
|
|
|
if (wxResult == null) {
|
|
|
|
|
log.error("微信API返回空响应");
|
|
|
|
|
return ResultUtils.error("微信服务器无响应");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (wxResult.contains("\"errcode\":")) {
|
|
|
|
|
log.error("微信API返回错误: " + wxResult);
|
|
|
|
|
return ResultUtils.error("微信API错误: " + wxResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 转换参数
|
|
|
|
|
Code2Session userJson = FastJsonTools.getJson(wxResult, Code2Session.class);
|
|
|
|
|
log.info("userJson: " + userJson);
|
|
|
|
|
|
|
|
|
|
if(userJson == null || userJson.getOpenid() == null) {
|
|
|
|
|
throw new BusinessException(500,"小程序获取open失败");
|
|
|
|
|
if (userJson == null) {
|
|
|
|
|
log.error("解析微信响应失败");
|
|
|
|
|
return ResultUtils.error("解析微信响应失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userJson.getOpenid() == null) {
|
|
|
|
|
log.error("微信响应中未包含openid: " + wxResult);
|
|
|
|
|
return ResultUtils.error("微信响应中未包含openid");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String openid = userJson.getOpenid();
|
|
|
|
@ -67,12 +90,35 @@ public class WxLoginController {
|
|
|
|
|
vo.setOpenid(openid);
|
|
|
|
|
vo.setSessionKey(sessionKey);
|
|
|
|
|
|
|
|
|
|
return ResultUtils.success("获取成功",vo);
|
|
|
|
|
return ResultUtils.success("获取成功", vo);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
log.error("WxLoginController.wxLogin Error", e);
|
|
|
|
|
return ResultUtils.error("小程序获取open失败: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加一个测试接口,验证配置是否正确
|
|
|
|
|
@GetMapping("/testConfig")
|
|
|
|
|
public ResultVo testConfig() {
|
|
|
|
|
try {
|
|
|
|
|
return ResultUtils.success("配置检查",
|
|
|
|
|
"appId: " + appId +
|
|
|
|
|
", appSecret: " + (appSecret != null ? "已设置" : "未设置"));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResultUtils.error("配置检查失败: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 验证服务器是否可以访问微信 API:
|
|
|
|
|
@GetMapping("/testNetwork")
|
|
|
|
|
public ResultVo testNetwork() {
|
|
|
|
|
try {
|
|
|
|
|
String testUrl = "https://api.weixin.qq.com";
|
|
|
|
|
ResponseEntity<String> response = restTemplate.getForEntity(testUrl, String.class);
|
|
|
|
|
return ResultUtils.success("网络连接正常", response.getStatusCode());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResultUtils.error("网络连接失败: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|