javacodeadmin/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/HomeUtril.java

450 lines
18 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ruoyi.system.ControllerUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IAdvImgService;
import com.ruoyi.system.service.IServiceCateService;
import com.ruoyi.system.service.IServiceGoodsService;
import com.ruoyi.system.service.ISiteConfigService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HomeUtril {
private static final IAdvImgService advImgService = SpringUtils.getBean(IAdvImgService.class);
private static final ISiteConfigService siteConfigService = SpringUtils.getBean(ISiteConfigService.class);
private static final IServiceCateService serviceCateService = SpringUtils.getBean(IServiceCateService.class);
private static final IServiceGoodsService serviceGoodsService = SpringUtils.getBean(IServiceGoodsService.class);
/**
* 获取广告图片列表
*
* @return 广告图片列表
* <p>
* 接口说明:
* - 根据广告类型获取对应的图片列表
* - 自动添加图片CDN前缀
* - 支持多种广告位配置
*/
public Map<String, Object> getAdvImgData(long type) {
try {
Map<String, Object> result = new HashMap<>();
// 构建查询条件
AdvImg advImgQuery = new AdvImg();
advImgQuery.setType(type);
advImgQuery.setStatus(1L);
// 查询广告图片列表
List<AdvImg> advImgList = advImgService.selectAdvImgList(advImgQuery);
System.out.println("#####################"+advImgList.size());
// 为每张图片添加CDN前缀
for (AdvImg advImg : advImgList) {
advImg.setImage(AppletControllerUtil.buildImageUrl(advImg.getImage()));
}
result.put("data", advImgList);
return result;
} catch (Exception e) {
return AppletControllerUtil.appletWarning("获取广告图片失败:" + e.getMessage());
}
}
/**
* 获取首页通知公告列表接口
*
* @return 上线状态的通知公告列表
* <p>
* 接口说明:
* - 查询config_seven配置中的通知公告数据
* - 只返回status=1上线状态的公告
* - 按sort字段升序排列
* - 返回title、content、link、sort等字段
* - 无需用户登录验证
* <p>
*/
public Map<String, Object> getHomeNoticeList(String categoryParam) {
try {
Map<String, Object> result = new HashMap<>();
String filterCategory = null;
if ("1".equals(categoryParam)) {
filterCategory = "home";
} else if ("2".equals(categoryParam)) {
filterCategory = "activity";
}
// 1. 查询config_seven配置
SiteConfig configQuery = new SiteConfig();
configQuery.setName("config_seven");
List<SiteConfig> configList = siteConfigService.selectSiteConfigList(configQuery);
if (configList.isEmpty()) {
// 配置不存在时返回空数组
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
SiteConfig config = configList.get(0);
String configValue = config.getValue();
if (configValue == null || configValue.trim().isEmpty()) {
// 配置值为空时返回空数组
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
// 2. 解析JSON配置
JSONObject configJson;
try {
configJson = JSONObject.parseObject(configValue);
} catch (Exception e) {
System.err.println("解析config_seven配置JSON失败" + e.getMessage());
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
// 3. 获取notice数组
if (!configJson.containsKey("notice")) {
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
Object noticeObj = configJson.get("notice");
if (!(noticeObj instanceof JSONArray)) {
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
JSONArray noticeArray = (JSONArray) noticeObj;
// 4. 过滤上线状态的公告并构建返回数据
List<Map<String, Object>> resultList = new ArrayList<>();
for (int i = 0; i < noticeArray.size(); i++) {
try {
JSONObject noticeItem = noticeArray.getJSONObject(i);
// 检查状态是否为上线status=1
Integer status = noticeItem.getInteger("status");
if (status == null || status != 1) {
continue; // 跳过下线或状态异常的公告
}
// 新增:按分类过滤
if (filterCategory != null) {
String itemCategory = noticeItem.getString("category");
if (itemCategory == null) itemCategory = "home"; // 兼容老数据
if (!filterCategory.equals(itemCategory)) {
continue;
}
}
// 构建公告数据
Map<String, Object> notice = new HashMap<>();
notice.put("title", noticeItem.getString("title"));
notice.put("content", noticeItem.getString("content"));
notice.put("link", noticeItem.getString("link"));
notice.put("sort", noticeItem.getInteger("sort"));
notice.put("status", status);
resultList.add(notice);
} catch (Exception e) {
// 单个公告解析失败时跳过,继续处理其他公告
System.err.println("解析单个公告数据失败:" + e.getMessage());
continue;
}
}
// 5. 按sort字段升序排序
resultList.sort((a, b) -> {
Integer sortA = (Integer) a.get("sort");
Integer sortB = (Integer) b.get("sort");
if (sortA == null) sortA = Integer.MAX_VALUE;
if (sortB == null) sortB = Integer.MAX_VALUE;
return sortA.compareTo(sortB);
});
result.put("data", resultList);
return result;
} catch (Exception e) {
System.err.println("获取首页通知公告列表异常:" + e.getMessage());
return AppletControllerUtil.appletError("获取通知公告列表失败:" + e.getMessage());
}
}
/**
* 获取服务分类列表
* 功能说明:
* - 获取状态为启用的服务分类
* - 支持二级分类树形结构
* - 将二级分类组装在一级分类下的children字段
* - 只返回title和icon字段
* - 自动添加图片CDN前缀
* - 支持用户登录状态验证(可选)
*
* @return 分类树形列表数据只包含title和icon
*/
public Map<String, Object> getServiceCategories(String city) {
try {
Map<String, Object> result = new HashMap<>();
// 1. 查询所有启用状态的服务分类
ServiceCate serviceCateQuery = new ServiceCate();
serviceCateQuery.setStatus(1L);
serviceCateQuery.setType(1L);
if (StringUtils.isNotBlank(city)){
serviceCateQuery.setCity(city);
}
List<ServiceCate> allCategoryList = serviceCateService.selectServiceCateList(serviceCateQuery);
// 2. 分离一级分类和二级分类
List<ServiceCate> firstLevelCategories = new ArrayList<>();
Map<Long, List<ServiceCate>> secondLevelMap = new HashMap<>();
for (ServiceCate category : allCategoryList) {
if (category.getParentId() == null || category.getParentId() == 0L) {
// 一级分类
firstLevelCategories.add(category);
} else {
// 二级分类按父级ID分组
secondLevelMap.computeIfAbsent(category.getParentId(), k -> new ArrayList<>()).add(category);
}
}
// 3. 构建简化的分类数据只包含title和icon
List<Map<String, Object>> resultList = new ArrayList<>();
for (ServiceCate firstLevel : firstLevelCategories) {
Map<String, Object> firstLevelData = new HashMap<>();
firstLevelData.put("id", firstLevel.getId());
firstLevelData.put("title", firstLevel.getTitle());
firstLevelData.put("icon", AppletControllerUtil.buildImageUrl(firstLevel.getIcon()));
// 4. 处理二级分类
List<Map<String, Object>> childrenList = new ArrayList<>();
List<ServiceCate> children = secondLevelMap.get(firstLevel.getId());
if (children != null && !children.isEmpty()) {
for (ServiceCate child : children) {
Map<String, Object> childData = new HashMap<>();
childData.put("id", child.getId());
childData.put("title", child.getTitle());
childData.put("icon", AppletControllerUtil.buildImageUrl(child.getIcon()));
childrenList.add(childData);
}
}
firstLevelData.put("children", childrenList);
resultList.add(firstLevelData);
}
result.put("data", resultList);
return result;
} catch (Exception e) {
return AppletControllerUtil.appletError("获取服务分类列表失败:" + e.getMessage());
}
}
/**
* 获取活动专区展示接口
* 查询config_eight配置返回activities数组
*/
public Map<String, Object> getActivityList() {
try {
Map<String, Object> result = new HashMap<>();
// 查询config_eight配置
SiteConfig configQuery = new SiteConfig();
configQuery.setName("config_eight");
List<SiteConfig> configList = siteConfigService.selectSiteConfigList(configQuery);
if (configList.isEmpty()) {
// 配置不存在时返回空数组
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
SiteConfig config = configList.get(0);
String configValue = config.getValue();
if (configValue == null || configValue.trim().isEmpty()) {
// 配置值为空时返回空数组
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
// 解析JSON配置
JSONObject configJson;
try {
configJson = JSONObject.parseObject(configValue);
} catch (Exception e) {
System.err.println("解析config_eight配置JSON失败" + e.getMessage());
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
// 获取activities数组
if (!configJson.containsKey("activities")) {
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
Object activitiesObj = configJson.get("activities");
if (!(activitiesObj instanceof JSONArray)) {
return AppletControllerUtil.appletSuccess(new ArrayList<>());
}
JSONArray activitiesArray = (JSONArray) activitiesObj;
// 直接返回activities数组
result.put("data", activitiesArray);
return result;
} catch (Exception e) {
System.err.println("获取活动专区列表异常:" + e.getMessage());
return AppletControllerUtil.appletError("获取活动专区列表失败:" + e.getMessage());
}
}
/**
* 首页拼团专区列表接口
* 查询type=1且isgroup=1的服务商品前4个
* 返回icon、标题、price、groupprice字段
*/
public Map<String, Object> getGroupList(String city) {
try {
Map<String, Object> result = new HashMap<>();
// 构建查询条件
ServiceGoods queryGoods = new ServiceGoods();
if (StringUtils.isNotBlank(city)){
queryGoods.setCity(city);
}
queryGoods.setType(1); // type=1
queryGoods.setIsgroup(1); // isgroup=1
queryGoods.setStatus("1"); // 只查询启用状态的商品
// 查询服务商品列表
List<ServiceGoods> goodsList = serviceGoodsService.selectServiceGoodsList(queryGoods);
// 只取前4个
if (goodsList.size() > 4) {
goodsList = goodsList.subList(0, 6);
}
// 构建返回数据
List<Map<String, Object>> resultList = new ArrayList<>();
for (ServiceGoods goods : goodsList) {
Map<String, Object> goodsData = new HashMap<>();
// 基本信息
goodsData.put("id", goods.getId());
goodsData.put("groupnum", goods.getGroupnum());
goodsData.put("title", goods.getTitle());
goodsData.put("price", goods.getPrice() != null ? goods.getPrice().toString() : "0.00");
goodsData.put("groupprice", goods.getGroupprice() != null ? goods.getGroupprice().toString() : "0.00");
// 处理图片URL - 添加CDN前缀
goodsData.put("icon", AppletControllerUtil.buildImageUrl(goods.getIcon()));
resultList.add(goodsData);
}
result.put("data", resultList);
return result;
} catch (Exception e) {
System.err.println("获取拼团专区列表异常:" + e.getMessage());
return AppletControllerUtil.appletError("获取拼团专区列表失败:" + e.getMessage());
}
}
/**
* 获取系统配置信息
* 功能说明:
* - 根据配置名称获取对应的配置值
* - 配置值以JSON格式返回
* - 支持动态配置管理
*
* @param name 配置项名称
* @return 配置信息数据
*/
public Map<String, Object> getConfig(String name) {
try {
Map<String, Object> result = new HashMap<>();
name="config_two";
// 参数验证
if (StringUtils.isEmpty(name)) {
return AppletControllerUtil.appletWarning("配置名称不能为空");
}
// 查询配置信息
SiteConfig config = AppletControllerUtil.getSiteConfig(name, siteConfigService);
if (config == null) {
return AppletControllerUtil.appletWarning("未找到指定的配置项:" + name);
}
// 解析配置值为JSON对象
JSONObject configJson = AppletControllerUtil.parseConfigValue(config.getValue());
result.put("data", configJson);
return result;
} catch (Exception e) {
return AppletControllerUtil.appletError("获取配置信息失败:" + e.getMessage());
}
}
/**
* 获取系统配置信息
* 功能说明:
* - 根据配置名称获取对应的配置值
* - 配置值以JSON格式返回
* - 支持动态配置管理
*
* @param name 配置项名称
* @return 配置信息数据
*/
public Map<String, Object> getConfigData(String name) {
try {
Map<String, Object> result = new HashMap<>();
// 参数验证
if (StringUtils.isEmpty(name)) {
return AppletControllerUtil.appletWarning("配置名称不能为空");
}
// 查询配置信息
SiteConfig config = AppletControllerUtil.getSiteConfig(name, siteConfigService);
if (config == null) {
return AppletControllerUtil.appletWarning("未找到指定的配置项:" + name);
}
// 解析配置值为JSON对象
JSONObject configJson = AppletControllerUtil.parseConfigValue(config.getValue());
result.put("data", configJson);
return result;
} catch (Exception e) {
return AppletControllerUtil.appletError("获取配置信息失败:" + e.getMessage());
}
}
/**
* 判断用户的会员性质
* @param user 用户对象
* @return 1=会员0=新会员2=续费会员,-1=未知
*/
public static int getMemberType(Users user) {
if (user == null) return -1;
Integer ismember = user.getIsmember();
Object begin = null, end = null;
try {
begin = user.getClass().getMethod("getMemberBegin").invoke(user);
end = user.getClass().getMethod("getMemberEnd").invoke(user);
} catch (Exception e) {
// 兼容没有getMemberBegin/getMemberEnd方法的情况
}
if (ismember != null && ismember == 0) {
if (begin == null && end == null) {
return 0; // 新会员
} else if (begin != null && end != null) {
return 2; // 续费会员
}
} else if (ismember != null && ismember == 1) {
return 1; // 会员
}
return -1;
}
}