Compare commits

...

4 Commits

Author SHA1 Message Date
科技小王子 a9758b9d3a refactor(shop): 优化商城信息获取和缓存逻辑 3 days ago
科技小王子 ec2316625e refactor(wx-login): 重构微信登录和小程序码生成逻辑 3 days ago
科技小王子 cff719a80b Merge branch 'main' into dev 4 days ago
科技小王子 7ec7522357 feat(wx): 添加微信小程序码生成功能 4 days ago
  1. 14
      Dockerfile
  2. 59
      docker-compose.yml
  3. 12
      pom.xml
  4. 7
      src/main/java/com/gxwebsoft/cms/controller/CmsAdController.java
  5. 3
      src/main/java/com/gxwebsoft/cms/entity/CmsAd.java
  6. 3
      src/main/java/com/gxwebsoft/cms/mapper/xml/CmsAdMapper.xml
  7. 4
      src/main/java/com/gxwebsoft/cms/param/CmsAdParam.java
  8. 6
      src/main/java/com/gxwebsoft/cms/service/CmsAdService.java
  9. 7
      src/main/java/com/gxwebsoft/cms/service/impl/CmsAdServiceImpl.java
  10. 1
      src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImpl.java
  11. 17
      src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImplHelper.java
  12. 2
      src/main/java/com/gxwebsoft/common/core/utils/WxUtil.java
  13. 273
      src/main/java/com/gxwebsoft/common/system/controller/WxLoginController.java
  14. 10
      src/main/java/com/gxwebsoft/common/system/service/SettingService.java
  15. 56
      src/main/java/com/gxwebsoft/common/system/service/impl/SettingServiceImpl.java
  16. 87
      src/main/java/com/gxwebsoft/house/service/impl/HouseInfoServiceImpl.java
  17. 1
      src/main/java/com/gxwebsoft/shop/controller/ShopMainController.java
  18. 5
      src/main/java/com/gxwebsoft/shop/service/impl/ShopWebsiteServiceImpl.java
  19. 18
      src/main/java/com/gxwebsoft/shop/vo/MenuVo.java
  20. 8
      src/main/resources/application-prod.yml
  21. 2
      src/main/resources/application.yml
  22. 110
      src/test/java/com/gxwebsoft/WxDev.java
  23. 52
      src/test/java/com/gxwebsoft/hjm/MqttServiceTest.java

14
Dockerfile

@ -1,21 +1,19 @@
# 使用OpenJDK 17作为基础镜像
FROM openjdk:17-jre-alpine
# 使用更小的 Alpine Linux + OpenJDK 17 镜像
FROM openjdk:17-jdk-alpine
# 设置工作目录
WORKDIR /app
# 创建证书目录
RUN mkdir -p /app/certs
# 创建日志目录
RUN mkdir -p /app/logs
# 创建上传文件目录
RUN mkdir -p /app/uploads
# 添加应用用户(安全考虑)
RUN addgroup -g 1000 appgroup && \
adduser -D -s /bin/sh -u 1000 -G appgroup appuser
# 安装wget用于健康检查,并添加应用用户(安全考虑)
RUN apk add --no-cache wget && \
addgroup -g 1000 appgroup && \
adduser -D -u 1000 -G appgroup appuser
# 复制jar包到容器
COPY target/*.jar app.jar

59
docker-compose.yml

@ -2,9 +2,9 @@ version: '3.8'
services:
# 应用服务
cms-app:
cms-api:
build: .
container_name: cms-java-app
container_name: cms-api
ports:
- "9200:9200"
environment:
@ -19,9 +19,6 @@ services:
- ./uploads:/app/uploads
networks:
- cms-network
depends_on:
- mysql
- redis
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:9200/actuator/health"]
@ -30,58 +27,6 @@ services:
retries: 3
start_period: 60s
# MySQL数据库
mysql:
image: mysql:8.0
container_name: cms-mysql
environment:
MYSQL_ROOT_PASSWORD: root123456
MYSQL_DATABASE: modules
MYSQL_USER: modules
MYSQL_PASSWORD: 8YdLnk7KsPAyDXGA
ports:
- "3308:3306"
volumes:
- mysql_data:/var/lib/mysql
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/init:/docker-entrypoint-initdb.d
networks:
- cms-network
restart: unless-stopped
command: --default-authentication-plugin=mysql_native_password
# Redis缓存
redis:
image: redis:6.2-alpine
container_name: cms-redis
ports:
- "16379:6379"
volumes:
- redis_data:/data
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf
networks:
- cms-network
restart: unless-stopped
command: redis-server /usr/local/etc/redis/redis.conf
# Nginx反向代理(可选)
nginx:
image: nginx:alpine
container_name: cms-nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/ssl:/etc/nginx/ssl
- ./uploads:/var/www/uploads
networks:
- cms-network
depends_on:
- cms-app
restart: unless-stopped
networks:
cms-network:
driver: bridge

12
pom.xml

@ -340,6 +340,18 @@
<version>0.2.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
<!-- 可选:用来做内存缓存 access_token -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>com.freewayso</groupId>
<artifactId>image-combiner</artifactId>

7
src/main/java/com/gxwebsoft/cms/controller/CmsAdController.java

@ -50,6 +50,13 @@ public class CmsAdController extends BaseController {
return success(ad);
}
@Operation(summary = "根据code查询广告位")
@GetMapping("/getByCode/{code}")
public ApiResult<CmsAd> getByCode(@PathVariable("code") String code) {
final CmsAd ad = cmsAdService.getByIdCode(code);
return success(ad);
}
@Operation(summary = "添加广告位")
@PostMapping()
public ApiResult<?> save(@RequestBody CmsAd cmsAd) {

3
src/main/java/com/gxwebsoft/cms/entity/CmsAd.java

@ -39,6 +39,9 @@ public class CmsAd implements Serializable {
@Schema(description = "类型")
private Integer type;
@Schema(description = "唯一标识")
private String code;
@Schema(description = "栏目ID")
private Integer categoryId;

3
src/main/java/com/gxwebsoft/cms/mapper/xml/CmsAdMapper.xml

@ -15,6 +15,9 @@
<if test="param.type != null">
AND a.type = #{param.type}
</if>
<if test="param.code != null">
AND a.code = #{param.code}
</if>
<if test="param.categoryId != null">
AND a.category_id = #{param.categoryId}
</if>

4
src/main/java/com/gxwebsoft/cms/param/CmsAdParam.java

@ -29,6 +29,10 @@ public class CmsAdParam extends BaseParam {
@Schema(description = "类型")
private Integer type;
@Schema(description = "唯一标识")
@QueryField(type = QueryType.EQ)
private String code;
@Schema(description = "栏目ID")
@QueryField(type = QueryType.EQ)
private Integer categoryId;

6
src/main/java/com/gxwebsoft/cms/service/CmsAdService.java

@ -39,4 +39,10 @@ public interface CmsAdService extends IService<CmsAd> {
*/
CmsAd getByIdRel(Integer adId);
/**
* 根据code查询
*
* @return CmsAd
*/
CmsAd getByIdCode(String code);
}

