微信连接,本地数据上传
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:
33
build-local.bat
Normal file
33
build-local.bat
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
@echo off
|
||||||
|
echo Building Light Delivery application with Docker...
|
||||||
|
|
||||||
|
echo Step 1: Compiling the project...
|
||||||
|
call mvn clean package -DskipTests
|
||||||
|
|
||||||
|
if %errorlevel% neq 0 (
|
||||||
|
echo Error: Maven build failed
|
||||||
|
exit /b %errorlevel%
|
||||||
|
)
|
||||||
|
|
||||||
|
echo Step 2: Building Docker image...
|
||||||
|
docker build -t light-delivery-app .
|
||||||
|
|
||||||
|
if %errorlevel% neq 0 (
|
||||||
|
echo Error: Docker build failed
|
||||||
|
exit /b %errorlevel%
|
||||||
|
)
|
||||||
|
|
||||||
|
echo Step 3: Verifying Docker image...
|
||||||
|
docker images | findstr light-delivery-app
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo Build completed successfully!
|
||||||
|
echo.
|
||||||
|
echo To run the container locally:
|
||||||
|
echo docker run -d --name light-delivery -p 8080:8080 -p 8443:8443 light-delivery-app
|
||||||
|
echo.
|
||||||
|
echo To export the image for cloud deployment:
|
||||||
|
echo docker save light-delivery-app -o light-delivery-app.tar
|
||||||
|
echo.
|
||||||
|
|
||||||
|
pause
|
38
build-with-docker.bat
Normal file
38
build-with-docker.bat
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
|
||||||
|
echo Building Light Delivery application with Docker...
|
||||||
|
|
||||||
|
REM 第一步:编译项目
|
||||||
|
echo Step 1: Compiling the project...
|
||||||
|
call mvn clean package -DskipTests
|
||||||
|
|
||||||
|
if %errorlevel% neq 0 (
|
||||||
|
echo Error: Maven build failed
|
||||||
|
exit /b %errorlevel%
|
||||||
|
)
|
||||||
|
|
||||||
|
REM 第二步:构建 Docker 镜像
|
||||||
|
echo Step 2: Building Docker image...
|
||||||
|
docker build -t light-delivery-app .
|
||||||
|
|
||||||
|
if %errorlevel% neq 0 (
|
||||||
|
echo Error: Docker build failed
|
||||||
|
exit /b %errorlevel%
|
||||||
|
)
|
||||||
|
|
||||||
|
REM 第三步:验证镜像
|
||||||
|
echo Step 3: Verifying Docker image...
|
||||||
|
docker images | findstr light-delivery-app
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo Build completed successfully!
|
||||||
|
echo.
|
||||||
|
echo To run the container locally:
|
||||||
|
echo docker run -d --name light-delivery -p 8080:8080 -p 8443:8443 light-delivery-app
|
||||||
|
echo.
|
||||||
|
echo To export the image for cloud deployment:
|
||||||
|
echo docker save light-delivery-app -o light-delivery-app.tar
|
||||||
|
echo.
|
||||||
|
|
||||||
|
pause
|
18
deploy.bat
Normal file
18
deploy.bat
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
@echo off
|
||||||
|
echo Starting deployment of light-delivery-app to cloud server...
|
||||||
|
|
||||||
|
echo Building Docker image...
|
||||||
|
docker build -t light-delivery-app .
|
||||||
|
|
||||||
|
echo Exporting image to tar file...
|
||||||
|
docker save light-delivery-app -o light-delivery-app.tar
|
||||||
|
|
||||||
|
echo Uploading image to cloud server...
|
||||||
|
scp light-delivery-app.tar root@115.190.121.151:/tmp/
|
||||||
|
|
||||||
|
echo Deploying application on cloud server...
|
||||||
|
ssh root@115.190.121.151 "docker load -i /tmp/light-delivery-app.tar && docker stop light-delivery 2>nul && docker rm light-delivery 2>nul && docker run -d --name light-delivery -p 8080:8080 -p 8443:8443 light-delivery-app && docker ps | findstr light-delivery"
|
||||||
|
|
||||||
|
echo Deployment completed!
|
||||||
|
|
||||||
|
pause
|
41
deploy.sh
Normal file
41
deploy.sh
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 部署脚本
|
||||||
|
echo "开始部署 light-delivery-app 到云服务器..."
|
||||||
|
|
||||||
|
# 重新构建Docker镜像
|
||||||
|
echo "构建Docker镜像..."
|
||||||
|
docker build -t light-delivery-app .
|
||||||
|
|
||||||
|
# 导出镜像为tar文件
|
||||||
|
echo "导出镜像为tar文件..."
|
||||||
|
docker save light-delivery-app -o light-delivery-app.tar
|
||||||
|
|
||||||
|
# 上传到云服务器
|
||||||
|
echo "上传镜像到云服务器..."
|
||||||
|
scp light-delivery-app.tar root@115.190.121.151:/tmp/
|
||||||
|
|
||||||
|
# 在云服务器上执行部署命令
|
||||||
|
echo "在云服务器上部署应用..."
|
||||||
|
ssh root@115.190.121.151 << 'EOF'
|
||||||
|
echo "加载Docker镜像..."
|
||||||
|
docker load -i /tmp/light-delivery-app.tar
|
||||||
|
|
||||||
|
echo "停止并删除旧容器..."
|
||||||
|
docker stop light-delivery || true
|
||||||
|
docker rm light-delivery || true
|
||||||
|
|
||||||
|
echo "运行新容器..."
|
||||||
|
docker run -d \
|
||||||
|
--name light-delivery \
|
||||||
|
-p 8080:8080 \
|
||||||
|
-p 8443:8443 \
|
||||||
|
light-delivery-app
|
||||||
|
|
||||||
|
echo "查看容器状态..."
|
||||||
|
docker ps | grep light-delivery
|
||||||
|
|
||||||
|
echo "部署完成!"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo "部署脚本执行完成。"
|
@@ -1,5 +1,7 @@
|
|||||||
package com.light.delivery.service.impl;
|
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.dto.UserInfoResponse;
|
||||||
import com.light.delivery.model.Employee;
|
import com.light.delivery.model.Employee;
|
||||||
import com.light.delivery.model.LoginResponse;
|
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.service.UserService;
|
||||||
import com.light.delivery.util.JwtUtil;
|
import com.light.delivery.util.JwtUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -43,6 +47,24 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private JwtUtil jwtUtil;
|
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令牌获取用户信息。
|
* 根据JWT令牌获取用户信息。
|
||||||
* 从token中解析出用户的openid,然后查询数据库获取完整的用户信息。
|
* 从token中解析出用户的openid,然后查询数据库获取完整的用户信息。
|
||||||
@@ -99,8 +121,8 @@ public class UserServiceImpl implements UserService {
|
|||||||
throw new IllegalArgumentException("登录code不能为空");
|
throw new IllegalArgumentException("登录code不能为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 模拟微信登录,实际应该调用微信API获取 openid
|
// 调用微信API获取真实的openid
|
||||||
String openid = "openid_" + code; // 模拟 openid
|
String openid = getOpenIdFromWeChat(code);
|
||||||
|
|
||||||
User user = userRepository.findByOpenid(openid);
|
User user = userRepository.findByOpenid(openid);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
@@ -133,6 +155,35 @@ public class UserServiceImpl implements UserService {
|
|||||||
return response;
|
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
|
18
transfer-base-image.bat
Normal file
18
transfer-base-image.bat
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
@echo off
|
||||||
|
echo Transferring base OpenJDK image to cloud server...
|
||||||
|
|
||||||
|
echo Exporting openjdk:17-jdk-slim image to tar file...
|
||||||
|
docker save openjdk:17-jdk-slim -o openjdk-17-jdk-slim.tar
|
||||||
|
|
||||||
|
echo Uploading openjdk image to cloud server...
|
||||||
|
scp openjdk-17-jdk-slim.tar root@115.190.121.151:/tmp/
|
||||||
|
|
||||||
|
echo Loading image on cloud server...
|
||||||
|
ssh root@115.190.121.151 "docker load -i /tmp/openjdk-17-jdk-slim.tar"
|
||||||
|
|
||||||
|
echo Verifying image on cloud server...
|
||||||
|
ssh root@115.190.121.151 "docker images | grep openjdk"
|
||||||
|
|
||||||
|
echo Base image transfer completed!
|
||||||
|
|
||||||
|
pause
|
Reference in New Issue
Block a user