package com.ruoyi.system.controller;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.ControllerUtil.*;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.AppleDoMain.OrderApple;
import com.ruoyi.system.domain.AppleDoMain.AddressApple;
import com.ruoyi.system.service.*;
import com.winnerlook.model.VoiceResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.text.SimpleDateFormat;
import java.math.BigDecimal;
import com.ruoyi.system.config.QiniuConfig;
import com.ruoyi.system.utils.QiniuUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.WechatTransfer;
import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.system.domain.QuoteCraft;
import com.ruoyi.system.domain.QuoteType;
import com.ruoyi.system.domain.QuoteMaterialType;
import com.ruoyi.system.domain.QuoteMaterial;
/**
* 小程序控制器
*
* 提供小程序端所需的API接口
* 主要功能:
* 1. 服务分类管理
* 2. 服务商品列表和详情
* 3. 广告图片获取
* 4. 配置信息查询
* 5. 用户信息验证
*
* @author Mr. Zhang Pan
* @version 1.0
* @date 2025-05-26
*/
@RestController
public class AppletController extends BaseController {
@Autowired
private IServiceCateService serviceCateService;
@Autowired
private IUsersService usersService;
@Autowired
private ISiteConfigService siteConfigService;
@Autowired
private IAdvImgService advImgService;
@Autowired
private IServiceGoodsService serviceGoodsService;
@Autowired
private IUserAddressService userAddressService;
@Autowired
private IOrderService orderService;
@Autowired
private IOrderReworkService orderReworkService;
@Autowired
private ICooperateService cooperateService;
@Autowired
private ICouponUserService couponUserService;
@Autowired
private IIntegralLogService integralLogService;
@Autowired
private IIntegralOrderService integralOrderService;
@Autowired
private IIntegralProductService integralProductService;
@Autowired
private IIntegralOrderLogService integralOrderLogService;
@Autowired
private IIntegralCateService integralCateService;
@Autowired
private IOrderLogService orderLogService;
@Autowired
private IDiyCityService diyCityService;
@Autowired
private ISiteSkillService siteSkillService;
@Autowired
private QiniuConfig qiniuConfig;
@Autowired
private IGoodsCartService goodsCartService;
@Autowired
private IGoodsOrderService goodsOrderService;
@Autowired
private ICouponsService couponsService;
@Autowired
private IWorkerSignService workerSignService;
@Autowired
private IWorkerLevelService workerLevelService;
@Autowired
private IOrderCommentService orderCommentService;
@Autowired
private IWorkerMarginLogService workerMarginLogService;
@Autowired
private IWorkerMoneyLogService workerMoneyLogService;
@Autowired
private IWechatTransferService wechatTransferService;
@Autowired
private IQuoteCraftService quoteCraftService;
@Autowired
private IQuoteTypeService quoteTypeService;
@Autowired
private IQuoteMaterialTypeService quoteMaterialTypeService;
@Autowired
private IQuoteMaterialService quoteMaterialService;
@Autowired
private WechatPayUtil wechatPayUtil;
@Autowired
private IGoodsOrderCursorService goodsOrderCursorService;
@Autowired
private IWorkerApplyService workerApplyService;
@Autowired
private IUserMemberRechargeProgramService userMemberRechargeProgramService;
@Autowired
private IUserMemberRechargeLogService userMemberRechargeLogService;
@Autowired
private IUserMemnerConsumptionLogService userMemnerConsumptionLogService;
@Autowired
private IUsersInvoiceInfoService usersInvoiceInfoService;
@Autowired
private IUserGroupBuyingService userGroupBuyingService;
/**
* 获取服务分类列表
*
* @param request HTTP请求对象
* @return 分类列表数据
*
* 接口说明:
* - 获取状态为启用的服务分类
* - 自动添加图片CDN前缀
* - 支持用户登录状态验证
*/
@GetMapping(value = "/api/service/cate")
public AjaxResult getInfo(HttpServletRequest request) {
try {
// 验证用户登录状态(可选)
Map userData = AppletControllerUtil.getUserData(request.getHeader("token"), usersService);
// 构建查询条件:状态启用且类型为服务
ServiceCate serviceCateQuery = new ServiceCate();
serviceCateQuery.setStatus(1L); // 启用状态
// serviceCateQuery.setType(1L); // 服务类型
// 查询分类列表
List categoryList = serviceCateService.selectServiceCateList(serviceCateQuery);
// 为每个分类添加CDN前缀
for (ServiceCate category : categoryList) {
category.setIcon(AppletControllerUtil.buildImageUrl(category.getIcon()));
}
return AppletControllerUtil.appletSuccess(categoryList);
} catch (Exception e) {
return AppletControllerUtil.appletError("获取服务分类列表失败:" + e.getMessage());
}
}
/**
* 获取系统配置信息
*
* @param name 配置项名称
* @param request HTTP请求对象
* @return 配置信息数据
*
* 接口说明:
* - 根据配置名称获取对应的配置值
* - 配置值以JSON格式返回
* - 支持动态配置管理
*/
@GetMapping(value = "/api/public/config/{name}")
public AjaxResult config(@PathVariable("name") String name, HttpServletRequest request) {
try {
// 参数验证
if (name == null || name.trim().isEmpty()) {
return AppletControllerUtil.appletWarning("配置名称不能为空");
}
// 构建查询条件
SiteConfig configQuery = new SiteConfig();
configQuery.setName(name.trim());
// 查询配置列表
List configList = siteConfigService.selectSiteConfigList(configQuery);
if (!configList.isEmpty()) {
// 解析配置值为JSON对象
String configValue = configList.get(0).getValue();
if (configValue != null && !configValue.trim().isEmpty()) {
JSONObject jsonObject = JSONObject.parseObject(configValue);
return AppletControllerUtil.appletSuccess(jsonObject);
} else {
return AppletControllerUtil.appletWarning("配置值为空");
}
} else {
return AppletControllerUtil.appletWarning("未找到指定的配置项:" + name);
}
} catch (Exception e) {
return AppletControllerUtil.appletError("获取配置信息失败:" + e.getMessage());
}
}
/**
* 获取默认配置信息
*
* @param request HTTP请求对象
* @return 返回config_one配置的JSON对象
*
* 功能说明:
* - 获取名为"config_one"的系统配置
* - 将配置值解析为JSON对象并返回
* - 用于小程序获取基础配置信息
*/
@GetMapping(value = "/api/public/get/config")
public AjaxResult getconfig(HttpServletRequest request) {
try {
SiteConfig configQuery = new SiteConfig();
configQuery.setName("config_one");
List list = siteConfigService.selectSiteConfigList(configQuery);
if (list != null && !list.isEmpty()) {
JSONObject jsonObject = JSONObject.parseObject(list.get(0).getValue());
return AppletControllerUtil.appletSuccess(jsonObject);
} else {
return AppletControllerUtil.appletWarning("未找到默认配置信息");
}
} catch (Exception e) {
return AppletControllerUtil.appletError("获取配置信息失败:" + e.getMessage());
}
}
/**
* 查询用户收货地址列表
*
* @param limit 每页显示数量
* @param page 页码
* @param request HTTP请求对象
* @return 分页地址列表
*
* 请求参数:
* - limit: 每页显示数量,默认15
* - page: 页码,默认1
*
*/
@GetMapping("/api/user/address/list")
public AjaxResult getaddresslist(
@RequestParam(value = "limit", defaultValue = "15") int limit,
@RequestParam(value = "page", defaultValue = "1") int page,
HttpServletRequest request) {
try {
// 1. 验证分页参数
Map pageValidation = PageUtil.validatePageParams(page, limit);
if (!(Boolean) pageValidation.get("valid")) {
return AppletControllerUtil.appletWarning((String) pageValidation.get("message"));
}
// 2. 验证用户登录状态
String token = request.getHeader("token");
Map userValidation = AppletLoginUtil.validateUserToken(token, usersService);
if (!(Boolean) userValidation.get("valid")) {
return AppletControllerUtil.appletUnauthorized();
}
// 3. 获取用户信息
Users user = (Users) userValidation.get("user");
if (user == null) {
return AppletControllerUtil.appletWarning("用户信息获取失败");
}
// 4. 设置分页参数
PageHelper.startPage(page, limit);
// 5. 查询用户地址列表
UserAddress userAddressQuery = new UserAddress();
userAddressQuery.setUid(user.getId());
List addressList = userAddressService.selectUserAddressList(userAddressQuery);
// 6. 获取分页信息并构建响应
TableDataInfo tableDataInfo = getDataTable(addressList);
// 7. 构建符合要求的分页响应格式
Map pageData = PageUtil.buildPageResponse(tableDataInfo, page, limit);
return AppletControllerUtil.appletSuccess(pageData);
} catch (Exception e) {
System.err.println("查询用户地址列表异常:" + e.getMessage());
return AppletControllerUtil.appletError("查询地址列表失败:" + e.getMessage());
}
}
/**
* 根据地址ID查询用户收货地址详情
*
* @param id 地址ID
* @param request HTTP请求对象
* @return 地址详细信息
*
* 接口说明:
* - 根据地址ID获取单个地址的详细信息
* - 验证用户登录状态和地址归属权
* - 返回AddressApple格式的地址数据,用于前端修改页面
*
*/
@GetMapping("/api/user/address/info/{id}")
public AjaxResult getAddressInfo(@PathVariable("id") Long id, HttpServletRequest request) {
try {
// 1. 参数验证
if (id == null || id <= 0) {
return AppletControllerUtil.appletWarning("地址ID无效");
}
// 2. 验证用户登录状态
String token = request.getHeader("token");
Map userValidation = AppletLoginUtil.validateUserToken(token, usersService);
if (!(Boolean) userValidation.get("valid")) {
return AppletControllerUtil.appletUnauthorized();
}
// 3. 获取用户信息
Users user = (Users) userValidation.get("user");
if (user == null) {
return AppletControllerUtil.appletWarning("用户信息获取失败");
}
// 4. 查询地址信息
UserAddress userAddress = userAddressService.selectUserAddressById(id);
if (userAddress == null) {
return AppletControllerUtil.appletWarning("地址不存在");
}
// 5. 验证地址归属权
if (!userAddress.getUid().equals(user.getId())) {
return AppletControllerUtil.appletWarning("无权访问该地址信息");
}
// 6. 转换为AddressApple格式并返回
AddressApple addressApple = AddressApple.fromUserAddress(userAddress);
return AppletControllerUtil.appletSuccess(addressApple);
} catch (Exception e) {
System.err.println("查询用户地址详情异常:" + e.getMessage());
return AppletControllerUtil.appletError("查询地址详情失败:" + e.getMessage());
}
}
/**
* 修改用户收货地址
*
* @param params 地址修改参数
* @param request HTTP请求对象
* @return 修改结果
* 接口说明:
* - 验证用户登录状态和地址归属权
* - 支持修改地址的所有字段
* - 自动处理默认地址逻辑(设为默认时会取消其他默认地址)
* - 返回修改后的地址信息
*/
@PostMapping("/api/user/address/edit")
public AjaxResult editAddress(@RequestBody Map params, HttpServletRequest request) {
try {
// 1. 参数验证
if (params == null || params.get("id") == null) {
return AppletControllerUtil.appletWarning("地址ID不能为空");
}
Long addressId;
try {
addressId = Long.valueOf(params.get("id").toString());
if (addressId <= 0) {
return AppletControllerUtil.appletWarning("地址ID无效");
}
} catch (NumberFormatException e) {
return AppletControllerUtil.appletWarning("地址ID格式错误");
}
// 2. 验证用户登录状态
String token = request.getHeader("token");
Map userValidation = AppletLoginUtil.validateUserToken(token, usersService);
if (!(Boolean) userValidation.get("valid")) {
return AppletControllerUtil.appletUnauthorized();
}
// 3. 获取用户信息
Users user = (Users) userValidation.get("user");
if (user == null) {
return AppletControllerUtil.appletWarning("用户信息获取失败");
}
// 4. 查询原地址信息并验证归属权
UserAddress existingAddress = userAddressService.selectUserAddressById(addressId);
if (existingAddress == null) {
return AppletControllerUtil.appletWarning("地址不存在");
}
if (!existingAddress.getUid().equals(user.getId())) {
return AppletControllerUtil.appletWarning("无权修改该地址");
}
// 5. 构建更新的地址对象
UserAddress updateAddress = AppletControllerUtil.buildUpdateAddress(params, addressId, user.getId());
// 6. 验证必填字段
String validationResult = AppletControllerUtil.validateAddressParams(updateAddress);
if (validationResult != null) {
return AppletControllerUtil.appletWarning(validationResult);
}
// 7. 处理默认地址逻辑
if (updateAddress.getIsDefault() != null && updateAddress.getIsDefault() == 1L) {
// 如果设置为默认地址,先将该用户的所有地址设为非默认
userAddressService.updateUserAddressDefault(user.getId());
}
// 8. 执行地址更新
int updateResult = userAddressService.updateUserAddress(updateAddress);
if (updateResult > 0) {
return AppletControllerUtil.appletSuccess("地址修改成功");
} else {
return AppletControllerUtil.appletWarning("地址修改失败");
}
} catch (Exception e) {
System.err.println("修改用户地址异常:" + e.getMessage());
return AppletControllerUtil.appletError("修改地址失败:" + e.getMessage());
}
}
/**
* 新增用户收货地址
*
* @param params 地址新增参数
* @param request HTTP请求对象
* @return 新增结果
* 接口说明:
* - 验证用户登录状态
* - 支持新增地址的所有字段
* - 自动处理默认地址逻辑(设为默认时会取消其他默认地址)
* - 返回新增后的地址信息
*/
@PostMapping("/api/user/address/add")
public AjaxResult addAddress(@RequestBody Map params, HttpServletRequest request) {
try {
// 1. 验证用户登录状态
String token = request.getHeader("token");
Map userValidation = AppletLoginUtil.validateUserToken(token, usersService);
if (!(Boolean) userValidation.get("valid")) {
return AppletControllerUtil.appletUnauthorized();
}
// 2. 获取用户信息
Users user = (Users) userValidation.get("user");
if (user == null) {
return AppletControllerUtil.appletWarning("用户信息获取失败");
}
// 3. 构建新增的地址对象
UserAddress newAddress = AppletControllerUtil.buildNewAddress(params, user.getId());
// 4. 验证必填字段
String validationResult = AppletControllerUtil.validateAddressParams(newAddress);
if (validationResult != null) {
return AppletControllerUtil.appletWarning(validationResult);
}
// 5. 处理默认地址逻辑
if (newAddress.getIsDefault() != null && newAddress.getIsDefault() == 1L) {
// 如果设置为默认地址,先将该用户的所有地址设为非默认
userAddressService.updateUserAddressDefault(user.getId());
}
// 6. 执行地址新增
int insertResult = userAddressService.insertUserAddress(newAddress);
if (insertResult > 0) {
return AppletControllerUtil.appletSuccess("地址新增成功");
} else {
return AppletControllerUtil.appletWarning("地址新增失败");
}
} catch (Exception e) {
System.err.println("新增用户地址异常:" + e.getMessage());
return AppletControllerUtil.appletError("新增地址失败:" + e.getMessage());
}
}
/**
* 售后返修申请接口
*
* @param params 请求参数 包含order_id(订单ID)、phone(联系电话)、mark(返修原因备注)
* @param request HTTP请求对象
* @return 返修申请结果
*
* 功能说明:
* - 验证用户登录状态和订单归属权
* - 检查订单状态是否允许申请售后
* - 处理售后返修申请逻辑
* - 更新订单状态为售后处理中
* - 记录返修申请信息和联系方式
*
* 请求参数:
* - order_id: 订单ID
* - phone: 联系电话
* - mark: 返修原因备注
*/
@PostMapping("/api/service/order/rework")
public AjaxResult serviceOrderRework(@RequestBody Map params, HttpServletRequest request) {
try {
// 1. 参数验证
if (params == null) {
return AppletControllerUtil.appletWarning("请求参数不能为空");
}
String orderId = (String) params.get("order_id");
String phone = (String) params.get("phone");
String mark = (String) params.get("mark");
if (StringUtils.isEmpty(orderId)) {
return AppletControllerUtil.appletWarning("订单ID不能为空");
}
if (StringUtils.isEmpty(phone)) {
return AppletControllerUtil.appletWarning("联系电话不能为空");
}
// 验证手机号格式
if (!phone.matches("^1[3-9]\\d{9}$")) {
return AppletControllerUtil.appletWarning("联系电话格式不正确");
}
if (StringUtils.isEmpty(mark)) {
return AppletControllerUtil.appletWarning("返修原因不能为空");
}
// 2. 验证用户登录状态
String token = request.getHeader("token");
Map userValidation = AppletLoginUtil.validateUserToken(token, usersService);
if (!(Boolean) userValidation.get("valid")) {
return AppletControllerUtil.appletWarning("用户未登录或token无效");
}
// 3. 获取用户信息
Users user = (Users) userValidation.get("user");
if (user == null) {
return AppletControllerUtil.appletWarning("用户信息获取失败");
}
// 4. 查询订单信息并验证归属权
Order order = orderService.selectOrderByOrderId(orderId);
if (order == null) {
return AppletControllerUtil.appletWarning("订单不存在");
}
if (!order.getUid().equals(user.getId())) {
return AppletControllerUtil.appletWarning("无权操作此订单");
}
// 5. 验证订单状态是否允许申请售后
if (!AppletControllerUtil.isOrderAllowRework(order.getStatus())) {
return AppletControllerUtil.appletWarning("当前订单状态不允许申请售后");
}
// 6. 处理售后返修申请
boolean reworkResult = AppletControllerUtil.processReworkApplication(order, phone, mark, user, orderReworkService, orderService);
if (reworkResult) {
return AppletControllerUtil.appletSuccess("售后返修申请已提交,我们会尽快联系您处理");
} else {
return AppletControllerUtil.appletWarning("售后申请提交失败,请稍后重试");
}
} catch (Exception e) {
System.err.println("售后返修申请异常:" + e.getMessage());
return AppletControllerUtil.appletWarning("售后申请失败:" + e.getMessage());
}
}
/**
* 获取用户服务订单列表
*
* @param params 请求参数 包含page(页码)、limit(每页数量)、status(订单状态)
* @param request HTTP请求对象
* @return 返回分页的服务订单列表
*
* 功能说明:
* - 获取当前登录用户的服务类订单(type=1)
* - 支持按订单状态筛选
* - 返回带分页信息的订单列表
* - 自动填充商品信息(标题、图标、价格等)
*/
@PostMapping("api/service/order/lst")
public AjaxResult getserviceorderlst(@RequestBody Map params,
HttpServletRequest request) {
int page = (int) params.get("page");
int limit = (int) params.get("limit");
String status = (String) params.get("status");
// 1. 验证分页参数
Map pageValidation = PageUtil.validatePageParams(page, limit);
if (!(Boolean) pageValidation.get("valid")) {
return AppletControllerUtil.appletWarning((String) pageValidation.get("message"));
}
// 2. 验证用户登录状态
String token = request.getHeader("token");
Map userValidation = AppletLoginUtil.validateUserToken(token, usersService);
if (!(Boolean) userValidation.get("valid")) {
return AppletControllerUtil.appletWarning("用户未登录或token无效");
}
// 3. 获取用户信息
Users user = (Users) userValidation.get("user");
if (user == null) {
return AppletControllerUtil.appletWarning("用户信息获取失败");
}
// 4. 设置分页参数
PageHelper.startPage(page, limit);
// 5. 查询用户地址列表
OrderApple order = new OrderApple();
// order.setType(1);
order.setUid(user.getId());
if (StringUtils.isNotNull(status) && !"".equals(status)) {
order.setStatus(Long.valueOf(status));
}
List orderList = orderService.selectOrderAppleList(order);
for (OrderApple orderdata : orderList) {
Map jsonObject = new HashMap<>();
if (orderdata.getProduct_id() != null) {
ServiceGoods serviceGoods = serviceGoodsService.selectServiceGoodsById(orderdata.getProduct_id());
if (serviceGoods != null) {
jsonObject.put("title", serviceGoods.getTitle());
jsonObject.put("icon", "https://img.huafurenjia.cn/" + serviceGoods.getIcon());
jsonObject.put("id", serviceGoods.getId());
jsonObject.put("price_zn", serviceGoods.getPriceZn());
jsonObject.put("sub_title", serviceGoods.getSubTitle());
orderdata.setProduct(jsonObject);
}
}
}
// 6. 获取分页信息并构建响应
TableDataInfo tableDataInfo = getDataTable(orderList);
// 7. 构建符合要求的分页响应格式
Map pageData = PageUtil.buildPageResponse(tableDataInfo, page, limit);
return AppletControllerUtil.appletSuccess(pageData);
}
/**
* 获取用户商品订单列表
*
* @param params 请求参数 包含page(页码)、limit(每页数量)、status(订单状态)
* @param request HTTP请求对象
* @return 返回分页的商品订单列表
*
* 功能说明:
* - 获取当前登录用户的商品类订单
* - 支持按订单状态筛选
* - 返回带分页信息的订单列表
* - 包含完整的商品信息和订单状态文本
*/
@PostMapping("/api/goods/order/lst")
public AjaxResult getgoodsorderlst(@RequestBody Map params,
HttpServletRequest request) {
try {
int page = (int) params.get("page");
int limit = (int) params.get("limit");
String status = (String) params.get("status");
// 1. 验证分页参数
Map pageValidation = PageUtil.validatePageParams(page, limit);
if (!(Boolean) pageValidation.get("valid")) {
return AppletControllerUtil.appletWarning((String) pageValidation.get("message"));
}
// 2. 验证用户登录状态
String token = request.getHeader("token");
Map userValidation = AppletLoginUtil.validateUserToken(token, usersService);
if (!(Boolean) userValidation.get("valid")) {
return AppletControllerUtil.appletUnauthorized();
}
// 3. 获取用户信息
Users user = (Users) userValidation.get("user");
if (user == null) {
return AppletControllerUtil.appletWarning("用户信息获取失败");
}
// 4. 设置分页参数
PageHelper.startPage(page, limit);
// 5. 构建查询条件
GoodsOrder queryOrder = new GoodsOrder();
queryOrder.setUid(user.getId());
if (StringUtils.isNotNull(status) && !"".equals(status)) {
queryOrder.setStatus(Long.valueOf(status));
}
// 6. 查询商品订单列表
List orderList = goodsOrderService.selectGoodsOrderList(queryOrder);
// 7. 为每个订单填充商品信息
List