微信连接,本地数据上传
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 15m11s
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 15m11s
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.light.delivery.service.impl;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.light.delivery.dto.UserInfoResponse;
|
||||
import com.light.delivery.model.Employee;
|
||||
import com.light.delivery.model.LoginResponse;
|
||||
@@ -11,7 +13,9 @@ import com.light.delivery.repository.UserRepository;
|
||||
import com.light.delivery.service.UserService;
|
||||
import com.light.delivery.util.JwtUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
@@ -43,6 +47,24 @@ public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
private JwtUtil jwtUtil;
|
||||
|
||||
/**
|
||||
* 微信小程序appId
|
||||
*/
|
||||
@Value("${wx.appId}")
|
||||
private String wxAppId;
|
||||
|
||||
/**
|
||||
* 微信小程序secret
|
||||
*/
|
||||
@Value("${wx.secret}")
|
||||
private String wxSecret;
|
||||
|
||||
/**
|
||||
* 微信code2session接口URL
|
||||
*/
|
||||
@Value("${wx.api.code2session-url}")
|
||||
private String wxCode2SessionUrl;
|
||||
|
||||
/**
|
||||
* 根据JWT令牌获取用户信息。
|
||||
* 从token中解析出用户的openid,然后查询数据库获取完整的用户信息。
|
||||
@@ -99,8 +121,8 @@ public class UserServiceImpl implements UserService {
|
||||
throw new IllegalArgumentException("登录code不能为空");
|
||||
}
|
||||
|
||||
// 模拟微信登录,实际应该调用微信API获取 openid
|
||||
String openid = "openid_" + code; // 模拟 openid
|
||||
// 调用微信API获取真实的openid
|
||||
String openid = getOpenIdFromWeChat(code);
|
||||
|
||||
User user = userRepository.findByOpenid(openid);
|
||||
if (user == null) {
|
||||
@@ -133,6 +155,35 @@ public class UserServiceImpl implements UserService {
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用微信API获取用户openid
|
||||
* @param code 微信登录凭证
|
||||
* @return 用户的openid
|
||||
*/
|
||||
private String getOpenIdFromWeChat(String code) {
|
||||
try {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String url = wxCode2SessionUrl + "?appid=" + wxAppId + "&secret=" + wxSecret + "&js_code=" + code + "&grant_type=authorization_code";
|
||||
|
||||
String response = restTemplate.getForObject(url, String.class);
|
||||
|
||||
// 解析微信返回的JSON数据
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode = objectMapper.readTree(response);
|
||||
|
||||
if (jsonNode.has("errcode")) {
|
||||
// 微信返回错误
|
||||
String errMsg = jsonNode.has("errmsg") ? jsonNode.get("errmsg").asText() : "未知错误";
|
||||
throw new RuntimeException("微信登录失败: " + errMsg);
|
||||
}
|
||||
|
||||
// 返回真实的openid
|
||||
return jsonNode.get("openid").asText();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("调用微信API获取openid失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户签到功能。
|
||||
* 更新指定用户的信息,模拟签到过程。
|
||||
|
42
src/main/resources/application-local.properties
Normal file
42
src/main/resources/application-local.properties
Normal file
@@ -0,0 +1,42 @@
|
||||
spring.application.name=Light-Local
|
||||
server.port=8080
|
||||
|
||||
# 使ç¨è¿ç¨æ°æ®åºï¼ä¸ç产ç¯å¢ç¸åï¼
|
||||
spring.datasource.url=jdbc:mysql://115.190.121.151:3306/light_delivery?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||
spring.datasource.username=double
|
||||
spring.datasource.password=Hu@ng1998!
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
|
||||
# æ°æ®åºè¿æ¥æ± é
ç½®
|
||||
spring.datasource.hikari.maximum-pool-size=5
|
||||
spring.datasource.hikari.minimum-idle=1
|
||||
spring.datasource.hikari.connection-timeout=20000
|
||||
spring.datasource.hikari.idle-timeout=300000
|
||||
spring.datasource.hikari.max-lifetime=600000
|
||||
|
||||
# JPAé
ç½®
|
||||
spring.jpa.hibernate.ddl-auto=none
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
|
||||
|
||||
# ç¦ç¨SSLä»¥ä¾¿äºæ¬å°æµè¯
|
||||
server.ssl.enabled=false
|
||||
# ç§»é¤ææSSLç¸å
³é
ç½®ä»¥é²æ¢Spring Boot 3.xå°è¯å è½½å®ä»¬
|
||||
|
||||
# 微信å°ç¨åºé
ç½®ï¼æ¬å°ç¯å¢ä¸ç产ç¯å¢ä½¿ç¨ç¸åé
ç½®ï¼
|
||||
wx.appId=wx1b790fd953ac27bf
|
||||
wx.secret=b38c40c89cc4954472fb45d1e7cc27a4
|
||||
wx.token=huang1998
|
||||
wx.aesKey=lwararkj1BgurX8Qown6yjGNsqd8dEIEddDnYN3iLgc
|
||||
|
||||
wx.api.access-token-url=https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential
|
||||
wx.api.code2session-url=https://api.weixin.qq.com/sns/jscode2session
|
||||
|
||||
jwt.secret=CRMO1KEF/YP07zoV8+X1F3/DfkvksgPUs6tdLRR8urWCEBPMEO+5nt50xRkRl8JBJursSnvv++qylsaz5zryuA==
|
||||
jwt.expiration=86400
|
||||
|
||||
logging.level.com.light.delivery=DEBUG
|
||||
|
||||
management.endpoints.web.exposure.include=health,info
|
||||
management.endpoint.health.show-details=always
|
Reference in New Issue
Block a user