7
src/main/java/com/gxwebsoft/cms/service/impl/CmsAdServiceImpl.java

@ -47,4 +47,11 @@ public class CmsAdServiceImpl extends ServiceImpl<CmsAdMapper, CmsAd> implements
return param.getOne(baseMapper.selectListRel(param));
}
@Override
public CmsAd getByIdCode(String code) {
CmsAdParam param = new CmsAdParam();
param.setCode(code);
return param.getOne(baseMapper.selectListRel(param));
}
}

1
src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImpl.java

@ -334,6 +334,7 @@ public class CmsWebsiteServiceImpl extends ServiceImpl<CmsWebsiteMapper, CmsWebs
// 从数据库获取站点信息
CmsWebsite website = getWebsiteFromDatabase(tenantId);
if (website == null) {
throw new RuntimeException("请先创建站点");
}

17
src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImplHelper.java

@ -119,6 +119,7 @@ public class CmsWebsiteServiceImplHelper {
/**
* 转换导航列表为VO
* 整理导航栏目录结构(ShopInfo)
*/
public static List<MenuVo> convertNavigationToVO(List<CmsNavigation> navigations) {
if (navigations == null) {
@ -127,18 +128,18 @@ public class CmsWebsiteServiceImplHelper {
return navigations.stream().map(nav -> {
MenuVo navVO = new MenuVo();
navVO.setNavigationId(nav.getNavigationId());
navVO.setNavigationName(nav.getTitle()); // 修复:使用 title 字段
navVO.setNavigationUrl(nav.getPath()); // 修复:使用 path 字段
navVO.setNavigationIcon(nav.getIcon()); // 修复:使用 icon 字段
navVO.setNavigationColor(nav.getColor()); // 修复:使用 color 字段
navVO.setId(nav.getNavigationId());
navVO.setName(nav.getTitle());
navVO.setPath(nav.getPath());
navVO.setIcon(nav.getIcon());
navVO.setColor(nav.getColor());
navVO.setParentId(nav.getParentId());
navVO.setSort(nav.getSortNumber()); // 修复:使用 sortNumber 字段
navVO.setSort(nav.getSortNumber());
navVO.setHide(nav.getHide());
navVO.setTop(nav.getTop());
// 安全转换 target 字段:将字符串值映射为整数
navVO.setPath(nav.getPath());
navVO.setTarget(convertTargetToInteger(nav.getTarget()));
navVO.setNavigationType(nav.getModel()); // 修复:使用 model 字段
navVO.setType(nav.getModel());
// 递归处理子导航
if (nav.getChildren() != null) {

2
src/main/java/com/gxwebsoft/common/core/utils/WxUtil.java

@ -127,4 +127,6 @@ public class WxUtil {
this.qr_code = jsonObject.getString("qr_code");
this.open_userid = jsonObject.getString("open_userid");
}
}

273
src/main/java/com/gxwebsoft/common/system/controller/WxLoginController.java

@ -1,15 +1,15 @@
package com.gxwebsoft.common.system.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxwebsoft.common.core.config.ConfigProperties;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.security.JwtSubject;
@ -25,14 +25,20 @@ import com.gxwebsoft.common.system.result.LoginResult;
import com.gxwebsoft.common.system.service.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import okhttp3.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.gxwebsoft.common.core.constants.PlatformConstants.MP_WEIXIN;
@ -43,6 +49,9 @@ import static com.gxwebsoft.common.core.constants.RedisConstants.ACCESS_TOKEN_KE
@Tag(name = "微信小程序登录API")
public class WxLoginController extends BaseController {
private final StringRedisTemplate redisTemplate;
private final OkHttpClient http = new OkHttpClient();
private final ObjectMapper om = new ObjectMapper();
private volatile long tokenExpireEpoch = 0L; // 过期的 epoch 秒
@Resource
private SettingService settingService;
@Resource
@ -64,6 +73,8 @@ public class WxLoginController extends BaseController {
@Resource
private UserRefereeService userRefereeService;
public WxLoginController(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@ -118,7 +129,6 @@ public class WxLoginController extends BaseController {
UserParam userParam2 = new UserParam();
userParam2.setCode(userParam.getAuthCode());
JSONObject result = getOpenIdByCode(userParam2);
System.out.println("userInfo res:" + result);
String openid = result.getString("openid");
// String unionid = result.getString("unionid");
userParam.setOpenid(openid);
@ -215,7 +225,7 @@ public class WxLoginController extends BaseController {
// 获取openid
private JSONObject getOpenIdByCode(UserParam userParam) {
// 获取微信小程序配置信息
JSONObject setting = settingService.getBySettingKey("mp-weixin");
JSONObject setting = settingService.getBySettingKey("mp-weixin",getTenantId());
// 获取openId
String apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + setting.getString("appId") + "&secret=" + setting.getString("appSecret") + "&js_code=" + userParam.getCode() + "&grant_type=authorization_code";
// 执行get请求
@ -268,27 +278,36 @@ public class WxLoginController extends BaseController {
* 获取接口调用凭据AccessToken
* <a href="https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html">...</a>
*/
private String getAccessToken() {
String key = ACCESS_TOKEN_KEY.concat(":").concat(getTenantId().toString());
// 获取微信小程序配置信息
JSONObject setting = settingService.getBySettingKey("mp-weixin");
public String getAccessToken() {
Integer tenantId = getTenantId();
String key = ACCESS_TOKEN_KEY.concat(":").concat(tenantId.toString());
// 使用跨租户方式获取微信小程序配置信息
JSONObject setting = settingService.getBySettingKeyIgnoreTenant("mp-weixin", tenantId);
if (setting == null) {
throw new BusinessException("请先配置小程序");
}
// 从缓存获取access_token
String value = redisTemplate.opsForValue().get(key);
if (value != null) {
// 解析access_token
JSONObject response = JSON.parseObject(value);
// return response.getString("access_token");
String accessToken = response.getString("access_token");
if (accessToken != null) {
return accessToken;
}
}
// 微信获取凭证接口
String apiUrl = "https://api.weixin.qq.com/cgi-bin/token";
// 组装url参数
String url = apiUrl.concat("?grant_type=client_credential").concat("&appid=").concat(setting.getString("appId")).concat("&secret=").concat(setting.getString("appSecret"));
String url = apiUrl.concat("?grant_type=client_credential")
.concat("&appid=").concat(setting.getString("appId"))
.concat("&secret=").concat(setting.getString("appSecret"));
// 执行get请求
String result = HttpUtil.get(url);
System.out.println("result = " + result);
// 解析access_token
JSONObject response = JSON.parseObject(result);
if (response.getString("access_token") != null) {
@ -313,7 +332,7 @@ public class WxLoginController extends BaseController {
// 请求微信接口获取openid
String apiUrl = "https://api.weixin.qq.com/sns/jscode2session";
final HashMap<String, Object> map = new HashMap<>();
final JSONObject setting = settingService.getBySettingKey("mp-weixin");
final JSONObject setting = settingService.getBySettingKey("mp-weixin",getTenantId());
final String appId = setting.getString("appId");
final String appSecret = setting.getString("appSecret");
map.put("appid", appId);
@ -341,7 +360,7 @@ public class WxLoginController extends BaseController {
String apiUrl = "https://api.weixin.qq.com/sns/jscode2session";
final HashMap<String, Object> map = new HashMap<>();
final JSONObject setting = settingService.getBySettingKey("mp-weixin");
final JSONObject setting = settingService.getBySettingKey("mp-weixin",getTenantId());
final String appId = setting.getString("appId");
final String appSecret = setting.getString("appSecret");
map.put("appid", appId);
@ -400,28 +419,147 @@ public class WxLoginController extends BaseController {
}
@Operation(summary = "获取微信小程序码-订单核销码-数量极多的业务场景")
@GetMapping("/getOrderQRCodeUnlimited/{orderNo}")
public ApiResult<?> getOrderQRCodeUnlimited(@PathVariable("orderNo") String orderNo) {
String apiUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + getAccessToken();
final HashMap<String, Object> map = new HashMap<>();
map.put("scene", "orderNo=".concat(orderNo));
map.put("page", "package/admin/order-scan");
map.put("env_version", "trial");
// 获取图片 Buffer
byte[] qrCode = HttpRequest.post(apiUrl)
.body(JSON.toJSONString(map))
.execute().bodyBytes();
System.out.println("qrCode = " + qrCode);
@GetMapping("/getOrderQRCodeUnlimited/{scene}")
public void getOrderQRCodeUnlimited(@PathVariable("scene") String scene, HttpServletResponse response) throws IOException {
System.out.println("scene = " + scene);
// 保存的文件名称
final String fileName = CommonUtil.randomUUID8().concat(".png");
// 保存路径
String filePath = getUploadDir().concat("qrcode/") + fileName;
File file = FileUtil.writeBytes(qrCode, filePath);
if (file != null) {
return success(config.getFileServer().concat("/qrcode/").concat(fileName));
try {
// 使用统一的 access_token 获取方法
String accessToken = getAccessToken();
String apiUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
final HashMap<String, Object> map = new HashMap<>();
map.put("scene", scene);
map.put("page", "pages/index/index");
map.put("env_version", "trial");
String jsonBody = JSON.toJSONString(map);
System.out.println("请求的 JSON body = " + jsonBody);
// 获取微信 API 响应
cn.hutool.http.HttpResponse httpResponse = HttpRequest.post(apiUrl)
.body(jsonBody)
.execute();
byte[] responseBytes = httpResponse.bodyBytes();
String contentType = httpResponse.header("Content-Type");
// 检查响应内容类型,判断是否为错误响应
if (contentType != null && contentType.contains("application/json")) {
// 微信返回了错误信息(JSON格式)
String errorResponse = new String(responseBytes, "UTF-8");
System.err.println("微信 API 错误响应: " + errorResponse);
// 返回错误信息给前端
response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().write(errorResponse);
return;
}
// 成功获取二维码图片
response.setContentType("image/png");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition", "inline; filename=qrcode.png");
// 输出图片
response.getOutputStream().write(responseBytes);
System.out.println("二维码生成成功,大小: " + responseBytes.length + " bytes");
} catch (Exception e) {
System.err.println("生成二维码失败: " + e.getMessage());
e.printStackTrace();
// 返回错误信息
response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write("{\"error\":\"生成二维码失败: " + e.getMessage() + "\"}");
}
}
@Operation(summary = "获取微信小程序码-用户ID")
@GetMapping("/getQRCodeText")
public byte[] getQRCodeText(String scene, String page, Integer width,
Boolean isHyaline, String envVersion) throws IOException {
HttpUrl url = HttpUrl.parse("https://api.weixin.qq.com/wxa/getwxacodeunlimit")
.newBuilder()
.addQueryParameter("access_token", getLocalAccessToken())
.build();
System.out.println("page = " + page);
// 构造请求 JSON
// 注意:scene 仅支持可见字符,长度上限 32,尽量 URL-safe(字母数字下划线等)
// page 必须是已发布小程序内的路径(不带开头斜杠也可)
var root = om.createObjectNode();
root.put("scene", scene);
if (page != null) root.put("page", page);
if (width != null) root.put("width", width); // 默认 430,建议 280~1280
if (isHyaline != null) root.put("is_hyaline", isHyaline);
if (envVersion != null) root.put("env_version", envVersion); // release/trial/develop
okhttp3.RequestBody reqBody = okhttp3.RequestBody.create(
root.toString(), MediaType.parse("application/json; charset=utf-8"));
Request req = new Request.Builder().url(url).post(reqBody).build();
try (Response resp = http.newCall(req).execute()) {
if (!resp.isSuccessful()) {
throw new IOException("HTTP " + resp.code() + " calling getwxacodeunlimit");
}
MediaType ct = resp.body().contentType();
byte[] bytes = resp.body().bytes();
// 微信出错时返回 JSON,需要识别一下
if (ct != null && ct.subtype() != null && ct.subtype().contains("json")) {
String err = new String(bytes);
throw new IOException("WeChat error: " + err);
}
return bytes; // 成功就是图片二进制(PNG)
}
}
/** 获取/刷新 access_token */
public String getLocalAccessToken() throws IOException {
long now = Instant.now().getEpochSecond();
String key = "AccessToken:Local:" + getTenantId();
if (redisUtil.get(key) != null && now < tokenExpireEpoch - 60) {
return redisUtil.get(key);
}
// 使用跨租户方式获取微信小程序配置信息
Integer tenantId = getTenantId();
JSONObject setting = settingService.getBySettingKeyIgnoreTenant("mp-weixin", tenantId);
if (setting == null) {
throw new IOException("请先配置小程序");
}
String appId = setting.getString("appId");
String appSecret = setting.getString("appSecret");
if (appId == null || appSecret == null) {
throw new IOException("小程序配置不完整,缺少 appId 或 appSecret");
}
HttpUrl url = HttpUrl.parse("https://api.weixin.qq.com/cgi-bin/token")
.newBuilder()
.addQueryParameter("grant_type", "client_credential")
.addQueryParameter("appid", appId)
.addQueryParameter("secret", appSecret)
.build();
Request req = new Request.Builder().url(url).get().build();
try (Response resp = http.newCall(req).execute()) {
String body = resp.body().string();
JsonNode json = om.readTree(body);
if (json.has("access_token")) {
String token = json.get("access_token").asText();
long expiresIn = json.get("expires_in").asInt(7200);
redisUtil.set(key, token, expiresIn, TimeUnit.SECONDS);
tokenExpireEpoch = now + expiresIn;
return token;
} else {
throw new IOException("Get access_token failed: " + body);
}
}
return fail("获取失败", null);
}
/**
@ -431,5 +569,74 @@ public class WxLoginController extends BaseController {
return config.getUploadPath() + "file/";
}
@Operation(summary = "调试:检查微信小程序配置")
@GetMapping("/debug/checkWxConfig")
public ApiResult<?> debugCheckWxConfig() {
Integer tenantId = getTenantId();
Map<String, Object> result = new HashMap<>();
result.put("tenantId", tenantId);
try {
// 尝试获取配置
JSONObject setting = settingService.getBySettingKeyIgnoreTenant("mp-weixin", tenantId);
result.put("hasConfig", true);
result.put("config", setting);
} catch (Exception e) {
result.put("hasConfig", false);
result.put("error", e.getMessage());
// 提供创建配置的建议
Map<String, Object> suggestion = new HashMap<>();
suggestion.put("message", "请在系统设置中创建微信小程序配置");
suggestion.put("configKey", "mp-weixin");
suggestion.put("tenantId", tenantId);
suggestion.put("sampleConfig", createSampleWxConfig());
result.put("suggestion", suggestion);
}
return success("配置检查完成", result);
}
@Operation(summary = "调试:创建示例微信小程序配置")
@PostMapping("/debug/createSampleWxConfig")
public ApiResult<?> debugCreateSampleWxConfig(@RequestBody Map<String, String> params) {
Integer tenantId = getTenantId();
String appId = params.get("appId");
String appSecret = params.get("appSecret");
if (appId == null || appSecret == null) {
return fail("请提供 appId 和 appSecret", null);
}
try {
// 创建配置对象
Setting setting = new Setting();
setting.setSettingKey("mp-weixin");
setting.setTenantId(tenantId);
// 创建配置内容
Map<String, String> config = new HashMap<>();
config.put("appId", appId);
config.put("appSecret", appSecret);
setting.setContent(JSON.toJSONString(config));
setting.setComments("微信小程序配置");
setting.setSortNumber(1);
// 保存配置
settingService.save(setting);
return success("微信小程序配置创建成功", setting);
} catch (Exception e) {
return fail("创建配置失败: " + e.getMessage(), null);
}
}
private Map<String, String> createSampleWxConfig() {
Map<String, String> sample = new HashMap<>();
sample.put("appId", "wx_your_app_id_here");
sample.put("appSecret", "your_app_secret_here");
return sample;
}
}

10
src/main/java/com/gxwebsoft/common/system/service/SettingService.java

@ -46,7 +46,15 @@ public interface SettingService extends IService<Setting> {
* @param key key
* @return Setting
*/
JSONObject getBySettingKey(String key);
JSONObject getBySettingKey(String key,Integer tenantId);
/**
* 跨租户获取设置内容
* @param key 设置键
* @param tenantId 租户ID
* @return JSONObject
*/
JSONObject getBySettingKeyIgnoreTenant(String key, Integer tenantId);
Setting getData(String settingKey);

56
src/main/java/com/gxwebsoft/common/system/service/impl/SettingServiceImpl.java

@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.annotation.IgnoreTenant;
import com.gxwebsoft.common.core.config.ConfigProperties;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.web.PageParam;
@ -16,6 +17,7 @@ import com.gxwebsoft.common.system.service.SettingService;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@ -44,8 +46,10 @@ public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> impl
@Value("${spring.profiles.active:prod}")
private String activeProfile;
@Autowired
private SettingService settingService;
@Override
@Override
public PageResult<Setting> pageRel(SettingParam param) {
PageParam<Setting, SettingParam> page = new PageParam<>(param);
//page.setDefaultOrder("create_time desc");
@ -70,11 +74,15 @@ public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> impl
}
@Override
public JSONObject getBySettingKey(String key) {
public JSONObject getBySettingKey(String key, Integer tenantId) {
System.out.println("tenantId = " + tenantId);
final JSONObject settingKey = settingService.getBySettingKey("setting_key", tenantId);
System.out.println("settingKey = " + settingKey);
Setting setting = this.getOne(new QueryWrapper<Setting>().eq("setting_key", key), false);
System.out.println("setting1 = " + setting);
if(setting == null){
if ("mp-weixin".equals(key)) {
throw new BusinessException("小程序未配置");
throw new BusinessException("小程序未配置1");
}
if ("payment".equals(key)) {
throw new BusinessException("支付未配置");
@ -98,6 +106,48 @@ public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> impl
return JSON.parseObject(setting.getContent());
}
@Override
@IgnoreTenant("跨租户获取指定租户的设置配置")
public JSONObject getBySettingKeyIgnoreTenant(String key, Integer tenantId) {
System.out.println("跨租户查询设置 - key: " + key + ", tenantId: " + tenantId);
final List<Setting> list = list(new LambdaQueryWrapper<Setting>().eq(Setting::getTenantId, tenantId));
System.out.println("list = " + list);
// 使用跨租户查询,指定租户ID
Setting setting = this.getOne(new QueryWrapper<Setting>()
.eq("setting_key", key)
.eq("tenant_id", tenantId), false);
System.out.println("跨租户查询结果: " + setting);
if(setting == null){
if ("mp-weixin".equals(key)) {
throw new BusinessException("租户 " + tenantId + " 的小程序未配置,请先在系统设置中配置微信小程序信息");
}
if ("payment".equals(key)) {
throw new BusinessException("租户 " + tenantId + " 的支付未配置");
}
if ("sms".equals(key)) {
throw new BusinessException("租户 " + tenantId + " 的短信未配置");
}
if ("wx-work".equals(key)){
throw new BusinessException("租户 " + tenantId + " 的企业微信未配置");
}
if ("setting".equals(key)) {
throw new BusinessException("租户 " + tenantId + " 的基本信息未配置");
}
if ("wx-official".equals(key)) {
throw new BusinessException("租户 " + tenantId + " 的微信公众号未配置");
}
if ("printer".equals(key)) {
throw new BusinessException("租户 " + tenantId + " 的打印机未配置");
}
throw new BusinessException("租户 " + tenantId + " 的配置项 " + key + " 未找到");
}
return JSON.parseObject(setting.getContent());
}
@Override
public Setting getData(String settingKey) {
return query().eq("setting_key", settingKey).one();

87
src/main/java/com/gxwebsoft/house/service/impl/HouseInfoServiceImpl.java

@ -16,6 +16,7 @@ import com.gxwebsoft.common.core.config.ConfigProperties;
import com.gxwebsoft.common.core.utils.CommonUtil;
import com.gxwebsoft.common.core.utils.ImageUtil;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.system.controller.WxLoginController;
import com.gxwebsoft.common.system.service.SettingService;
import com.gxwebsoft.house.mapper.HouseInfoMapper;
import com.gxwebsoft.house.service.HouseInfoService;
@ -57,6 +58,8 @@ public class HouseInfoServiceImpl extends ServiceImpl<HouseInfoMapper, HouseInfo
@Resource
private RedisUtil redisUtil;
@Resource
private WxLoginController wxLoginController;
private static final String ACCESS_TOKEN_KEY = "cache:wx:access_token";
@ -230,7 +233,7 @@ public class HouseInfoServiceImpl extends ServiceImpl<HouseInfoMapper, HouseInfo
*/
private String generateMiniProgramQRCode(Integer houseId) {
try {
String apiUrl = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + getAccessToken();
String apiUrl = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + wxLoginController.getAccessToken();
final HashMap<String, Object> map = new HashMap<>();
// 设置小程序页面路径:sub_pages/house/detail/ + houseId
map.put("path", "sub_pages/house/detail/?houseId=" + houseId);
@ -269,47 +272,47 @@ public class HouseInfoServiceImpl extends ServiceImpl<HouseInfoMapper, HouseInfo
*
* @return access_token
*/
private String getAccessToken() {
String key = ACCESS_TOKEN_KEY.concat(":").concat("1"); // 这里可以根据实际情况获取tenantId
System.out.println("key = " + key);
// 获取微信小程序配置信息
JSONObject setting = settingService.getBySettingKey("mp-weixin");
if (setting == null) {
throw new RuntimeException("请先配置小程序");
}
// 从缓存获取access_token
String value = redisUtil.get(key);
System.out.println("redisTemplate-value = " + value);
if (value != null) {
JSONObject response = JSON.parseObject(value);
String accessToken = response.getString("access_token");
if (StrUtil.isNotBlank(accessToken)) {
return accessToken;
}
}
// 微信获取凭证接口
String apiUrl = "https://api.weixin.qq.com/cgi-bin/token";
// 组装url参数
String url = apiUrl.concat("?grant_type=client_credential")
.concat("&appid=").concat(setting.getString("appId"))
.concat("&secret=").concat(setting.getString("appSecret"));
// 执行get请求
String result = cn.hutool.http.HttpUtil.get(url);
System.out.println("获取access_token结果: " + result);
// 解析access_token
JSONObject response = JSON.parseObject(result);
if (response.getString("access_token") != null) {
// 存入缓存
redisUtil.set(key, result, 7000L, TimeUnit.SECONDS);
return response.getString("access_token");
}
throw new RuntimeException("小程序配置不正确");
}
// private String getAccessToken() {
// String key = ACCESS_TOKEN_KEY.concat(":").concat("1"); // 这里可以根据实际情况获取tenantId
// System.out.println("key = " + key);
// // 获取微信小程序配置信息
// JSONObject setting = settingService.getBySettingKey("mp-weixin");
// if (setting == null) {
// throw new RuntimeException("请先配置小程序");
// }
//
// // 从缓存获取access_token
// String value = redisUtil.get(key);
// System.out.println("redisTemplate-value = " + value);
// if (value != null) {
// JSONObject response = JSON.parseObject(value);
// String accessToken = response.getString("access_token");
// if (StrUtil.isNotBlank(accessToken)) {
// return accessToken;
// }
// }
//
// // 微信获取凭证接口
// String apiUrl = "https://api.weixin.qq.com/cgi-bin/token";
// // 组装url参数
// String url = apiUrl.concat("?grant_type=client_credential")
// .concat("&appid=").concat(setting.getString("appId"))
// .concat("&secret=").concat(setting.getString("appSecret"));
//
// // 执行get请求
// String result = cn.hutool.http.HttpUtil.get(url);
// System.out.println("获取access_token结果: " + result);
//
// // 解析access_token
// JSONObject response = JSON.parseObject(result);
// if (response.getString("access_token") != null) {
// // 存入缓存
// redisUtil.set(key, result, 7000L, TimeUnit.SECONDS);
// return response.getString("access_token");
// }
//
// throw new RuntimeException("小程序配置不正确");
// }
/**
* 文件上传位置(服务器)

1
src/main/java/com/gxwebsoft/shop/controller/ShopMainController.java

@ -42,6 +42,7 @@ public class ShopMainController extends BaseController {
try {
// 使用专门的商城信息获取方法
ShopVo shopVo = shopWebsiteService.getShopInfo(tenantId);
// log.debug("获取商城信息成功: {}", shopVo);
return success(shopVo);
} catch (IllegalArgumentException e) {
return fail(e.getMessage(), null);

5
src/main/java/com/gxwebsoft/shop/service/impl/ShopWebsiteServiceImpl.java

@ -2,9 +2,7 @@ package com.gxwebsoft.shop.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.gxwebsoft.cms.entity.CmsWebsite;
import com.gxwebsoft.cms.service.CmsWebsiteService;
import com.gxwebsoft.cms.service.impl.CmsWebsiteServiceImplHelper;
import com.gxwebsoft.common.core.utils.JSONUtil;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.shop.service.ShopWebsiteService;
@ -34,7 +32,7 @@ public class ShopWebsiteServiceImpl implements ShopWebsiteService {
/**
* 商城信息缓存键前缀
*/
private static final String SHOP_INFO_KEY_PREFIX = "shop_info:";
private static final String SHOP_INFO_KEY_PREFIX = "ShopInfo:";
@Override
public ShopVo getShopInfo(Integer tenantId) {
@ -57,7 +55,6 @@ public class ShopWebsiteServiceImpl implements ShopWebsiteService {
// 直接调用 CMS 服务获取站点信息,然后使用商城专用缓存
ShopVo shopVO = cmsWebsiteService.getSiteInfo(tenantId);
if (shopVO == null) {
throw new RuntimeException("请先创建商城");
}

18
src/main/java/com/gxwebsoft/shop/vo/MenuVo.java

@ -18,19 +18,22 @@ import java.util.List;
public class MenuVo implements Serializable {
@Schema(description = "导航ID")
private Integer navigationId;
private Integer id;
@Schema(description = "导航名称")
private String navigationName;
private String name;
@Schema(description = "导航链接")
private String navigationUrl;
@Schema(description = "导航类型")
private String type;
@Schema(description = "路由地址")
private String path;
@Schema(description = "导航图标")
private String navigationIcon;
private String icon;
@Schema(description = "导航颜色")
private String navigationColor;
private String color;
@Schema(description = "父级ID")
private Integer parentId;
@ -47,9 +50,6 @@ public class MenuVo implements Serializable {
@Schema(description = "打开方式 0当前窗口 1新窗口")
private Integer target;
@Schema(description = "导航类型")
private String navigationType;
@Schema(description = "子导航")
private List<MenuVo> children;
}

8
src/main/resources/application-prod.yml

@ -3,17 +3,19 @@
# 数据源配置
spring:
datasource:
url: jdbc:mysql://1Panel-mysql-Bqdt:3306/modules?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
url: jdbc:mysql://8.134.169.209:13306/modules?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username: modules
password: 8YdLnk7KsPAyDXGA
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
remove-abandoned: true
# redis
redis:
database: 0
host: 1Panel-redis-Q1LE
port: 6379
host: 8.134.169.209
port: 16379
password: redis_WSDb88
# 日志配置

2
src/main/resources/application.yml

@ -157,7 +157,7 @@ shop:
tenant-configs:
- tenant-id: 10324
tenant-name: "百色中学"
timeout-minutes: 60 # 捐款订单给更长的支付时间
timeout-minutes: 120 # 捐款订单给更长的支付时间
enabled: true
# 可以添加更多租户配置
# - tenant-id: 10550

110
src/test/java/com/gxwebsoft/WxDev.java

@ -0,0 +1,110 @@
package com.gxwebsoft;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.time.Instant;
import java.util.concurrent.atomic.AtomicReference;
@Service
public class WxDev {
@Value("${wechat.appid}")
private String appId;
@Value("${wechat.secret}")
private String secret;
private final StringRedisTemplate redisTemplate;
private final OkHttpClient http = new OkHttpClient();
private final ObjectMapper om = new ObjectMapper();
/** 简单本地缓存 access_token(生产建议放 Redis) */
private final AtomicReference<String> cachedToken = new AtomicReference<>();
private volatile long tokenExpireEpoch = 0L; // 过期的 epoch 秒
public WxDev(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
/** 获取/刷新 access_token */
public String getAccessToken() throws IOException {
long now = Instant.now().getEpochSecond();
System.out.println("cachedToken.get = " + cachedToken.get());
if (cachedToken.get() != null && now < tokenExpireEpoch - 60) {
return cachedToken.get();
}
HttpUrl url = HttpUrl.parse("https://api.weixin.qq.com/cgi-bin/token")
.newBuilder()
.addQueryParameter("grant_type", "client_credential")
.addQueryParameter("appid", "wx51962d6ac21f2ed2")
.addQueryParameter("secret", "d821f98de8a6c1ba7bc7e0ee84bcbc8e")
.build();
Request req = new Request.Builder().url(url).get().build();
try (Response resp = http.newCall(req).execute()) {
String body = resp.body().string();
JsonNode json = om.readTree(body);
if (json.has("access_token")) {
String token = json.get("access_token").asText();
int expiresIn = json.get("expires_in").asInt(7200);
System.out.println("token1 = " + token);
cachedToken.set(token);
tokenExpireEpoch = now + expiresIn;
return token;
} else {
throw new IOException("Get access_token failed: " + body);
}
}
}
/** 调用 getwxacodeunlimit,返回图片二进制 */
public byte[] getUnlimitedCode(String scene, String page, Integer width,
Boolean isHyaline, String envVersion) throws IOException {
String accessToken = getAccessToken();
System.out.println("accessToken = " + accessToken);
HttpUrl url = HttpUrl.parse("https://api.weixin.qq.com/wxa/getwxacodeunlimit")
.newBuilder()
.addQueryParameter("access_token", accessToken)
.build();
// 构造请求 JSON
// 注意:scene 仅支持可见字符,长度上限 32,尽量 URL-safe(字母数字下划线等)
// page 必须是已发布小程序内的路径(不带开头斜杠也可)
var root = om.createObjectNode();
root.put("scene", scene);
if (page != null) root.put("page", page);
if (width != null) root.put("width", width); // 默认 430,建议 280~1280
if (isHyaline != null) root.put("is_hyaline", isHyaline);
if (envVersion != null) root.put("env_version", envVersion); // release/trial/develop
RequestBody reqBody = RequestBody.create(
root.toString(), MediaType.parse("application/json; charset=utf-8"));
Request req = new Request.Builder().url(url).post(reqBody).build();
try (Response resp = http.newCall(req).execute()) {
if (!resp.isSuccessful()) {
throw new IOException("HTTP " + resp.code() + " calling getwxacodeunlimit");
}
MediaType ct = resp.body().contentType();
byte[] bytes = resp.body().bytes();
// 微信出错时返回 JSON,需要识别一下
if (ct != null && ct.subtype() != null && ct.subtype().contains("json")) {
String err = new String(bytes);
throw new IOException("WeChat error: " + err);
}
return bytes; // 成功就是图片二进制(PNG)
}
}
@Test
public void getQrCode() throws IOException {
final byte[] test = getUnlimitedCode("register", "pages/index/index",180,false,"develop");
System.out.println("test = " + test);
}
}

52
src/test/java/com/gxwebsoft/hjm/MqttServiceTest.java

@ -1,52 +0,0 @@
package com.gxwebsoft.hjm;
import com.gxwebsoft.hjm.service.MqttService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.Resource;
/**
* MQTT服务测试类
*
* @author 科技小王子
* @since 2025-07-02
*/
@SpringBootTest
@ActiveProfiles("dev")
public class MqttServiceTest {
@Resource
private MqttService mqttService;
@Test
public void testMqttConnection() {
System.out.println("MQTT连接状态: " + mqttService.isConnected());
System.out.println("MQTT客户端信息: " + mqttService.getClientInfo());
}
@Test
public void testMqttReconnect() {
try {
mqttService.reconnect();
System.out.println("MQTT重连测试完成");
} catch (Exception e) {
System.err.println("MQTT重连测试失败: " + e.getMessage());
}
}
@Test
public void testMqttPublish() {
try {
if (mqttService.isConnected()) {
mqttService.publish("/test/topic", "测试消息");
System.out.println("MQTT消息发布测试完成");
} else {
System.out.println("MQTT未连接,跳过发布测试");
}
} catch (Exception e) {
System.err.println("MQTT消息发布测试失败: " + e.getMessage());
}
}
}
Loading…
Cancel
Save